Wednesday, April 24, 2013

How could you maintain passwordless connection for SSH, SCP, SFTP, RSYNC commands?


Step 01:
On the client/local machine run the following commands:

mkdir -p $HOME/.ssh (if .shh does not exist.)
chmod 0700 $HOME/.ssh
ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -P ''

This should result in two files, $HOME/.ssh/id_dsa (private key) and $HOME/.ssh/id_dsa.pub (public
        key).

Step 02:
Copy $HOME/.ssh/id_dsa.pub to the server.

Step 03:
On the server run the following commands:

cat id_dsa.pub >> $HOME/.ssh/authorized_keys2
chmod 0600 $HOME/.ssh/authorized_keys2

Or maybe depending on the version of OpenSSH the following commands may also be required:

cat id_dsa.pub >> $HOME/.ssh/authorized_keys
chmod 0600 $HOME/.ssh/authorized_keys

Step 04 (Optional):
An alternative is to create a link from authorized_keys2 to authorized_keys:
cd $HOME/.ssh && ln -s authorized_keys2 authorized_keys

Step 05:
On the client test the results by ssh'ing to the server:

ssh -i $HOME/.ssh/id_dsa server
Add the following $HOME/.ssh/config on the client(Optional).

Host server IdentityFile ~/.ssh/id_dsa
This allows ssh access to the server without having to specify the path to the id_dsa file as an argument to ssh each time.