Distribute the ssh keys

Some situations requires that a server can ssh without login to the a set of remote hosts.
This is achieved by deploying the proper ssh keys.

To a single host

host="hostname here"
cat ~/.ssh/id_rsa.pub | ssh root@${host} 'cat >> ~/.ssh/authorized_keys'

To several machines

To distribute the keys, create a hosts.txt file with the platform hostname :

host1
host2
host3

then run the following script :

#!/bin/bash

for host in $(cat ./hosts.txt); do
echo $host;
cat ~/.ssh/id_rsa.pub | ssh root@${host} 'cat >> ~/.ssh/authorized_keys'
done
Advertisements