linux 配置ssh免密登录

1,生成ssh 密钥对

ssh-keygen生成SSH密钥对,id_rsa.pub公钥用来加密,id_rsa私钥用来解密。
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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.
一路回车,可以查看生成的公私钥。
authorized_keys  id_rsa  id_rsa.pub

2,使用 ssh-copy-id 来复制本地公钥到目的Host的~/.ssh/authorized_keys中.

[root@ansible ansible]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@ansible-2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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@ansible-2's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@ansible-2'"
and check to make sure that only the key(s) you wanted were added.

3, 尝试直接登录成功

Last login: Sat Mar  6 03:15:12 2021 from IP
posted @ 2021-03-06 17:37  小白运维狗  阅读(100)  评论(0)    收藏  举报