无法通过密码验证连接到基本的 ubuntu ssh 服务器-权限被拒绝公钥

无法通过密码验证连接到基本的 ubuntu ssh 服务器-权限被拒绝公钥

标题说明了一切。我严格遵循了以下指南: https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-20-04/ 但最后我还是无法通过 ssh 连接到 ubuntu - 我被拒绝权限(公钥)。所以我进入我的 sshd_config 并修改了以下内容:PermitRootLogin yes PubkeyAuthentication no PasswordAuthentication yes PermitEmptyPasswords no

其余一切都保留了 ssh 服务器的默认设置。但是,我仍然无法通过 ssh 登录,因为我收到 Permission denied (publickey) 信息。考虑到我已经明确禁用了公钥身份验证,我觉得这很奇怪。我甚至尝试使用以下 ssh 命令:ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no myuser@myserver

但是我仍然得到相同的结果。我到底错过了什么?这似乎应该有效。这是在 ubuntu 20.04 中

答案1

我在尝试从 mint 20.04 连接到 ubuntu 20.04 服务器时遇到了这个问题。sshd_config服务器的设置与你描述的完全一致。

如果您尝试,ssh -v user@hostname您可能会看到它尝试一堆身份验证密钥,即使它们不在中,config并且您没有-i在命令行中使用。它会不断尝试找到的不同密钥,直到服务器感到厌烦并关闭连接。

在这种情况下,请尝试禁用客户(暂时,在命令行上一次性执行):

ssh user@servername -o PubkeyAuthentication=no

执行此操作后,系统会提示输入密码并成功登录。我还可以使用sshpass -pMYPASSWORD ssh user@servername

示例输出:

$ sshpass -p<password> ssh -v  <user>@<servername> 
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/<user>/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to <servername> [<servername>] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/<user>/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4
debug1: match: OpenSSH_8.2p1 Ubuntu-4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to <servername>:22 as '<user>'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:r2Ra44NtKxs4WPYKroLMETpd2ebGxUwiKuJdCLqX3fM
debug1: Host '<servername>' is known and matches the ECDSA host key.
debug1: Found key in /home/<user>/.ssh/known_hosts:33
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:Yxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0 /home/<user>/.ssh/<privatekey>
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxk <user>@MacBook-Pro.fritz.box
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxg [email protected]
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:qxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxg right@<user>-<computername>-Mint
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:zxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxM right@right-<computername>
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc root@unite
Received disconnect from <servername> port 22:2: Too many authentication failures
Disconnected from <servername> port 22

答案2

如果我理解正确的话,你想使用以下命令设置 ssh 访问密码(与密钥对相反)。

除了您提到的设置之外,其他设置也可以发挥作用/etc/ssh/sshd_config

您提到您已经设置了这些:

PermitRootLogin yes
PubkeyAuthentication no
PasswordAuthentication yes
PermitEmptyPasswords no

你也需要设置以下之一:

KbdInteractiveAuthentication yes
  # recent OpenSSH versions

ChallengeResponseAuthentication yes
  # less recent OpenSSH versions

此外,如果可选设置AuthenticationMethods处于活动状态,您可能应该将其注释掉 - 它的参数是身份验证方法的列表,并且必须通过所有方法才能允许登录。

重新开始sshd

保存后/etc/ssh/sshd_config,重新启动sshd(SSH服务器守护进程):

sudo systemctl disable --now ssh
sudo systemctl enable --now ssh

故障排除

要查看尝试、接受和拒绝了哪些身份验证方法,请尝试连接到主机以-v获取详细输出:

<client>$ ssh -v host

从输出中,您可以了解尝试了哪些身份验证方法以及主机的响应。例如,查找以下行:

debug1: Next authentication method: publickey

debug1: Next authentication method: keyboard-interactive

相关内容