只有当chroot时sftp才允许拒绝权限?

我已经在我的Centos框中configuration了sshd_conf,如下所示:

Match group pilots ChrootDirectory /home/pilots ForceCommand internal-sftp X11Forwarding no AllowTcpForwarding no 

和/ home / pilot这样的目录:

 # ls -al /home/pilots total 12 drwxr-x---. 3 root pilots 4096 Mar 10 14:20 . drwxr-xr-x. 7 root root 4096 Mar 10 14:10 .. drwxrwxr-x. 2 root pilots 4096 Mar 10 15:21 data -rwxrwxrwx. 1 root root 0 Mar 10 14:20 topLevel # 

如果我在启动ChrootDirectory指令的情况下以飞行员组的用户身份login,我可以进入/ home / pilots文件夹(或其子目录)并执行ls或毫不费力地完成任务。 但是,如果我启用ChrootDirectory指令,而我仍然可以在sftp中,并可以cd到数据,我不能做一个LS或在任一目录中。 例如,尝试ls给出了远程readdir(“/”):权限被拒绝的错误,并且试图获得topLevel时,没有findFile“/ topLevel”。 我在想,也许我不在我期望的目录中,但是cd数据的能力似乎表明chroot的确按照预期工作。

看消息日志,当ls被拒绝时,我看到以下内容:

 type=1400 audit(1394494944.504:50): avc: denied { read } for pid=22758 comm="sshd" name="pilots" dev=dm-0 ino=400504 scontext=unconfined_u:system_r:chroot_user_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=dir 

所以有一个拒绝的logging。 尽pipe如此,仍然不能告诉我为什么。

我可能会做错什么?

几个潜在的重要笔记:

  • 有问题的用户存在于远程LDAP服务器上,通过sssd访问
  • 访问控制必须按组进行,因为许多用户需要对同一个文件夹进行读取访问。 因此,所有权仍然是根。

编辑:进一步调查,这似乎与SELinux有关 – 做一个echo 0 >/selinux/enforce修复了这个问题,虽然在一个kludgy,用大锤杀死一个ant。 如果可能的话,我想知道“正确的”修复。

我在这个页面上find了解决scheme。 总而言之,按照上述configurationconfigurationsftp之后,需要运行以下两个命令,以允许在启用SELinux的情况下进行访问:

 setsebool -P ssh_chroot_rw_homedirs on restorecon -R /home/$USERNAME 

在这种情况下,第二个命令是restorecon -R /home/pilots 。 在此之后,即使在chroot时,sftp也能按预期工作,而不必完全禁用SELinux。

从@ ibrewster的回答 (包括他所链接的外部资源 )的尾巴开始,这里是来自该外部页面的全套指令,还有一些额外的信息使得无密码login和SELinux强制执行工作成为可能。

以防万一外部链接的页面在将来消失。

这些说明适用于RHEL7和CentOS7(也可能是其他版本):

在远程系统上:

首先,添加和configuration用户帐户chrooted:

请注意, 外部资源为sftp-server使用了不同的path。 确保你的系统有正确的path,或准备好自己的痛苦。 ;-)下面的path适用于RHEL7和CentOS7的最小安装。

 # From command line: groupadd sftponly useradd -d /home/$USERNAME -s /usr/libexec/openssh/sftp-server -M -N -g sftponly $USERNAME mkdir -p /home/$USERNAME/uploads /home/$USERNAME/downloads /home/$USERNAME/.ssh chown $USERNAME:sftponly /home/$USERNAME/uploads /home/$USERNAME/downloads /home/$USERNAME/.ssh chown root /home/$USERNAME chmod 755 /home/$USERNAME chmod 700 /home/$USERNAME/.ssh passwd $USERNAME echo '/usr/libexec/openssh/sftp-server' >> /etc/shells 

虽然我在上面设置了密码,但是一旦知道configuration的工作方式,我将使用无密码login。 向前…

假设您已经启用了SELinux并执行(您应该),请发出以下命令使其更加快乐:

 setsebool -P ssh_chroot_rw_homedirs on restorecon -R /home/$USERNAME 

现在,编辑sshdconfiguration如下:

 [root@remote]# vi /etc/ssh/sshd_config # # CHANGE lines: # # override default of no subsystems #Subsystem sftp /usr/libexec/openssh/sftp-server # commented out Subsystem sftp internal-sftp # added # # ADD the following at the bottom: # Match group sftponly ChrootDirectory %h AllowTcpForwarding no ForceCommand internal-sftp 

最后,重新启动sshd

 [root@remote]# systemctl restart sshd.service 

在客户端系统上

首先在本地创build相同的帐户。

 [root@client]# useradd -m $USERNAME [root@client]# passwd $USERNAME [root@client]# su $USERNAME 

好的,现在让我们设置我们的RSA密钥对。

 [$USERNAME@client]# ssh-keygen 

我无法获得ssh-copy-id与上面的chrootconfiguration一起工作,所以我用我的客户端的id_rsa.pub文本手动创build了authorized_keys文件。

 [$USERNAME@client]# cat .ssh/id_rsa.pub ---some key here--- 

然后回到REMOTE系统,

 [root@remote]# vi /home/$USERNAME/.ssh/authorized_keys ---past key from right above, then wq--- 

不要忘记在这个文件上设置权限:

 [root@remote]# chown $USERNAME:sftpusers /home/$USERNAME/.ssh/authorized_keys 

准备testing

一切都应该到位。 从客户端:

 [$USERNAME@client]# sftp $USERNAME@remote 

这应该让你进来。如果提示你input密码(我们还没有在远程上禁用PasswordAuthentication),你的远程系统上有一个configuration问题。 扫描您的/ var / log / secure以获取详细信息。

如果您没有提示input密码而login,那么您几乎已经完成了。

回到远程系统:

 [root@remote]# vi /etc/ssh/sshd_config # disable PasswordAuthentication PasswordAuthentication no # or optionally, just comment that line out # PasswordAuthentication no -- save and exit with :wq -- 

在远程系统上重新启动sshd:

 [root@remote]# systemctl restart sshd.service 

来自客户的最终testing

 [$USERNAME@client]# sftp $USERNAME@remote # in like Flynn? yay! 

成品

您应该设置为使用chroot sftp和无密码loginSELinux设置为强制执行