![‘ssh localhost’ 显示‘权限被拒绝(公钥)’。](https://linux22.com/image/1086134/%E2%80%98ssh%20localhost%E2%80%99%20%E6%98%BE%E7%A4%BA%E2%80%98%E6%9D%83%E9%99%90%E8%A2%AB%E6%8B%92%E7%BB%9D%EF%BC%88%E5%85%AC%E9%92%A5%EF%BC%89%E2%80%99%E3%80%82.png)
我将提到我尝试过的许多方法,并提供一些诊断。我将不胜感激任何有关如何调试此问题的线索:
me@myubuntu:~/.ssh$ ssh localhost
Permission denied (publickey).
之前我跑过
sudo ufw allow ssh/tcp
在我的路由器上设置端口 22 的端口转发,并验证该端口已打开这里(但可能没有必要——当我 ssh 到自己时,ssh 是否会到达路由器?)。
在我的 home/.ssh 目录中,我执行了该操作cat id_rsa.pub >> authorized_keys
并使用 设置了权限chmod 600 authorized_keys
。
运行ssh -v localhost
会产生很多输出,这里是我认为问题发生的最后几行:
debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /home/me/.ssh/known_hosts:3
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/me/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: me@myubuntu
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/me/.ssh/id_dsa
debug1: Trying private key: /home/me/.ssh/id_ecdsa
debug1: No more authentication methods to try.
Permission denied (publickey).
我成功通过 ssh 连接到我的 bitbucket 存储库,所以我认为这意味着我的 id_rsa 密钥集有效且具有正确的权限。
更新 1:
> ~/.ssh$ ls -l ~/.ssh/authorized_keys
-rw------- 1 me me 747 Oct 25 08:27 /home/me/.ssh/authorized_keys
(看起来不错)
更新 2:
~/.ssh$ ls -ld ~/.ssh
drwx------ 2 me me 4096 Oct 25 08:27 /home/me/.ssh
(看起来不错)
更新 3:/var/log/auth.log
我运行后最后几行ssh localhost
:
Oct 25 09:49:47 me-myubuntu sshd[16442]: Connection from 127.0.0.1 port 36944
Oct 25 09:49:47 me-myubuntu sshd[16442]: User me from localhost not allowed because not listed in AllowUsers
Oct 25 09:49:47 me-myubuntu sshd[16442]: input_userauth_request: invalid user me [preauth]
Oct 25 09:49:47 me-myubuntu sshd[16442]: Connection closed by 127.0.0.1 [preauth]
看看这个!我之前弄乱了 sshd_config 文件,忘记正确指定我的 AllowUsers 选项。
解决方案:在行localhost
中包含AllowUsers
sudo gedit /etc/ssh/sshd_config
答案1
请检查/var/log/auth.log
连接过程中可能出现的错误。此行
10 月 25 日 09:49:47 me-myubuntu sshd[16442]: 不允许使用 localhost 的用户 me,因为未在 AllowUsers 中列出
指出了问题所在。