How to add ssh keys to multiple hosts

Requisite
- list.txt: list of hosts
- ~/.ssh/id_rsa.pub: keys to add
Bash script
copy.sh
#!/bin/bash
for ip in `cat list.txt`; do
sshpass -p "yourpassword" ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub [email protected]$ip
done
Explanation
- sshpass pass your password to the ssh prompt
- -o StrictHostKeyChecking=no ignores the “host identity not established” prompt, allowing sshpass to do its job
How to execute
sudo apt install -y sshpass
chmod +x copy.sh
./copy.sh
Reference
- 1: https://unix.stackexchange.com/questions/145182/copy-ssh-public-key-to-multiple-linux-hosts/489279
- 2: https://stackoverflow.com/questions/28461713/how-to-ignore-or-pass-yes-when-the-authenticity-of-host-cant-be-established-i