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

问题描述 投票:0回答:3
[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 [email protected]
/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
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 

Number of key(s) added: 1

现在尝试使用以下命令登录计算机:

ssh '[email protected]'"
并检查以确保仅添加了您想要的密钥。

[root@spectrumscale .ssh]# ssh 192.168.1.215
[email protected]'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
ssh
3个回答
9
投票

您必须诊断此问题的根本原因。您可以通过在要登录的系统上使用

sshd
命令读取相关日志
journalctl
来找到此信息。

阅读日志:

journalctl -t sshd

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

通过

修复权限

chmod go-w /home/<your_user> chmod 700 /home/<your_user>/.ssh chmod 600 /home/<your_user>/.ssh/authorized_keys
还要确保在 sshd 配置文件中

/etc/ssh/sshd_config

,确保
PubkeyAuthentication
没有被注释并设置
yes

/etc/ssh/sshd_config 中确保这些是一行,

PubkeyAuthentication yes

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

sudo service sshd restart
这对我有用,希望有帮助!


1
投票
如果您已验证所有权限均正确,但仍被提示输入密码,请确保将以下行添加到您想要无需密码登录的系统上的文件

/etc/ssh/sshd_config 。这将允许 SSH 守护进程接受 ssh-rsa 密钥类型

pubkeyacceptedkeytypes ssh-rsa
完成此操作后,只需运行命令

service sshd restart

,无密码登录现在就可以工作了


0
投票
与所讨论的情况没有直接关系,但就我而言,强制

ssh-copy-id

 使用 
-f
 选项(在 
Windows 上)复制带有 BOM
.pub
文件的内容导致 ~/.ssh/authorized_keys 不被正确读取并且 ssh 需要密码。这种情况的解决办法是通过
nano ~/.ssh/authorized_keys
等删除不需要的字符
    

© www.soinside.com 2019 - 2024. All rights reserved.