无法让无密码(提供 SSH)SFTP 工作

Sho*_*ibi 4 linux debian ssh sftp

我已经 chroot 了 sftp 设置,如下所示。

# 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
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes
AllowGroups admins clients

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 yes
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 /usr/lib/openssh/sftp-server

# 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 without-password".
# 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



Subsystem sftp internal-sftp

Match group clients
    ChrootDirectory /var/chroot-home
    X11Forwarding no
    AllowTcpForwarding no
    ForceCommand internal-sftp
Run Code Online (Sandbox Code Playgroud)

虚拟用户

root:~# tail -n1 /etc/passwd
david:x:1000:1001::/david:/bin/sh
Run Code Online (Sandbox Code Playgroud)

现在在这种情况下 david 可以使用 say filezilla 客户端 sftp 并且他被 chroot 到 /var/chroot-home/david/。但是,如果我要设置无密码身份验证怎么办?我试过将他的密钥粘贴到 /var/chroot-home/david/.ssh/authorized_keys 但没有用,我尝试像 david 一样 ssh'ing 到盒子,它只是在我提供后停止在“debug1:Sending env LC_CTYPE = C”它的密码并且auth.log中没有显示任何内容,可能是因为它找不到homedir。如果我以 root 身份执行“su - david”,我会看到“没有目录,使用 HOME=/ 登录”,这是有道理的。符号链接也无济于事。

我也试过:

Match group clients
        ChrootDirectory /var/chroot-home/%u
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp
Run Code Online (Sandbox Code Playgroud)

虚拟用户

root:~# tail -n1 /etc/passwd
david:x:1000:1001::/var/chroot-home/david:/bin/sh
Run Code Online (Sandbox Code Playgroud)

这样,如果我不将 /var/chroot-home/david 更改为 root:root sshd 会抱怨所有权或权限模式不佳,如果我这样做,david 在使用 sftp 时无法再直接在他的家中上传/删除任何内容档案馆。

小智 6

首先,主目录在 /etc/passwd应该反映未 chroot 的路径,否则您通常会遇到问题。在这种情况下,sshd在 chroot 之前检查授权密钥,因此它需要使用未 chroot 的路径找到它们。这就是为什么您的第一次尝试不起作用的原因。

要注意的第二件事:在您的第一个设置下,当 david 登录时,他开始在/var/chroot-home/david,但实际上他已被 chroot 到/var/chroot-home,这意味着如果他输入,cd ..他可以看到所有其他主目录(尽管不是它们的内容,如果权限正确)。这对您来说可能是也可能不是问题,但要注意这是一件好事。


如果以上对你没问题,你可以使用你的第一个 sshd_config,/var/chroot-home/davidpasswd文件中设置 david 的主目录,并添加以下符号链接,以便 david 仍然在他的主目录中启动:

cd /var/chroot-home
mkdir var
ln -s .. var/chroot-home
Run Code Online (Sandbox Code Playgroud)

该符号链接将确保您可以使用相同的路径访问主目录,无论您是否在 chroot 中。


但是,如果您不希望客户端看到彼此的主目录的名称,则需要 chroot 进入主目录本身,如您的第二个解决方案。但正如你所见,sshd不喜欢那样(因为各种微妙的原因,授予用户对文件系统根目录的写访问权限是危险的)。可悲的是,你在这里大多不走运。一个(笨拙的)解决方案是files/在每个主目录中创建一个子目录,并为客户端提供对该目录的写访问权限。

另一个选择可能是无论如何都要 chroot 到 /var/chroot-home,并以不同的方式命名主目录,例如使用用户 ID 号而不是名称。