Script to copy RSA key multiple servers

I recently needed to add my rsa public key to multiple servers where the .ssh directory didn’t already exist nor the .ssh/authorized_keys in addition to the correct permissions for the .ssh directory and authorized_keys file

I found this taken from the below post on how to install it on OS X:

I can’t find an official source for the ssh-copy-id. If you can, please comment a link for me.

Nevertheless, this should get you what you’re looking for:
wget -O /usr/bin/ssh-copy-id
sudo chmod +x /usr/bin/ssh-copy-id

Note: ssh-copy-id appends to authorized_keys file

I created this script to run in order to add to multiple servers:

SSH-COPY-ID=$(which ssh-copy-id)
# Array to add .ssh authorized keys to systems for access
Systems=(server1 server2 server3 server4 server 5 …)
for S in “${Systems[@]}”
#echo $S.$DOMAIN
$SSH-COPY-ID -i ~/.ssh/ username@$S.$DOMAIN

Note: You need to run this script from your home directory and if you have dsa keys instead of rsa keys, make sure to use the correct name.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s