I am trying to copy .ssh/id_rsa.pub from our central server to multiple servers. I have the following script which I usually use to push changes to the different servers.


for ip in $(<IPs); do
    # Tell the remote server to start bash, but since its
    # standard input is not a TTY it will start bash in
    # noninteractive mode.
    ssh -q "$ip" bash <<-'EOF'



But in this case, I need to cat the public key on the local server and then add that to multiple servers. Is there a way by using the above here document script to execute the following.

cat .ssh/id_rsa.pub |ssh tony@ 'cat > .ssh/authorized_keys'
  • why you need to cat on local since you're copied from one central location to remotes ? – klerk Jul 17 '14 at 21:43
  • I need to add central server's public key hence local server. Sorry for the confusion. – user67186 Jul 17 '14 at 21:48

With this simple loop you can automate it and spread to all remote servers.

for ip in `cat /home/list_of_servers`; do
    ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
  • Hi, I accept your anwser and it worked fine. Thanks – user67186 Jul 17 '14 at 22:16
  • +1. I'm a big fan of a very easy scripting style that get the job done! – Laith Leo Alobaidy Dec 24 '18 at 19:12

Here is my simple script to copy ssh-keygen to multiple servers without asking password everytime.

for server in `cat server.txt`;  
    sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server  

The accepted answer won't work if one needs to put someone else's public key to multiple machines. So, I've come up with the following solution:

cat add-vassal-tc-agents.sh

set -x # enable bash debug mode
if [ -s vassal-public-key.pub ]; then # if file exists and not empty
    for ip in `cat tc-agents-list.txt`; do # for each line from the file
        # add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
        sed -e '$s/$/\n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
    echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"

This script adds the new key to the users on the list of machines, provided that the environment it is run on has the access.

Example of tc-agents-list.txt:


For copying your public key, you have something in-built in openssh itself. So instead of cat and ssh use this :-

ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST
  • I want to execute the command copying central server's public to remote servers which exist in IP file. The script iterates through them. So, your example may not be useful here. Thanks – user67186 Jul 17 '14 at 21:52
  • No need to use the -i option if you are using the default location of the public key. – Laith Leo Alobaidy Aug 28 '19 at 4:44

You can do this with a simple while-loop and embedded server list like this:

while read SERVER
    ssh-copy-id user@"${SERVER}"
done <<\EOF

Having the list inside the script does away with separate data files which can get misplaced.

  • ssh-copy-id has already been suggested in some other answers. – RalfFriedl Jun 26 '19 at 16:48

Assuming you have a file with servers IP list, name SERVER, and just the IP addresses of the servers are defined.

This for loop will work as well.

for user in $(awk '{print $1}' SERVER | awk '{printf "user1@""%s"(NR==0?RS:"\n"), $1}' ) ;
        ssh-copy-id -f -i id_rsa_k2.pub $user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.