我尝试为专门为 ssh 设置的用户帐户设置 chroot jail。
我设置了它,昨天它似乎工作正常,但今天它却不工作了。这似乎不是 SSH 问题,因为我可以很好地使用其他用户帐户进行 SSH 连接。以下是详细的 ssh 以及我收到的错误
OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 20: Applying options for *
debug1: /etc/ssh/ssh_config line 102: Applying options for *
debug1: Connecting to 130.191.240.69 [130.191.240.69] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/client/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.2
debug1: match: OpenSSH_5.2 pat OpenSSH_5* compat 0x0c000000
debug1: Authenticating to 130.191.240.69:22 as 'jaileduser'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr [email protected] none
debug1: kex: client->server aes128-ctr [email protected] none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:I7TPrpH1RDXr2pnfVc8xpjHiZ9okuzsIJkAm4dBOHHI
debug1: Host '130.191.240.69' is known and matches the RSA host key.
debug1: Found key in /Users/client/.ssh/known_hosts:2
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/client/.ssh/id_rsa
debug1: Trying private key: /Users/client/.ssh/id_dsa
debug1: Trying private key: /Users/client/.ssh/id_ecdsa
debug1: Trying private key: /Users/client/.ssh/id_ed25519
debug1: Next authentication method: keyboard-interactive
Password:
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to 130.191.240.69 ([130.191.240.69]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: channel 0: free: client-session, nchannels 1
Connection to 130.191.240.69 closed by remote host.
Connection to 130.191.240.69 closed.
Transferred: sent 2840, received 2344 bytes, in 0.5 seconds
Bytes per second: sent 6236.2, received 5147.1
debug1: Exit status -1
据我所知,我能够连接,然后立即关闭连接。我怀疑这可能是 chroot jail 中缺少了某些东西,但我还没能弄清楚。任何帮助都将不胜感激
注释掉 ChrootDirectory 命令将让用户松开并允许连接,但权限与链接的修复一致,因此我认为这不是问题所在。我的输出中也没有该错误。
我认为我还应该添加 /usr/sbin/sshd -d 的输出,希望能添加更多有用的信息
/usr/sbin/sshd -d
debug1: sshd version OpenSSH_5.2p1
Could not load host key: /etc/ssh_host_rsa_key
Could not load host key: /etc/ssh_host_dsa_key
debug1: setgroups() failed: Operation not permitted
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-d'
debug1: Bind to port 22 on 0.0.0.0.
Bind to port 22 on 0.0.0.0 failed: Permission denied.
debug1: Bind to port 22 on ::.
Bind to port 22 on :: failed: Permission denied.
Cannot bind any address.
答案1
Chrooting SSH 连接会禁锢一切,包括您尝试启动的 shell。因此,首先,/bin/bash(或 /bin/zsh 或您喜欢的任何 shell)需要存在。其次,运行 shell 所需的所有库也必须存在。
要提供所有需要的文件,您需要将一些绑定挂载添加到 chroot 目录。具体哪些可能因发行版而异,但通常至少包括:
- /垃圾桶
- /lib (或 /lib64,也可能两者兼有)
- /usr/lib (/usr/lib64)
您可能还想提供 /usr/bin。
注意:虽然您需要 /usr/lib 和可能的 /usr/bin,但通常不需要 /usr 本身。只需创建一个空目录并将所需的两个子目录放入其中即可。
要为 /bin 创建绑定挂载,请创建文件夹 /chrootdir/bin(将 /chrootdir 替换为您的实际位置),然后使用
mount -o bind /bin /chrootdir/bin
对任何其他需要的目录也执行此操作。
附注:您的 sshd 调试尝试失败,因为守护进程无法绑定到端口 22。您可以从消息中看到这一点。我假设您没有使用 root 权限启动它。
答案2
因此,我发现解决我的问题的方法是一个相当愚蠢的疏忽:
sudo chmod 755 /Users/jaileduser/jail/
我昨天设置了这些权限,但不知为何它们被修改了,我不清楚这是怎么发生的,但无论如何我让 ssh jaileduser 工作了。
非常感谢那些试图提供帮助的人