This article is almost a translation of Push SSH public keys to multiple host. Thank you.
A password is required for logging on to a remote machine through SSH. Many systems need to access the remote machine without a password. For example, when the hadoop master node accesses a subnode, in this case, you can use the ssh-copy-id command to copy the public key to a remote machine,
For example: ssh-copy-id-I hduser@192.168.7.70
However, if there are many machines, such as, or more, you may want to write a script and execute it in batches. However, there are two manual steps for executing the ssh-copy-id command,
First, when you see the following question, you must answer yes or no to each machine,
The authenticity of host 'myfirsthost .work.cainmanor.com (10.256.33.106) 'can't be established.
RSA key fingerprint is fc: 40: 7c: de: b8: ac: a2: f5: d4: 11: d0: 0e: b2: 77: 8a: 63.
Are you sure you want to continue connecting (yes/no )? Yes
The second is the password of each machine. When you ssh-copy-id to each machine, you need to enter the password to access this machine,
To solve the first problem, you can modify ~ /. Ssh/config file (created if no one exists). Add the following two lines to the file,
StrictHostKeyChecking no
UserKnownHostsFile =/dev/null
After completing all the SSH copy operations, remember to use the # key to comment out (it's okay if you don't comment out ...).
To solve the second problem, you can use the sshpass tool, which may need to be downloaded. On Centos, I need to manually download the rpm package for installation. Sshpass provides the required password for ssh. You can run the following command,
Sshpass-p 'Password' ssh-copy-id hduser @ host
In actual operations, the above command will be placed in a script file like the following, and the hosts file will be provided, and the hosts file will record all machines that need to copy the public key,
For X in 'cat hosts'
Do
Sshpass-p 'Password' ssh-copy-id hduser $ {X}
Done
Hosts files record host information. This mode is very common when executing ssh commands in batches.
After completing the SSH Public Key copy, you can select pssh to execute commands in batches.