Wednesday, February 20, 2008

Setting up sftp between two UNIX hosts

The "ssh-keygen" command on the source host has to be run in order to create a public-private key "id_dsa" and then the public key "id_dsa.pub" needs to be shipped to the desired destination host.

You only need to set this up the first time and after that key exchange will be handled automatically. No more password imbedded inside the script.

How to setup sftp without password prompt.

1. Create the keys for the origin account, i.e. the account that performs the copy:

$ cd ~/.ssh
$ ssh-keygen -t dsa
You are asked for a passphrase, do not enter a passphrase, type for empty passphrase.

2. Verify the creation of the 2 files:

~/.ssh/id_dsa
~/.ssh/id_dsa.pub

3. Copy ~/.ssh/id_dsa.pub to the destination node.

4. Login into the destination node and verify if file ~/.ssh/authorized_keys is already present, if not do:

$ cd ~/.ssh
$ mv id_dsa.pub authorized_keys

If ~/.ssh/authorized_keys is already present, it means that other nodes are authorized to access the account.

In this case, use an editor to verify ~/.ssh/authorized_keys and add/replace id_dsa.pub as needed.

Note that every entry is one single very long line.

Additional info:
The destination directory must have permission set to 750.