
This will eliminate your pubkey from getting appended multiple times. NOTE: That last bit is to handle the case where you run the above multiple times against the same servers. sort -u $afile -o $afile - uniquely sorts authorized_keys file and saves it.cat - > $afile - take input from STDIN and append to authorized_keys file.set a variable, $afile, with the path to authorized_keys file.rw- 1 remoteuser remoteuser 771 May 21 23:03 /home/remoteuser/.ssh/authorized_keysĬreate the directory ~/.ssh and ignore warning us if it's already there Set the remote user's umask to 077, this is so that any directories or files we're going to create, will have their permissions set accordingly like so: $ ls -ld ~/.ssh ~/.ssh/authorized_keysĭrwx- 2 remoteuser remoteuser 4096 May 21 22:58 /home/remoteuser/.ssh These are the commands that pssh will run on each server: ' \ '.cmds to add pubkey.' - this is the trickiest part of what's going on, so I'll break this down by itself (see below).-i tells pssh to send any output to STDOUT rather than store it in files (its default behavior).
#SSH COPY PUBLIC KEY PASSWORD#
-A tells pssh to ask for your password and then reuse it for all the servers that it connects to.-l is the remote server's account (we're assuming you have the same username across the servers in the IP file).pssh uses the -I switch to ingest data via STDIN.cat outputs the public key file to pssh.

The above script is generally structured like so: $ cat | pssh -h -l -A -I -i '.cmds to add pubkey.' Warning: do not enter your password if anyone else has superuser | pssh -h ips.txt -l remoteuser -A -I -i \ Here's an example that does the job: $ cat ~/.ssh/my_id_rsa.pub \ NOTE: Using this method doesn't allow you to use ssh-copy-id, however, so you'll need to roll your own method for appending your SSH pub key file to your remote account's ~/.ssh/authorized_keys file. Rather than type your password multiple times you can make use of pssh and its -A switch to prompt for it once, and then feed the password to all the servers in a list.
