ssh-copy-id 之后 ssh 仍然要求输入密码

8 ssh

[root@spectrumscale ~]# chmod 700 .ssh
[root@spectrumscale ~]# cd .ssh
[root@spectrumscale .ssh]# ssh-keygen -t rsa 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
05:63:ff:2a:82:fc:c9:31:87:fc:a1:61:dc:4e:5a:52 root@spectrumscale
The key's randomart image is:
+--[ RSA 2048]----+
|        +        |
|       . +       |
|          o      |
|         . .     |
|        E   .    |
|   . + +   .     |
|    o @ B .      |
|     + / o       |
|      * o        |
+-----------------+
[root@spectrumscale .ssh]#  ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.215
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.215's password: 
Permission denied, please try again.
root@192.168.1.215's password: 

Number of key(s) added: 1
Run Code Online (Sandbox Code Playgroud)

现在尝试使用以下命令登录计算机 ssh 'root@192.168.1.215'"并检查以确保仅添加了您想要的密钥。

[root@spectrumscale .ssh]# ssh 192.168.1.215
root@192.168.1.215's password: 
Last failed login: Tue Nov 12 17:47:37 IST 2019 from 192.168.1.203 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue Nov 12 14:44:01 2019 from localhost
Run Code Online (Sandbox Code Playgroud)

ano*_*knr 9

您必须诊断此问题的根本原因。您可以通过在要登录的系统上sshd使用命令阅读相关日志来找到此信息。journalctl

读取日志:

journalctl -t sshd
Run Code Online (Sandbox Code Playgroud)

如果日志显示类似“身份验证被拒绝:目录所有权或模式错误”的内容,则这是由于目录所有权模式/home/<your_user>/.ssh错误造成的。

通过修复权限

chmod go-w /home/<your_user>
chmod 700 /home/<your_user>/.ssh
chmod 600 /home/<your_user>/.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)

还要确保在 sshd 配置文件中 /etc/ssh/sshd_config,确保PubkeyAuthentication没有注释和设置yes

在/etc/ssh/sshd_config内确保这是一行,

PubkeyAuthentication yes
Run Code Online (Sandbox Code Playgroud)

在sshd 配置文件中编辑后可能需要重新启动sshd 服务。

sudo service sshd restart 
Run Code Online (Sandbox Code Playgroud)

这对我有用,希望有帮助!