How to Setup Rsync with SSH on UNIX / Linux (rsync without password)

by Ramesh Natarajan on July 15, 2011

Question: When I perform rsync, it asks for my password on the remote server before starting the transfer. I would like to avoid this, and perform rsync without password. Can you explain with an example on how to setup rsync over ssh without password on Linux?

Answer: The following steps explains how to setup rsync over ssh that doesn’t ask for a password. This is helpful when you are scheduling a cron job for automatic backup using rsync.

1. Test rsync over ssh (with password):

Do a rsync to make sure it asks for the password for your account on the remote server, and successfully copies the files to the remote server.

The following example will synchronize the local folder /home/ramesh to the remote folder /backup/ramesh (on server).

We discussed in detail about rsync in our previous 15 rsync examples articles.

This should ask you for the password of your account on the remote server.

rsync -avz -e ssh /home/ramesh/ ramesh@

2. ssh-keygen generates keys.

Now setup ssh so that it doesn’t ask for password when you perform ssh. Use ssh-keygen on local server to generate public and private keys.

$ ssh-keygen
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Note: When it asks you to enter the passphrase just press enter key, and do not give any password here.

3. ssh-copy-id copies public key to remote host

Use ssh-copy-id, to copy the public key to the remote host.

ssh-copy-id -i ~/.ssh/

Note: The above will ask the password for your account on the remote host, and copy the public key automatically to the appropriate location. If ssh-copy-id doesn’t work for you, use the method we discussed earlier to setup ssh password less login.

4. Perform rsync over ssh without password

Now, you should be able to ssh to remote host without entering the password.


Perform the rsync again, it should not ask you to enter any password this time.

rsync -avz -e ssh /home/ramesh/ ramesh@

If you want to schedule this rsync backup job automatically, use cron to set it up.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 15 comments… read them below or add one }

1 Uday July 15, 2011 at 8:42 am

This is very useful, was one of the problems to research on my todo. Done now! Thanks.

2 chris July 15, 2011 at 10:42 am

Great info. NOTE: this only works if your identity key is named or If you choose a custom name you’ll have to create/modify the ~./.ssh/config file (when rsync-ing). or use the -i flag for the ssh command.

3 shark巨菜 July 16, 2011 at 7:46 am

I use rsa instead of dsa, what is your view?

4 umask666 July 16, 2011 at 7:47 pm

Yo, thanks for the article. I learned about the ssh-copy-id command. I never used that before. How about using some options with the ssh-keygen command?

ssh -t rsa -N ” -f ~/.ssh/id_rsa_key -b 4096 -C ‘rsync backup’

@shark dsa is the default if you do not specify rsa.

5 umask666 July 16, 2011 at 7:48 pm

Great website!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I love it!

6 Iain September 20, 2011 at 10:57 pm

Excellent article. Been searching for this answer for a while now and this was just the ticket. Thanks

7 SteveT February 2, 2012 at 2:09 pm

Great tip! Thanks!

8 Anonymous August 30, 2012 at 3:20 am

Great, thanks !
Just a suggestion to avoid copying the empty passphrase key onto the default one.
Let’s say that the empty passphrase key as been created as ~/.ssh/, and for sure the private key as well.
It is therefore possible to use it within the ssh option on the rsync command.

Reusing the command given above, it gives :
rsync -avz -e ‘ssh -i ~/.ssh/id_rsa_empty’ /home/ramesh/ ramesh@

Hope this will help
Best regards.

9 Omkar A September 25, 2013 at 3:41 am

Thanks A lot!! This is what i was looking for.

10 sophea October 31, 2013 at 10:22 pm

Dear all,
how do i use rsync and then will compress to tar file include date in name?

11 Shinoy November 7, 2013 at 2:20 pm

Great information. Thanks Ramesh.

12 pszemek November 9, 2013 at 5:43 am

if you must specify ssh port, you can:
add file ~/.ssh/config
and specify host parameters:

Port 8023
User the_user_example

now you can :
rsync -avz -e ssh /home/source/ YOUR_HOST_ALIAS:/remote/destiation/

13 Dirk Krause December 22, 2013 at 12:38 pm

a) to chime in with chris: if you chose a different id file name you need to take the ‘-i’ option
b) don’t use the tilde with the ‘-i’ option: do it like so:
rsync -avze “ssh -i /Users/myName/.ssh/pi1_rsa” test pi@myIP:
(see here)
c) if you are on a mac with no ssh-copy-id, this helps.

14 Jose Mendes January 1, 2014 at 6:06 am

Anyone knows how to send the command to the background?
The nohup do not work after I logout.

15 sam January 21, 2015 at 11:47 am

Works like a charm to do a cron job for rsyncing a remote server to a local server. Thanks!!!

Leave a Comment

Previous post:

Next post: