SSH Login to another host without password using ssh-keygen

How To Setup SSH equivalence

If you have multiple hosts with the same user on them you can setup ssh equivalence so you can ssh from one host to another without a password.  This is also help for scp commands.

First you will need to create an ssh-key, make sure you run this as the regular user and not root

$ ssh-keygen -t rsa 

The keys just generated is placed in the users home directory

~/.ssh/id_rsa

~/.ssh/id_rsa.pub

Then you will use the bash script ssh-copy-id.sh to copy the key you just created to a remote host you would like to login to without a password.

$ ./ssh-copy-id.sh user@host

When you run the ssh-copy-id.sh you will be prompted to login to the remote host with your user.

That is it, now when you ssh user@host you will not need to enter a password.

If you need a copy of the ssh-copy-id.sh script, here are the contents which can also be found here on git

 

#!/bin/sh
KEY="$HOME/.ssh/id_rsa.pub"
 
if [ ! -f ~/.ssh/id_rsa.pub ];then
echo "private key not found at $KEY"
echo "* please create it with "ssh-keygen -t rsa" *"
echo "* to login to the remote host without a password, don't give the key you create with ssh-keygen a password! *"
exit
fi
 
if [ -z $1 ];then
echo "Please specify user@host.tld as the first switch to this script"
exit
fi
 
echo "Putting your key on $1... "
 
KEYCODE=`cat $KEY`
ssh -q $1 "mkdir ~/.ssh 2>/dev/null; chmod 700 ~/.ssh; echo "$KEYCODE" >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys"
 
echo "done!"