每当我将用户添加到“sftp”组以便使用 chroot 时,我就无法再通过 SSH 连接,并且它显示:“无法 chdir 到主目录 /home/sftp_user:没有此文件或目录“ 和 ”此服务仅允许 sftp 连接“。如果我设置所有者/home/sftp_用户到sftp_用户然后我可以登录,但 chroot 不再起作用。我该如何修复这个问题,以便能够使用 SSH 和 chroot(仅在 SFTP 上,而不是 SSH)?
# Package generated configuration file
# See the sshd_config(5) manpage for details_
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding no
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp internal-sftp
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin yes".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
Match Group sftp
ChrootDirectory %h
ForceCommand internal-sftp
AllowTcpForwarding no
答案1
实际上,我认为你所问的问题是不可能的。
来自 sshd_config 手册:
ChrootDirectory
Specifies the pathname of a directory to chroot(2) to after
authentication. At session startup sshd(8) checks that all
components of the pathname are root-owned directories which are
not writable by any other user or group. After the chroot,
sshd(8) changes the working directory to the user's home
directory.
因此,要使 chroot 正常工作,目录必须由 root 拥有。这就是为什么当您更改 /home/sftp_user 的所有者时它不起作用的原因。
另一方面,当所有者正确时,chroot 可以工作,但您尝试访问 chroot 之外的路径(您的主目录)。如果您希望此操作有效,则需要在 chroot 内创建主文件夹。因此,如果您 chroot 到 /home/sftp_user,则需要创建 /home/sftp_user/home/sftp_user。
连接时,sshd 无法判断您要执行的是“ssh”还是“sftp”。因此,您需要使用其他方法来区分。也许是 sftp 特定帐户或其他东西。