ubuntu配置SSH免密码登陆,提示Permission denied (publickey).

在配置Ubuntu EC2实例的SSH免密码登陆时遇到'Permission denied (publickey)'错误。尝试了多种解决方案,包括设置用户密码、修改sshd_config文件等,最终发现是因为实例中RSAAuthentication和PubkeyAuthentication未开启。将其设置为yes并重启实例后,成功解决了SSH公钥认证问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用EC2的ubuntu实例创建Hadoop集群时,配置ssh密钥出现问题:

ubuntu@ip-172-31-30-202:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.The key fingerprint is:c:9e:43:1:5c:97 

ubuntu@ip-172-31-30-202

The key's randomart image is:
+--[ RSA 2048]----+
|             .   |
|    .E    |
|     .. .     |
|    . .       |
|     .        |
|    ..       |
|  =          |
+-----------------+

ubuntu@ip-172-31-30-202:~$ cd .ssh
ubuntu@ip-172-31-30-202:~/.ssh$ ls
authorized_keys  id_rsa  id_rsa.pub
ubuntu@ip-172-31-30-202:~/.ssh$ ssh-copy-id ubuntu@ip-172-31-29-124
/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
Warning: Permanently added 'ip-172-31-29-124,172.31.29.124' (ECDSA) to the list of known hosts.
Permission denied (publickey).

在向其他节点复制公钥时总是提示:Permission denied (publickey).

找了很多种解决方案如下:

http://blog.chinaunix.net/uid-22556372-id-1773417.html

http://blog.itpub.net/25851087/viewspace-1262468/

但是都没解决我的问题,后来找到了原因,因为我的新的实例并没有在当前用户上创建登录密码,导致在识别用户时无密码作为权限标识,所以提示无权限,解决办法如下:

ubuntu@ip-172-31-31-26:~$ sudo passwd ubuntu
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

ubuntu@ip-172-31-31-26:~/.ssh$ ssh-copy-id ubuntu@ip-172-31-31-25
/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
ubuntu@ip-172-31-31-25's password: 
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'ubuntu@ip-172-31-31-25'"
and check to make sure that only the key(s) you wanted were added.
ubuntu@ip-172-31-31-26:~/.ssh$ ssh ubuntu@ip-172-31-31-25

即可解决这个问题,可能在种情况的原因有很多种,我的解决方式仅供参考。

-------------------------------------------------------------------------------------------------------------------------

通过上述办法仍有实例存在这个问题

ubuntu@ip-172-31-31-29:~/.ssh$ ssh-copy-id ubuntu@ip-172-31-31-30
/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
Permission denied (publickey).

之前修改过/etc/ssh/sshd_config文件中的配置,然后保存退出,在/etc/init.d/路径下使用service ssh restart命令重启ssh,但是仍不管用。

我的解决办法如下:

ubuntu@ip-172-31-31-29:/$ cd etc/ssh
ubuntu@ip-172-31-31-29:/etc/ssh$ sudo vim sshd_config

将配置文件中如下修改为yes

RSAAuthentication yes
PubkeyAuthentication yes

RhostsRSAAuthentication yes

PasswordAuthentication yes

保存退出,然后重启实例,即可正常ssh-copy-id  ubuntu@IPadress

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值