我刚刚安装了一个基于 CentOS 的 SFTP 服务器。
我需要所有传入的文件都转到 /mnt/inbound/ 文件夹,因此我想确保来自该主机的每个通过 SFTP 登录的用户都将 /mnt/inbound/ 作为他们的起点,并且我想确保他们不能去其他任何地方。
我已经能够使用 SFTP 客户端与测试用户连接并确保用户被监禁在各自的文件夹中——但用户无法上传文件......
这是我到目前为止所做的:
$ groupadd sftponly
# Enable built-in implementation of SFTP Subsystem sftp internal-sftp
Match Group sftponly # Force the connection to use the built-in SFTP support ForceCommand internal-sftp # Chroot the connection into the specified directory ChrootDirectory /mnt/inbound/%u # Disable network tunneling PermitTunnel no # Disable authentication agent forwarding AllowAgentForwarding no # Disable TCP connection forwarding AllowTcpForwarding no # Disable X11 remote desktop forwarding X11Forwarding no
$ sudo useradd -g sftponly testuser
$ sudo mkdir /mnt/inbound/testuser
我现在可以使用 FileZilla(或任何其他客户端)与该主机建立 SFTP 连接,并且我可以看到用户被监禁在 /mnt/inbound/testuser 文件夹中。但是用户不能上传文件。
我曾尝试更改 /mnt/inbound/test 文件夹的权限,以便用户 test 可以访问它,但这会破坏用户通过 SFTP 进行连接的能力。
我无法将文件夹的所有者更改为 root 以外的所有者,那么如何确保用户可以读取和写入其各自的文件夹?
我在互联网(包括 StackExchange)上看到了几次回答类似问题的尝试,但似乎都缺少一两点——因为当我尝试按照答案中提供的说明进行操作时,我总是以损坏的 SFTP 设置结束。
问候,P。
pmd*_*dci 13
是的,我设法在 #openssh IRC 频道获得了一些建议,这是我的解决方案中缺少的内容:
ChrootDirectory 中指定的目录必须由 root 拥有。因为在上面的sshd_config
文件中我已经指定了%u
变量,所以每个用户都有自己的根目录基于他们的用户名(例如testuser
将是/mnt/inbound/testuser/
),那么所有这些目录都必须由 root 拥有。这实际上是我创建目录时的默认设置,sudo mkdir /mnt/inbound/<username>
因为mkdir
命令是通过sudo
.
所以我需要做的是在下创建一个子目录/mnt/inbound/<username>
并为用户授予该目录权限。就我而言,我称此目录为uploads
。
所以我稍微改变了我的配置如下:
Match Group sftponly # Chroot the connection into the specified directory ChrootDirectory /mnt/inbound/%u # Force the connection to use the built-in SFTP support ForceCommand internal-sftp -d /uploads
该ForceCommand
行已更改为 include -d /uploads
,表示用户登录后的默认目录为/uploads
。请注意,它是/uploads
而不是/mnt/inbound/%u/uploads
因为它考虑到/mnt/inbound/%u
已在配置的前一行中指定为新根。
如果我执行ChrootDirectory /mnt/inbound/
then 指定ForceCommand internal-sftp -d /%u
,我可以使/mnt/inbound/<username>
文件夹归最终用户所有,因为/mnt/inbound
现在是必须归根帐户所有的新根目录。但是,用户将能够导航到父文件夹并查看所有其他帐户的目录名称。我决定反对:)