SSH 公钥身份验证 - 仅在物理登录后有效

SSH 公钥身份验证 - 仅在物理登录后有效

我正在尝试使用公钥身份验证通过 SSH 连接到我的 ubuntu 服务器。由于某些原因,我收到“权限被拒绝(公钥)”。在客户端上,每当我执行

ssh -i ~/.ssh/id_rsa <username>@<ip> -p <port>

我的服务器上的 auth.log 有以下输出:

sshd[1425]: Connection closed by <client-ip> [preauth]

但是,一旦我使用相同的用户名物理登录到我的服务器上,来自我的客户端的以下 ssh 连接就会成功。但一旦我物理注销,客户端的下一个 ssh 会话就会失败。

/etc/ssh/sshd_config

# What ports, IPs and protocols we listen for
Port <port>
# 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

PermitEmptyPasswords no
ChallengeResponseAuthentication no

PasswordAuthentication no

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

UsePAM no

ClientAliveInterval 30
ClientAliveCountMax 99999    

有任何线索说明为什么会发生这种情况吗?或者您对安全方面有什么建议吗?谢谢你!

答案1

正如评论中提到的,您正在使用加密的主目录,并且可能使用pam_mount安装它。
pam_mount 使用登录期间获取的密码挂载分区。由于您尝试通过 ssh 公钥登录,因此存在两个问题:

  1. 公钥身份验证期间没有发送密码,因此无法使用它挂载您的主目录。
  2. 使用 pam_mount 时,您的主目录已挂载登录,但sshd需要获取您的authorized_keys文件登录,因此它没有安装。

这些问题中的任何一个都足以阻止其工作。

唯一的解决方案是从主目录中获取公钥。这其实很简单。

首先将authorized_keys文件复制出主目录:

cp -a /home/$USER/.ssh/authorized_keys /home/$USER-authorized_keys

然后sshd通过添加以下内容来指示使用该文件/etc/ssh/sshd_config(如果存在,则替换现有条目):

AuthorizedKeysFile .ssh/authorized_keys /home/%u-authorized_keys

并弹跳sshd

但请注意,这不会挂载您的主目录。您的主目录仍然需要您的密码才能解密。根据您的 pam_mount 配置方式,它可能会提示您输入密码,或者可能只是将您带入 shell,并卸载您的 home。

相关内容