Chapter 1 · Lesson 16 of 19
Server basics

Copy files to a server with scp and rsync

scp and rsync copy files between your computer and a server over SSH, and rsync only sends what changed.

By the end you will

Copy files and folders to a server with scp and rsync, and preview a sync safely.

Time
14 min
Level
Beginner
Check
4 questions
You need
  • A server you can log in to with SSH
  • A file or folder to copy

To copy files to a server, use scp file user@host:/path/ for one file or rsync -avz folder/ user@host:/path/ for a folder. You build a site on your laptop and need it on the server. Or a database dump sits on the server and you want a copy. Both tools send files through the same encrypted SSH connection you already use to log in, so there is no extra service to set up and no extra port to open.

How it works#

scp is a plain copy. You name a source and a destination, and it sends each file whole. A remote location is written user@host:path. The part after the colon is the path on the server, and a relative path starts in that user’s home directory.

rsync is a synchroniser. It compares the source and the destination, then sends only the parts that differ. The first run copies everything. Later runs are quick, because unchanged files are skipped. It also keeps file times and permissions when you ask it to, and can resume a broken transfer.

Both start ssh (see What is SSH) underneath, so everything in your SSH config (user, port, key) applies. Both need a login the server accepts. rsync also needs the rsync program on both ends, since the two sides talk to each other.

TaskUse
One file, oncescp
A folder, or repeating itrsync
A large file on a flaky linkrsync --partial
Make the server match a folder exactlyrsync --delete (preview first)

Replace deploy, 203.0.113.10 and the paths with your own in the commands below.

Step 1: Upload a file with scp#

bash
scp ./backup.tar.gz deploy@203.0.113.10:/home/deploy/

What you should see: a progress line with the file name, size, speed and time, ending at 100%. Download a file by swapping the order:

bash
scp deploy@203.0.113.10:/var/log/app.log ./

Add -r to copy a folder, and -P 2222 (capital P) for a custom SSH port. The capital is different from ssh -p, which is lower case.

Step 2: Sync a folder with rsync#

bash
rsync -avz ./site/ deploy@203.0.113.10:/home/deploy/site/

Flags: -a is archive mode (recurse and keep permissions and times), -v prints files, -z compresses in transit. What you should see: a list of file names, then a summary line with bytes sent and received. Run it again and only changed files are sent, so the list is short or empty.

Step 3: Preview before you sync#

Warning: the --delete flag removes files on the destination that are not in the source. Always preview with --dry-run first.

bash
rsync -avz --delete --dry-run ./site/ deploy@203.0.113.10:/home/deploy/site/

What you should see: the same kind of listing, with deleting in front of any file that would be removed, and the summary says (DRY RUN). Nothing is changed. Remove --dry-run only when the list looks right.

Step 4: Resume a big transfer#

bash
rsync -avz --partial --progress ./big.iso deploy@203.0.113.10:/home/deploy/

--partial keeps the half-sent file instead of deleting it, and --progress shows a live percentage. If the connection drops, run the same command again to continue.

The trailing slash#

./site/ copies the contents of site into the destination. ./site copies the folder itself, creating site/site. The slash on the destination side does not matter, but on the source it does. Check this twice, especially with --delete.

Check it worked#

bash
ssh deploy@203.0.113.10 'ls -l /home/deploy/site'

You should see your files with sizes that match the originals. For a stronger check on one file, compare checksums on both sides:

bash
sha256sum ./backup.tar.gz
ssh deploy@203.0.113.10 'sha256sum /home/deploy/backup.tar.gz'

What you should see: two lines with the same long hex string.

Edge cases and gotchas#

  • Trailing slash decides where files land. The most common mistake is site/site on the server. A dry run shows it before it happens.
  • IPv6 addresses need brackets in scp. Write deploy@[2001:db8::10]:/tmp/. Without them, the colons confuse the host and path parsing.
  • Spaces in remote paths need careful quoting. Quote the whole remote argument, and for scp you may need an extra level of quotes. Avoiding spaces in server paths removes the problem.
  • Ownership is not copied unless you are root. rsync -a tries to keep owner and group, but a normal user cannot give files to someone else. Files end up owned by the login user.
  • Hidden files are included for folders. Copying a folder takes dotfiles such as .env with it. Check what is inside before you upload, and use --exclude '.env' or --exclude '.git' to leave things out.
  • scp behaviour changed in recent OpenSSH. Newer OpenSSH versions run scp over the SFTP protocol, which can handle wildcards and special characters differently from older releases. If a pattern copies on one machine and not another, that is a likely reason; rsync avoids it.
  • Slow links need compression only for text. -z helps with logs and source code, but already-compressed files (zip, jpg, mp4) gain nothing and cost CPU.

Troubleshooting#

SymptomLikely causeFix
Permission denied on the destinationYour user cannot write thereCopy into your home, then move with sudo on the server
Permission denied (publickey)Wrong user or keyCheck the user name and your key, see Log in with SSH keys
rsync: command not foundrsync missing on the serversudo apt install rsync on the server
Connection refusedSSH is not reachableCheck the IP and port, and the firewall
scp: /path: No such file or directoryWrong remote path, or typoCheck it with ssh deploy@203.0.113.10 'ls /path'
Everything is copied again each runSource path has no trailing slash, or times differUse the trailing slash and -a so times are kept
rsync error: unexplained error (code 255)The SSH connection itself failedRun the ssh command alone and fix that first
rsync: [sender] change_dir ... failed: No such file or directoryThe source path is wrongCheck it with ls on the sending side
Host key verification failed.The saved server fingerprint changedConfirm why, then ssh-keygen -R <address>

Real-world scenario#

You build a static site into ./site on your laptop and need it on a server at 203.0.113.10. The first deploy is rsync -avz ./site/ deploy@203.0.113.10:/var/www/site/, which sends every file.

A week later you change one page and run the same command. rsync sends only that page and prints a short summary. Then you delete an old image locally and want the server to match. You run the command with --delete --dry-run first and see deleting images/old.png in the list, so you know the plan is right and run it for real.

Before you did this you once forgot the trailing slash and ended up with /var/www/site/site. The dry run now catches that.

Quick reference#

TaskCommand
Upload a filescp file user@host:/path/
Download a filescp user@host:/path/file ./
Copy a folderscp -r dir user@host:/path/
Sync a folderrsync -avz ./dir/ user@host:/path/dir/
Preview deletesrsync -avz --delete --dry-run SRC DEST
Resumersync -avz --partial --progress SRC DEST
Custom portscp -P 2222 or rsync -e "ssh -p 2222"

Security and safety notes#

  • --delete removes files on the destination. Always run --dry-run first.
  • Do not upload .env files, private keys or database dumps to a place others can read.

Next#

Stop retyping the options with SSH config file shortcuts.

Check what you learned4 questions
1You run rsync -avz ./site/ user@host:/srv/site/ . What does the trailing slash on ./site/ do?
2Why add --dry-run when you use --delete?
3Which tool is the better choice for a large folder you will sync again later?
4How do you give scp an IPv6 address such as 2001:db8::10 in the target?

Frequently asked questions

Should I use scp or rsync?

Use scp for a quick single file. Use rsync for folders, large transfers and repeated syncs, because it skips unchanged files and can resume.

Do these need anything installed on the server?

Both use your existing SSH login. rsync must also be installed on both ends, and on Ubuntu you can add it with sudo apt install rsync.

How do I copy to a server that uses a different SSH port?

With scp use a capital P, as in scp -P 2222. With rsync pass the whole ssh command, as in rsync -e "ssh -p 2222". An alias in your SSH config avoids both.

Can I copy a file straight into a folder only root can write?

Not as a normal user. Copy to your home directory first, then move it with sudo on the server. Or give your user ownership of the target folder if that is appropriate.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios