11

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.

#!/bin/bash


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'



EOF

done

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@0.0.0.0 '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
16

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

#!/bin/bash
for ip in `cat /home/list_of_servers`; do
    ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
done
  • 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 Al Obaidy Dec 24 '18 at 19:12
3

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

#!/bin/bash
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'
    done
else
    echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
fi

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:

root@10.10.0.1
root@10.10.0.2
root@10.10.0.3
root@10.10.0.4
2

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
1

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

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

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

while read SERVER
do
    ssh-copy-id user@"${SERVER}"
done <<\EOF
server1
server2
server3
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 at 16:48

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.