Bala's Blog

Copy SSH Public Key to multiple Remote Systems

August 11 2018
122 words, ~1 min. read
SSH,  tools 

I have a few Linux and FreeBSD systems, which I periodically refresh. And, whenever I refresh, I used to copy my SSH public key from my laptop to these systems so that I can perform passwordless login. While I was happy in using ssh-copy-id command, I was looking for an automated way by which I could do for all systems at one shot using shell/batch.

I came across sshpass, which satisfied my requirements. All I had to do was to create a bash shell script, something similar to the one shown below,

  #!/usr/bin/env bash

  sshpass ssh-copy-id -i ~/.ssh/id_rsa.pub root@centos6.sbchand.net -p <password>
  sshpass ssh-copy-id -i ~/.ssh/id_rsa.pub root@centos7.sbchand.net -p <password>
  sshpass ssh-copy-id -i ~/.ssh/id_rsa.pub root@debian9.sbchand.net -p <password>
  sshpass ssh-copy-id -i ~/.ssh/id_rsa.pub root@freebsd11.sbchand.net -p <password>