1. 程式人生 > 其它 >How to setup SSH password-free login

How to setup SSH password-free login

192.168.13.4 - ansible
192.168.13.5 - dockertest1

Generate key-pair on Ansible

private key - /root/.ssh/id_rsa
public key - /root/.ssh/id_rsa.pub

[root@ansible ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):                # Where do you want to save the private key - /root/.ssh
/id_rsa by default Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): # Password hint - Give you a hint when you can't remember your password. You can set it to null 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 public key will be saved in the same directory as the private key The key fingerprint is: SHA256:hADTUrrgZCuCT4RgyCfnYsTv6hI/G3lTw2t5a9qv2mQ root@ansible The key's randomart image is: +---[RSA 2048]----+ |=.o+o | |+*.=.. . | |+oO. . . | |==.+ . . | |=o= + S | |+o o . + | | o= o + E | |..oo o *.. | |o..o o+=o. | +----[SHA256]-----+ [root@ansible
~]#

Send Ansible's public key to dockertest1

It will be added to /root/.ssh/authorized_keys in dockertest1

[root@ansible ~]# ssh-copy-id root@192.168.13.5
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"                                       # The public key which will be sent is /root/.ssh/id_rsa.pub
The authenticity of host '192.168.13.5 (192.168.13.5)' can't be established.                                                # I have to know you(known_hosts), then I can allow myself to connect with you
ECDSA key fingerprint is SHA256:HrffjmCH2fl15x6rn2Y2y/9cwc8mwm84RN/KQCm6bns.
ECDSA key fingerprint is MD5:f5:22:21:04:60:39:84:0b:d7:21:3a:b4:4f:e5:3f:01.
Are you sure you want to continue connecting (yes/no)? yes                                                                  # Do you still want to connect? If yes, I will add you to my known_hosts
/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.13.5's password: 

Number of key(s) added: 1                                                                                                   # The public key has been sent to dockertest1 and added to its /root/.ssh/authorized_keys

Now try logging into the machine, with:   "ssh '[email protected]'"                                                         # Now you can try it
and check to make sure that only the key(s) you wanted were added.

[root@ansible ~]# 

Verify

[root@ansible ~]# ssh 192.168.13.5
Last login: Fri Mar 11 11:47:55 2022 from 192.168.13.1
[root@dockertest1 ~]# exit
logout
Connection to 192.168.13.5 closed.
[root@ansible ~]#