Monday, May 10, 2010

Using SCP without prompting for a password

I often have to scp several files from one server to another, but if it's not set up for it, every file prompts for a password. This is fine if you're only doing 1 or 2, but not if you're copying dozens.

You need to generate a public key and copy it to the destination server.

For example, I have to copy several files from server a (call it source) to server b (call it target).

On source, go to the ~/.ssh directory. If it doesn't exist, create it. If it does exist and there are files in there, it's probably a good idea to take a copy first in case you break something.

Generate a public key like this:

ssh-keygen -t rsa

You should see this:

Generating public/private rsa key pair
Enter file in which to save the key ...

Press Enter, and you should see this:

Enter passphrase (empty for no passphrase):

Press Enter twice.

You should see this:

Your identification has been saved in id_rsa.pub
Your public key has been saved in id_rsa.pub

Copy this file (you can scp it) to the target server, and put it in the /tmp folder there.

Log on to the target server, and cd to the ~/.ssh directory. If there is already an id_rsa.pub file you should rename the one you copied to the /tmp directory, and then move it to the ./ssh directory. If one doesn't already exist it's OK to just move it.

If there is an "authorized_keys" file, take a backup and edit it. Copy the contents of file you copied to the end of this. If there is no "authorized_keys" file in the ~/.ssh directory, just rename your file to "authorized_keys".

Log back onto the source server and try to scp a file - it should copy without prompting for a password.

No comments:

Post a Comment