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.
Comments on this entry are closed.
This is very useful, was one of the problems to research on my todo. Done now! Thanks.
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.
I use rsa instead of dsa, what is your view?
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.
Great website!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I love it!
Excellent article. Been searching for this answer for a while now and this was just the ticket. Thanks
Great tip! Thanks!
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
Thanks A lot!! This is what i was looking for.
Dear all,
how do i use rsync and then will compress to tar file include date in name?
Great information. Thanks Ramesh.
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/
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.
Anyone knows how to send the command to the background?
The nohup do not work after I logout.
Thanks.
Works like a charm to do a cron job for rsyncing a remote server to a local server. Thanks!!!
Thanks.