Cain Manor

Your Guide To All Things Cain™

Push SSH public keys to multiple host

I’m start­ing a new job where I need to have my SSH keys pushed to hun­dreds of Red Hat servers. The spe­cial sauce is a com­mand called ssh-copy-id. How­ever, using this com­mand requires you answer­ing a (yes/no) ques­tion, then shortly there­after enter your pass­word. Painful. Here’s how push your keys with­out the pain.

The first prob­lem is hav­ing to answer (yes/no) for each server. Nor­mally you see this…

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

To stop this prompt, we need to edit your ~/.ssh/config file. Add these two lines

StrictHostKeyChecking no
UserKnownHostsFile=/dev/null

Set­ting your User­Known­Hosts­File should only be a tem­po­rary fix. After you’ve pushed your keys, you should com­ment out both of those settings.

Your pass­word is the next prob­lem. We can solve that with ssh­pass. ssh­pass takes your pass­word and passes it on when ssh ask for it. There are three ways to do it, all of them inse­cure. Read the man page and decide which of those you want to use. For my pur­poses I just put it on the com­mand line — I’m on my per­sonal machine with no other users, and only I know the pass­words to the box. Don’t do this on a shared server.

Here is an exam­ple of how to push your pub­lic key one. Try it on a new server to make sure you get the results you expect.

sshpass -p 'MY_PASSWORD' ssh-copy-id gregc@new_host_with_no_keys

Now that we’ve got the prompts turned off, we’ll wrap a script around this. How you get the list of appro­pri­ate host­names or IP’s is your business.

for X in `cat my_host_that_need_keys`
do
sshpass -p 'MY_PASSWORD' ssh-copy-id gregc@${X}
done

Happy Com­put­ing!!

Comments are closed.