Thursday, October 16, 2014

Passwordless SSH


# on client:
user=root
[[ ! -f ~/.ssh/id_rsa.pub ]] && ssh-keygen
for n in $CLUSTER_NODES; do ssh-copy-id -i ~/.ssh/id_rsa.pub $root@$n; done

The code above is roughly equivalent to the code below, which is included only to explain roughly what ssh-copy-id does under the covers. The first approach is strongly recommended.  

CLUSTER_NODES="39.96.64.13 39.96.64.15 39.96.64.17"

for node in $CLUSTER_NODES
do
     cat ~/.ssh/id_rsa.pub |
          ssh $user@${node} 'cat >> ~/.ssh/authorized_keys;
               chmod go-w ~/;
               chmod 700 ~/.ssh;
               chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_rsa;
               chmod 644 ~/.ssh/id_rsa.pub' # ~/.ssh/known_hosts
done

# on server:
chmod go-w ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub ~/.ssh/known_hosts

No comments: