如何解释结合不同 ftp 目录 chmod 设置的 sftp 登录成功/失败行为?

如何解释结合不同 ftp 目录 chmod 设置的 sftp 登录成功/失败行为?

如果我从 -

sudo groupadd test_group
sudo useradd -g test_group -s /sbin/nologin test_user
sudo passwd test_user
    ...(some password)...
sudo mkdir -p /testdir/dev

我还有“/etc/ssh/sshd_config”,如下 -

Include /etc/ssh/sshd_config.d/*.conf
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding no
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp internal-sftp
Match User test_user
        ChrootDirectory /testdir
        PermitTunnel no
        ForceCommand internal-sftp -d /dev
        X11Forwarding no
        AllowTcpForwarding no

同样没有任何“iptables”规则,那么我可以成功地从另一台机器(在本例中,它是一台 Windows 电脑,使用 filezilla)进行 SFTP 连接,指定“test_user”用户名和密码,它会显示并限制我仅限 /testdir/dev 目录的内容。

但是,我注意到 SFTP 会话仅列出目录内容;它没有实际发送/写入文件的权限。即我有 -

drwxr-xr-x  root root ... /testdir
drwxr-xr-x  root root ... /testdir/dev

很公平。所以现在我想加强所有权和权限,以便我的“test_user”可以在“/testdir”下读写,并且任何“test_group”用户都可以读取,而其他用户没有任何访问权限。然后我会-

sudo chown -R test_user:test_group /testdir
sudo chmod -R 640 /testdir

现在,我之前成功的 SFTP 连接现在失败了,并显示“错误:无法连接到服务器”。

不知道为什么,但是更自由、免费的所有权限怎么样?

sudo chown -R test_user:test_group /testdir
sudo chmod -R 777 /testdir

我的“test_user”连接也出现同样的 sftp 失败。

有点困惑,我将所有权放回原始案例,但现在具有更广泛的访问权限 -

sudo chown -R root:root /testdir
sudo chmod -R 775 /testdir

这仍然无法连接,记住之前与用户:rwx,组:rx,其他:rx root root成功了,我现在已经用户:rwx,组:rwx,其他:rx root root,范围更广,但现在 SFTP 失败了。

然后,为了确认,我用 - 收紧了访问权限

sudo chown -R root:root /testdir
sudo chmod -R 755 /testdir

我的 sftp/filezilla 'test_user' + pw 连接再次成功,显示 /testdir/dev 目录的内容,尽管仍然不允许写入权限。

我显然误解了所有权和访问权限,并且可能误解了 sftp 服务最终试图在这里执行的操作的机制。有人可以解释一下吗——

  1. 'chmod -R 640 /testdir' 和所有者 'test_user:test_group' (rw- r-- --- test_user test_group) 案例似乎是我应该需要的('test_user' 可以读写,并且 'test_group' 的其他成员可以阅读)-对吗? - 但是为什么这会导致 sftp 连接失败?
  2. 如果“d rwx rx rx root root”案例启用了成功的 sftp 连接,与用户“test_user”连接,那么为什么只拓宽'd rwx r 的权限wx rx root root' (775) 然后导致 sftp 连接失败?
  3. 允许我的“test_user”通过 SFTP 连接对这些“/testdir/...”目录进行读/写访问,而无法访问其他(非 root)用户的正确/最佳方法是什么?
  4. 是否有关于 sftp 服务器上整个 SFTP 连接/身份验证阶段的步骤和过程的顺序(可能在不同用户下运行)的更详细描述,可以更清晰、更直观地了解这里发生的情况?

答案1

ChrootDirectorysshd 选项的文档部分说:

在会话启动时,sshd(8) 检查路径名的所有组成部分是否为 root 拥有的目录,任何其他用户或组都无法写入这些目录。

换句话说,您的/testdir目录必须由 root 拥有,而没有组或其他的写权限。如果不满足这些条件,我相信sshd将会放弃会议。

这些限制仅适用于 chroot 目录及其父目录。您可以/testdir使用您喜欢的任何所有权和权限在其中创建文件和目录。

相关内容