Copy the ssh public key to multiple servers.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.