ssh 公钥登录 linux 服务器总是提示要输入密码

2020-02-12 19:09发布

使用 ssh 公钥登录一台 ubuntu 服务器提示要输入密码,公钥已经在服务器的 ~/.ssh/authorized_keys 文件中添加,请问如何解决这个问题?

标签: linux
1条回答
Bombasti
2楼-- · 2020-02-12 20:10

搞定了!解决过程如下。

1)停止 sshd 服务。

service ssh stop

2)手动 debug 模式运行 sshd 。

/usr/sbin/sshd -d -p 22

3)运行失败,报错:

Could not load host key: /etc/ssh/ssh_host_ed25519_key
Missing privilege separation directory: /run/sshd

4)添加 -h /etc/ssh/ssh_host_rsa_key 参数消除第1个报错。

5)运行命令 mkdir systemd-tmpfiles --create 消除第2个报错。

6)用下面的命令成功运行 ssh 。

/usr/sbin/sshd -d -p 22 -h /etc/ssh/ssh_host_rsa_key

7)客户端 ssh 登录,手动 debug 模式运行的 sshd 报错。

debug1: list_hostkey_types:  [preauth]
No supported key exchange algorithms [preauth]
debug1: do_cleanup [preauth]

8)从上面的报错信息找到线索,通过下面的操作解决了问题。

  • 删除 .ssh 目录
rm -rf ~/.ssh
  • 重新生成 ssh key
ssh-keygen -t rsa
  • 重新添加客户端 ssh 公钥到 authorized_keys
vi ~/.ssh/authorized_keys
  • 启动 sshd
service ssh start
查看更多
登录 后发表回答