2013/02/04

How to scp without prompting for password

Whenever you need to use scp (secure copy) to copy files, it asks for passwords. It can get really annoying the fact that the password is asked every time. Fortunately, scp is it's easily scriptable.

Suppose we want to copy the file abc.tgz to an account of a remote machine:
scp abc.tgz user@foo:/documents
We can do it without the need of entering the user password, but still in a secure way thanks to ssh.

  1. First, generate a public/private key pair on the local machine:
    ssh-keygen -t rsa
  2. Then press Enter and leave blank the passphrase (since we don't want one). Your public key has been saved in ~/.ssh/id_rsa.pub
  3. Copy the content of public key id_rsa.pub just generated to the remote machine. You can use scp to make the copy. If you are logging in as a user, it would be in /home/user/.ssh/authorized_keys. Notice that the authorized_keys file can contain keys from other PCs. So, if the file already exists and contains text, you need to append the contents of your public key file to what already is there.

Ref: Linux Journal, Jayakara Kini's Weblog