无法在 SSH 中使用 RSA 密钥:服务器拒绝我们的密钥

无法在 SSH 中使用 RSA 密钥:服务器拒绝我们的密钥

我正在尝试使用 PuTTY 生成的 RSA 公钥/私钥从 Windows 计算机登录到两台 GNU/Linux 计算机,但对其中一台计算机有效,对 Ubuntu 计算机无效。密钥是在 Windows 7 台式电脑上生成的。从它我可以登录到运行 Angstrom 发行版的 Beaglebone Black。我将密钥复制到 Windows 7 笔记本电脑上,我也成功从它登录到 BBB,但无法登录到 Ubuntu 计算机。我在 PuTTY 终端上收到以下消息:

Using username "user".
Server refused our key
Using keyboard-interactive authentication.
Password:

我可以通过输入密码来完成登录,但我真的希望密钥能够正常工作,这样我就可以消除通过互联网进行密码登录的麻烦。两个服务器的用户名不同,但我认为这没问题,对吗?

我使用的是安装了 openssh-server 的 Ubuntu 14.04 LTS。我从 PuTTYgen 复制了公钥并将其粘贴到 ~/.ssh/known_hosts 中。密钥只有一行,开头如下:ssh-rsa AAAAB...

~/.ssh 目录的权限设置为 700,~/.ssh/known_hosts 设置为 600。

当我发现这个帖子在此板上,但这些解决方案都未能解决问题。

有一次我意外删除了 /etc/ssh/ 中的主机密钥,但我卸载了 openssh-server,然后重新安装,这些密钥又恢复了。好吧,至少看起来我把它们都找回来了。

我打开一个 PuTTY 终端并发出tail -f /var/log/auth.log,然后尝试在第二个终端上登录。没有任何提示出现。第一条消息确认密码登录成功,但没有关于密钥的任何信息。

我一直在修改配置文件 /etc/ssh/sshd_config,试图找到正确的设置组合,每次都用 重新启动守护进程service ssh restart。下面是该文件的当前状态。我想我可能遗漏了其中的一些东西,但我没有主意了。

# 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 without-password
StrictModes no

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 yes

# 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

答案1

正如steeldriver指出的那样,RSA密钥进入授权密钥文件,不是 已知主机

我从已知主机,创建并粘贴到授权密钥. chmod 600 authorized_keys,,sudo service ssh restart并且服务器已启动并正在运行。

谢谢steeldriver的回答。

答案2

如果您在使用较新的 Ubuntu 版本 Openssh 8.7 时遇到此问题;并且您的 /var/log/auth.log 尾部显示:键入 ssh-rsa 而不是 PubkeyAcceptedAlgorithms,那么您很可能需要在 /etc/ssh/sshd_config 中添加:

HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
PubkeyAuthentication yes
PubkeyAcceptedKeyTypes=+ssh-rsa

在我的案例中,iOS 上的 Shelly 的行为现在已得到解决。奇怪的是 ssh keygen 可以生成它不接受的密钥。这里又解决了许多困惑https://bbs.archlinux.org/viewtopic.php?id=270005。 多谢你们。

相关内容