SSH 登陆失败 Permission denied (publickey) 排错

SSH 登陆失败 Permission denied (publickey) 排错

1. 问题描述

  • 在阿里云上有一台Ubuntu 18.04 版本的服务器,今天使用 ssh 登陆时报错 “Permission denied (publickey)”;

  • 登陆的客户端机器是新安装的 Ubuntu 22.04 机器;

  • 为了安全,登陆方式一直使用的本地私钥登陆;

    后续说明中,使用的假设条件

    • 客户端机器上的用户是 foo
    • 服务器端的帐户是 bar
    • 服务器为 example.com
    • 本地私钥文件路径为 ~/.ssh/id_rsa
    $ ssh -i .ssh/id_rsa bar@example.com
    foo@example.com: Permission denied (publickey).
    

2. 问题分析

  • 从提示看,应该是权限问题,这个服务器长时间没动过,还以为被黑了,密钥被替换了?

  • 通过阿里云的网页版控制台登陆,使用了之前的私钥,可以顺利登陆,看来不是密钥内容的问题。

  • 因为ssh对密钥文件和存储密钥的目录有很强权限要求,所以首先设置客户端机器中相关文件系统的权限。示例中假设使用的私钥文件名字是 id_rsa

    $ chmod 700 /home/foo
    $ chmod 700 /home/foo/.ssh
    $ chmod 600 /home/foo/.ssh/id_rsa
    

    下表罗列了 ssh 所涉及的相关文件和目录的权限要求。如果有相关的文件,按要求设置。

    文件/文件夹权限要求
    ~/.ssh 目录700 (drwx------)
    public keys644 (-rw-r–r–)
    private keys600 (-rw-------)
    authorized_keys600 (-rw-------)
    known_hosts600 (-rw-------)
    config600 (-rw-------)
  • 权限检查和设置完成后,再次登陆,仍然报相同错误! 看来不是文件系统权限错误导致的。

  • 通过添加 -vvv 参数启用 ssh 登陆的详细调试信息,分析错误日志

    $ ssh -vvv -i .ssh/id_rsa bar@example.com 
    
    OpenSSH_8.9p1 Ubuntu-3, OpenSSL 3.0.2 15 Mar 2022
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
    debug1: /etc/ssh/ssh_config line 21: Applying options for *
    ......
    省略....
    ......
    debug3: authmethod_is_enabled publickey
    debug1: Next authentication method: publickey
    debug1: Offering public key: .ssh/id_rsa RSA SHA256:CU98bg6gzau/W/nrC+SfwnSpQdozsObTdbOh/ac4M9I explicit agent
    debug1: send_pubkey_test: no mutual signature algorithm
    debug1: Offering public key: /home/foo/.ssh/id_rsa RSA SHA256:1V9z05gv0vCLh5QtTrU9eE5Cjlgo9MKqIyva+MQZDTo agent
    debug1: send_pubkey_test: no mutual signature algorithm
    debug2: we did not send a packet, disable method
    debug1: No more authentication methods to try.
    bar@example.com: Permission denied (publickey)
    

    从详细日志分析,可见问题出在报错 “send_pubkey_test: no mutual signature algorithm” 的地方。

3. 解决方法

在搜索引擎里搜索错误信息,发现错误的原因是由于最新的 Ubuntu 22.04 操作系统中的 ssh 服务默认不支持 RSA 前面算法,而服务器端的 Ubuntu 18.04 使用的还是老版本 ssh。两者没用共同匹配的签名算法,所以登陆失败了。不过这个权限拒绝的报错 “Permission denied” 信息不太准确,容易误导用户。

下图是文章从 SSH-RSA key rejected with message “no mutual signature algorithm” 文章中的截图
在这里插入图片描述
根据文章提示,需要在客户端高版本的 ssh 的配置文件中增加 PubkeyAcceptedKeyTypes +ssh-rsa 选项增加对 rsa 算法的支持。

PS: 注意要修改的文件是客户端的 ssh_config 配置文件,不是大家经常修改的 sshd 服务器端的配置文件。


$ echo "PubkeyAcceptedKeyTypes +ssh-rsa" | sudo  tee -a /etc/ssh/ssh_config

$ ssh -i .ssh/id_rsa bar@example.com

最终解决了问题,登陆成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值