我已经build立了一个使用OpenSSH的SFTP服务器,一切正常,我创build的用户可以连接。
在authentication之后,用户直接在/chroot里find他们不允许写入的目录。 所以我把一个/subdirectory放到/chroot他们有写权限(受这个博客文章的启发),这也很好。
但是,由于我正在进行的项目的性质,用户应该直接在一个文件夹中,他们被允许在authentication后写入。 转发到/chroot/subdirectory可能是最好的解决scheme,但我没有find解释如何实现这一目标的资源。
可以这样做吗? 怎么样?
[编辑]是的,我相信这是可能的,但我也不相信openssh:
这里是我如何使用openssh chroot sftp:
我把sftp用户放在sshd_config文件中标识的特殊组中。 我确信sftp用户没有shell(所以他们不能用sshlogin),并使用.%h环境variables强制它们成为一个使用ChrootDirectory指令命名的主目录下的sftp chroot子目录。 其他由sshd_config解释的环境variableslogging在sshd_conf手册页中,如下所示:
path名可以包含以下在连接用户被authentication后在运行时展开的标记:%%被字面值'%'replace,%h被replace为被authentication的用户的主目录,%u被replace由该用户的用户名。
这里是我在OpenBSD上实现这个function的一个副本,如果你使用不同的系统, .%h环境variables当然可能是不同的:
# 1. Create the sftp jail directories # These directory permissions work with this /etc/ssh/sshd_config: # drwxr-xr-x 4 root wheel 512 May 14 16:20 /home/sftproot # drwxr-xr-x 3 root wheel 512 May 14 16:21 /home/sftproot/home # drwxr-xr-x 3 root wheel 512 May 14 16:37 /home/sftproot/home/User01 # drwxr-xr-x 3 User01 sftponly 512 May 14 16:39 /home/sftproot/home/User01/upload # drwxr-xr-x 3 root wheel 512 May 14 16:37 /home/sftproot/home/User02 # drwxr-xr-x 3 User02 sftponly 512 May 14 16:39 /home/sftproot/home/User02/upload # 2. Make sure /etc/ssh/sshd_config jails /home/sftproot/.%h # 3. Create a group whose members will only be allowed sftp access # groupadd sftponly # 4. Create User01 + User02 whom will only get sftp access # useradd -s /sbin/nologin -m -G sftponly User01 # useradd -s /sbin/nologin -m -G sftponly User02 # 5. In /etc/ssh/sshd_config enable use of chroot(internal-sftp) then force chroot dirs per user: # override default of no subsystems # Subsystem sftp /usr/libexec/sftp-server # Subsystem sftp internal-sftp # Rules for sftponly members # Match group sftponly # ChrootDirectory /home/sftproot/.%h # X11Forwarding no # AllowTcpForwarding no # ForceCommand internal-sftp # [Comment] Make sure /etc/ssh/sshd_config jails /home/sftproot/.%h # Which will translate .%h to /home/$username # [Comment] The sftp users will not be able to log in outside of sftp (as they have no shell). # As they sftp in they will land in the /home/sftproot/home/Userxx directory which # will be named "./" and where they have no write access. # However the directory ./upload is read/writable.
[编辑第2部分]但是, sshd_conf手册页还指定:
ChrootDirectory
指定要validation后chroot(2)的目录的path名。 在会话启动时,sshd(8)会检查path名的所有组件是否为任何其他用户或组所不能写入的根拥有的目录。 在chroot之后,sshd(8)将工作目录更改为用户的主目录。
因此,chroot目录path(包括由variables扩展指定的部分)是预期的,并且通过sshd进行testing,仅由root拥有和写入。 因此,openssh sftp chrooted服务的用户需要可写入的子目录才能写入主目录。
我相信这不是所有的SSH服务器的要求,但是。 我们也使用Tectia ,我发现用户可以写入他们各自的根目录。 不过,我们只在需要Windows的地方运行它,所以很遗憾我不能轻易testing相应的* nixconfiguration。 Tectia sftp chrooting支持页面没有明确指定用户home需要在Unix环境中由root拥有。 因此,我会猜测,与Tectia这不是一个要求,但是一个chroot用户home rootdir的所有权可能是实际用户的所有权。