ssh publickey permission denied 错误的解决方法

openssh publickey 登录的方式突然不好用了,检查 grep sshd /var/log/auth.log

1
Aug 30 16:58:33 pm-65c50001 sshd[9118]: Authentication refused: bad ownership or modes for directory /root

执行下面的命令后,可以成功登录了。

1
2
3
4
chown root:root /root
chown root:root /root/.ssh
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys

/etc/ssh/sshd_config 文件中最关键的配置项

1
PasswordAuthentication yes

我以前也遇到过这个问题,都是 file permession 设置错误,这次是 directory ownership 设置错误。排查此类错误,最靠谱的方法还是看日志,我折腾了 1 个小时。

Claude 3.5 Sonnet 没有回答出正确答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fix error `ssh publickey  Permission denied`


To fix the "ssh publickey Permission denied" error, we need to troubleshoot your SSH key configuration. Here are some steps to resolve this issue:

Check if your SSH key is added to the SSH agent:

ssh-add -l
If your key isn't listed, add it:

ssh-add ~/.ssh/your_private_key
Verify that your public key is properly added to the remote server's authorized_keys file. On the remote server, check:

cat ~/.ssh/authorized_keys
Your public key should be listed here.
Ensure correct permissions on SSH files:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Try connecting with verbose output to get more information:

ssh -vv user@host

Make sure you're using the correct username and hostname.
Check if the remote server allows public key authentication in its SSH configuration.

ssh publickey permission denied 错误的解决方法
https://usmacd.com/cn/ssh-publickey-permission-denied/
作者
henices
发布于
2024年9月5日
许可协议