≡ Menu

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

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 192.168.200.10 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@192.168.200.10:/backup/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/id_rsa.pub 192.168.200.10

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.

ssh 192.168.200.10

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

rsync -avz -e ssh /home/ramesh/ ramesh@192.168.200.10:/backup/ramesh/

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

Add your comment

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

Comments on this entry are closed.

  • Uday July 15, 2011, 8:42 am

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

  • chris July 15, 2011, 10:42 am

    Great info. NOTE: this only works if your identity key is named id_rsa.pub or id_dsa.pub. 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.

  • shark巨菜 July 16, 2011, 7:46 am

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

  • umask666 July 16, 2011, 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.

  • umask666 July 16, 2011, 7:48 pm

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

  • Iain September 20, 2011, 10:57 pm

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

  • SteveT February 2, 2012, 2:09 pm

    Great tip! Thanks!

  • Anonymous August 30, 2012, 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/id_rsa_empty.pub, 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@192.168.200.10:/backup/ramesh/

    Hope this will help
    Best regards.
    J-L

  • Omkar A September 25, 2013, 3:41 am

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

  • sophea October 31, 2013, 10:22 pm

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

  • Shinoy November 7, 2013, 2:20 pm

    Great information. Thanks Ramesh.

  • pszemek November 9, 2013, 5:43 am

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

    Host YOUR_HOST_ALIAS
    Port 8023
    User the_user_example
    Hostname the.host.example

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

  • Dirk Krause December 22, 2013, 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.

  • Jose Mendes January 1, 2014, 6:06 am

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

  • sam January 21, 2015, 11:47 am

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

  • Huseyin April 1, 2016, 7:36 am

    Thanks.