How To scp, ssh and rsync without prompting for password

Whenever you need to use scp to copy files, it asks for passwords. Same with rsync as it (by default) uses ssh as well. Usually scp and rsync commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an expect script to provide the password. Of course, I didn't. Instead I browsed for a solution and found it after quite some time. There are already a couple of links out there which talk about it. I am adding to it...

Lets say you want to copy between two hosts host_src and host_dest. host_src is the host where you would run the scp, ssh or rsyn command, irrespective of the direction of the file copy!

  1. On host_src, run this command as the user that runs scp/ssh/rsync

    $ ssh-keygen -t rsa

    This will prompt for a passphrase. Just press the enter key. It'll then generate an identification (private key) and a public key. Do not ever share the private key with anyone! ssh-keygen shows where it saved the public key. This is by default ~/.ssh/id_rsa.pub:

    Your public key has been saved in <your_home_dir>/.ssh/id_rsa.pub

  1. Transfer the id_rsa.pub file to host_dest by either ftp, scp, rsync or any other method.

  1. On host_dest, login as the remote user which you plan to use when you run scp, ssh or rsync on host_src.

  2. Copy the contents of id_rsa.pub to ~/.ssh/authorized_keys

    $ cat id_rsa.pub >>~/.ssh/authorized_keys
    $ chmod 700 ~/.ssh/authorized_keys

    If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.

  1. Note that ssh by default does not allow root to log in. This has to be explicitly enabled on host_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes. Don't forget to restart sshd so that it reads the modified config file. Do this only if you want to use the root login.

Well, thats it. Now you can run scp, ssh and rsync on host_src connecting to host_dest and it won't prompt for the password. Note that this will still prompt for the password if you are running the commands on host_dest connecting to host_src. You can reverse the steps above (generate the public key on host_dest and copy it to host_src) and you have a two way setup ready!

Comments:

In enterprise environments, it is also possible to use Kerberos to login to ssh servers without being prompted for a password. This support is built into OpenSSH and many graphical clients for Windows and Mac OS X.

Posted by Derek Morr on October 17, 2007 at 08:01 PM IST #

Using a key without a passphrase can be more of a risk than you may want. If anyone ever gets that key (say off a backup tape, or a one-time vulnerability), then the remote account is compromised.

As long as you're running the command interactively, a great alternative is to apply a passphrase, but use the ssh-agent to remember the passphrase while you're logged in (ssh-agent, ssh-add). While there are still vulnerabilities this way, they're much smaller than using a key without a passphrase.

And with the agent running (usually at login time), you only have to enter the passphrase one time.

Posted by Darren Dunham on October 18, 2007 at 02:15 PM IST #

Darren, I completely agree with you. Using ssh-add+ssh-agent does reduce the risk.

Posted by Jayakara Kini on October 23, 2007 at 05:14 AM IST #

Are there any ssh settings on the host_dest that are needed to ensure that this works. After taking the steps here I am still prompted for a password.

- R N

Posted by R Nayak on November 01, 2007 at 01:47 PM IST #

R Nayak: Are you using root login to ssh? If so, you'll have to edit /etc/ssh/sshd_config and restart sshd. See 'PermitRootLogin' option in that file. By default, sshd does not allow root login.

Posted by Jayakara Kini on November 21, 2007 at 03:17 AM IST #

R Nayak: Try chaging the permission of the home directory to 700(chmod 700) on both the src and dest hosts.

Posted by Tapajyoti on December 05, 2007 at 01:20 PM IST #

Hi
It's a superb piece of information, bein Redhat Linux Enterprize administrator, I was really lookin for a way to skip password durin scp and rsync and here I found it. Thanks for help mate. Take care.

Best Regards

Posted by Imran Shakir on December 11, 2007 at 06:54 AM IST #

Post a Comment:
Comments are closed for this entry.
About

jkini

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today