Logo
Blog: How to Script SFTP, SSH, and SCP with Automatic Authentication

First, obtain or setup a user on both your client and server whose name matches between the two boxes. (The passwords don’t have to match, just the names.)


On the Client (the box you will be connecting FROM):


As the user in question (root, josh, whoever) type:
 
ssh-keygen –t rsa
 

When asked the following, just hit enter to use the default:
 
Enter file in which to save the key (/home/username/.ssh/id_rsa):
 

Again, when asked the following, just hit enter to keep the passphrase blank:
 
Enter passphrase (empty for no passphrase):
 

You will be prompted for the passphrase one more time, again just leave it blank and hit enter.

This places 'id_rsa' and 'id_rsa.pub' (private, public) keys in $HOME/.ssh

Now we need to mod the permissions on these files to make sure everything will work:

 
chmod 600 $HOME/.ssh/id_rsa
chmod 600 $HOME/.ssh/id_rsa.pub
chmod 700 $HOME/.ssh
 

Copy the 'id_rsa.pub' (public key) to the server using whatever method you wish.


On the Server (the box you will be connecting TO):


Take the 'id_rsa.pub' that you transferred and append it to $HOME/.ssh/authorized_keys if it exists. If the file doesn't yet exist, just copy it over.
 
cp id_rsa.pub $HOME/.ssh/authorized_keys
 

Now change the permissions like:
 
chmod 600 $HOME/.ssh/authorized_keys
chmod 700 $HOME/.ssh
 

One more note than can sometimes trip you up. Make sure the .ssh directories and files are all owned by your user. That should be it. Now, from from your client box, you can run all the following types of commands and won't be asked to authenticate!

 
ssh serverName "ls"
scp username@servername:/home/username/*.prn $HOME/
sftp username@servername
 

Great success!

Blog Index