Tuesday, 12 April 2011

SCP scripting without password

To transfer files from one unix server (Server A) to another unix server (Server B), we usually use SCP command:
scp user@ServerB:/home/user/test.txt /home/user/
However, it always prompt for password, which is very bad for scripting.

YES! There is some solution, i.e. to be authorized in the destination server. Here is step-to-step procedures to achieve this:

1. Generate public/private key pairs in home/user/.ssh in Server A:
ssh-keygen -t rsa


2. Copy id_rsa.pub in Server A to home/user/.ssh/authorized_keys2 in Server B:
scp .ssh/id_rsa.pub user@ServerB:/home/user/.ssh/authorized_keys2
The system will prompt for user password at this stage.

Note: The destination file should be authorized_keys for SSH protocol version 1, or authorized_keys2 for SSH protocol version 2.

3. Make sure that permissions for .ssh directory in both servers are set to 700.
- To view permission
ls  -ld .ssh
- To change permission
chmod 700 .ssh

Done! Now SCP won't prompt password again during file copy.

For more command about file transfer using Linux or Unix, e.g. ftp, sftp, wget etc.,  you may visit http://aruljohn.com/info/filetransfer/