Tutorial

How To Copy Files With Rsync Over SSH

Updated on January 19, 2021
author

By Bulat Khamitov

How To Copy Files With Rsync Over SSH

Step 1 - Setup public SSH keys

On our origin server, we will generate public SSH keys with no password:

ssh-keygen -f ~/.ssh/id_rsa -q -P ""
cat ~/.ssh/id_rsa.pub

This is our public SSH key that can be placed on other hosts to give us access:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLVDBIpdpfePg/a6h8au1HTKPPrg8wuTrjdh0QFVPpTI4KHctf6/FGg1NOgM++hrDlbrDVStKn/b3Mu65//tuvY5SG9sR4vrINCSQF++a+YRTGU6Sn4ltKpyj3usHERvBndtFXoDxsYKRCtPfgm1BGTBpoSl2A7lrwnmVSg+u11FOa1xSZ393aaBFDSeX8GlJf1SojWYIAbE25Xe3z5L232vZ5acC2PJkvKctzvUttJCP91gbNe5FSwDolE44diYbNYqEtvq2Jt8x45YzgFSVKf6ffnPwnUDwhtvc2f317TKx9l2Eq4aWqXTOMiPFA5ZRM/CF0IJCqeXG6s+qVfRjB root@cloudads

Copy this key to your clipboard and login to your destination server.

Place this SSH key into your ~/.ssh/authorized_keys file:

If your SSH folder does not exist, create it manually:

mkdir ~/.ssh
chmod 0700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 0644 ~/.ssh/authorized_keys

Step 3 - Rsync files over

Rsync is a great utility, as it allows you, among many other things, to copy files recursively with compression, and over an encrypted channel.

We will copy a file from our origin server (198.211.117.101) in /root/bigfile.txt over to our destination server (IP: 198.211.117.129) and save it in /root/bigfile.txt as well.

Login on 198.211.117.101 and rsync the file over to 198.211.117.129:

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /root/bigfile.txt 198.211.117.129:/root/

If you are using a different user, for example "username" then you would have to append it in front of destination server. Make sure to have your public key in that user's ~/.ssh/authorized_keys file:

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /root/bigfile.txt username@198.211.117.129:/

The SSH options are useful to keep Rsync quiet and not prompting everytime you connect to a new server.

Verify that you have received the file on destination server (198.211.117.129):

ls -la /root/bigfile.txt

And you are all done!

By Bulat Khamitov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I’m having issues getting this to work. I tried sending to /root/ and also to a different user. When I send to root, I get this error:

rsync: change_dir#1 “/root/” failed: Permission denied (13)

When I try to send it to user@IP it thinks I’m sending it locally and creates a directory on my machine. So I tried sending it to: ssh://user@IP and also tried ssh://IP:/root/ – in both cases, it seems to get stuck. No errors, just stuck. Any suggestions on what to try next are appreciated in advance!

alexdo
Site Moderator
Site Moderator badge
March 30, 2024

Heya,

Firstly, if you’re trying to send files directly to the /root/ directory on the remote server, you might encounter a “Permission Denied” error. This is because by default, only the root user has write permissions to that directory. It’s generally not recommended to write directly to /root/ for security reasons. Consider sending your files to a different directory where you have write permissions.

Secondly, when specifying the destination as user@IP, rsync might interpret it as a local path instead of a remote destination. To specify a remote destination, you need to use the format user@IP:/path/to/destination. This ensures rsync knows you’re targeting a remote server.

As for the ssh:// prefix, rsync doesn’t recognize it. Rsync already uses SSH by default when you specify the -e "ssh ..." option, so you don’t need to explicitly include ssh:// in the destination address.

Here’s an example of how you could structure your rsync command:

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress ~/rocktree/dev/apps/countdown-timer/index.php user@192.81.208.45:/path/to/destination/

Just replace user with the appropriate username on the remote server and /path/to/destination/ with the directory path where you have write permissions.

Hope that this helps!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 26, 2013

@scdavis41 what exact command are you running?

Here’s the command I’m running in trying to send it to root:

rsync -avz -e “ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null” --progress ~/rocktree/dev/apps/countdown-timer/index.php 192.81.208.45:/root/

And here is the full error:

rsync: change_dir#1 “/root/” failed: Permission denied (13) rsync error: errors selecting input/output files, dirs (code 3) at main.c(562) [Receiver=3.0.9] rsync: connection unexpectedly closed (8 bytes received so far) [sender] rsync error: error in rsync protocol data stream (code 12) at /SourceCache/rsync/rsync-42/rsync/io.c(452) [sender=2.6.9]

ssh not connect properly

alexdo
Site Moderator
Site Moderator badge
March 30, 2024

Heya,

The error message indicates that there’s a permission issue when trying to access the destination directory /root/ on the remote server. This typically occurs because the user you’re connecting as (in this case, root) doesn’t have the necessary permissions to write to the target directory.

By default, the /root/ directory is only writable by the root user, and it’s generally not recommended to write directly to this directory for security reasons.

You can use a different directory and try fixing permissions to see it this will help. Another option will be to use a non-root user for the rsync if you’re only transferring files and don’t require root access on the remote server. You can create a new user with appropriate permissions and write access to the target directory.

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress ~/rocktree/dev

Hope that this helps!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 26, 2013

To be able to upload to /root, you have to connect as root:

rsync -avz -e “ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null” --progress ~/rocktree/dev/apps/countdown-timer/index.php root@192.81.208.45:/root/

Awesome! That did it. Thanks so much, Kamal.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
June 27, 2013

@scdavis41: Glad I could help! :]

nice! thanks!

'">xyz<1(

thanks !!

how to run rsync over ssh in a crontab… im having this problem …do you have any suggestions ?? i gave full path to rsync ,ssh etc etc …the script works fine when executed but when place in crontab i doesnt work at all.

alexdo
Site Moderator
Site Moderator badge
March 4, 2024

You can ensure that you’re using full paths for all commands and files in your script. This includes the paths to rsync, ssh, and any other executables or files referenced in your script.

Cron jobs don’t have an attached terminal, so any output generated by your script won’t be visible by default. Redirect both standard output and standard error to a log file within your cron job script. For example:

* * * * * /path/to/your/script.sh >> /path/to/logfile.log 2>&1

This will capture both standard output and standard error to logfile.log, which can help you diagnose any issues.

Regards

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 30, 2013

@urgen: Please paste the line you added to your crontab file.

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.