Copy files between Linux servers with scp and rsync

Beginner 15 min Nov 15, 2026 15 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 Debian 11 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn how to transfer files securely between Linux servers using scp and rsync, including SSH key authentication, incremental transfers with progress tracking, and troubleshooting common connection errors.

Prerequisites

  • Two Linux servers with SSH access
  • A non-root user with sudo privileges on both servers
  • Basic familiarity with the Linux command line

What this solves

Moving files between Linux servers is one of the most common sysadmin tasks: deploying code, copying backups, syncing configuration, or migrating data to a new host. This tutorial covers the two main tools for the job, scp and rsync, when to use each, and how to do it securely with SSH keys instead of passwords.

Understanding scp vs rsync vs sftp

All three tools move files over SSH, but they behave differently and are suited for different jobs.

ToolBest forKey limitation
scpOne-off copies, small file sets, scripting simple transfersNo resume, no delta transfer, copies everything every time
rsyncLarge directories, repeated syncs, backups, incremental updatesSlightly more complex syntax
sftpInteractive browsing and transfer over SSH, like an FTP sessionManual, not ideal for automation

In short: use scp for quick single-shot copies, use rsync for anything repeated, large, or where you care about bandwidth and resume capability. Both rely on SSH, so if you have not hardened SSH access yet, see configure SSH key authentication and security hardening first.

Step-by-step configuration

Install scp and rsync

scp ships with the openssh-client package on most distributions and is usually already installed. rsync often needs to be installed explicitly.

sudo apt update
sudo apt install -y openssh-client rsync
sudo dnf install -y openssh-clients rsync

Set up SSH key authentication

Password-based transfers are slower to automate and weaker against brute force attempts. Generate an SSH key pair on the source server and copy the public key to the destination.

ssh-keygen -t ed25519 -C "deploy@source-server" -f ~/.ssh/id_ed25519

Copy the public key to the remote server. Replace the user and IP with your own values.

ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10

Test that key-based login works without a password prompt.

ssh -i ~/.ssh/id_ed25519 deploy@203.0.113.10 "echo connected"
Never use chmod 777. SSH refuses keys with overly permissive modes. Keep private keys at 600, the ~/.ssh directory at 700, and public keys at 644.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

If you manage multiple keys or hit authentication failures with agents, see fix SSH too many authentication failures error on Linux.

Copy a single file with scp

The basic scp syntax is source followed by destination, each optionally prefixed with user@host:

scp report.csv deploy@203.0.113.10:/home/deploy/reports/

To pull a file from a remote server to your local machine, reverse the order.

scp deploy@203.0.113.10:/home/deploy/reports/report.csv ./

Copy directories recursively with scp

Use the -r flag to copy an entire directory tree. The -p flag preserves file modification times, access times, and permissions, which matters when copying configuration or application files.

scp -rp /var/www/example.com deploy@203.0.113.10:/var/www/

Specify a custom SSH port with -P (uppercase) if the remote server does not listen on port 22.

scp -rp -P 2222 /var/www/example.com deploy@203.0.113.10:/var/www/

Preview changes with rsync dry-run

Before syncing anything for real, run rsync with --dry-run to see exactly what would be transferred without touching any files.

rsync -avz --dry-run /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

The flags break down as: -a for archive mode (preserves permissions, timestamps, symlinks, and ownership), -v for verbose output, -z for compression during transfer.

Run the real rsync transfer with progress

Drop --dry-run and add --progress to see per-file transfer speed and overall completion.

rsync -avz --progress /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/
Note: A trailing slash on the source directory copies its contents into the destination. Omitting the slash copies the directory itself as a subfolder. Check this carefully before running large syncs.

Exclude files and directories

Use --exclude to skip files that should not be transferred, such as logs, caches, or version control metadata.

rsync -avz --progress --exclude '.git' --exclude 'node_modules' --exclude '*.log' /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

For more complex exclude rules, put patterns in a file and reference it.

.git/
node_modules/
*.log
*.tmp
.env
rsync -avz --progress --exclude-from=/home/deploy/rsync-excludes.txt /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

Use a custom SSH port and key with rsync

Pass SSH options through the -e flag when the destination uses a non-default port or a specific key file.

rsync -avz --progress -e "ssh -i ~/.ssh/id_ed25519 -p 2222" /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

Resume an interrupted transfer

rsync tracks what has already been copied, so re-running the same command after an interruption only transfers the remaining or changed data. Add --partial to keep partially transferred files instead of deleting them on interruption.

rsync -avz --progress --partial /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

For unreliable connections, combine --partial with --append-verify, which resumes partial files and verifies the result with a checksum comparison.

rsync -avz --progress --partial --append-verify /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

Verify file integrity after transfer

Use --checksum to force rsync to compare file contents via checksum rather than relying on size and modification time. This is slower but catches silent corruption.

rsync -avz --checksum --dry-run /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/

For a one-off manual check, compare SHA-256 hashes on both ends.

sha256sum /var/www/example.com/index.html
ssh deploy@203.0.113.10 "sha256sum /var/www/example.com/index.html"

Fix ownership after transfer, not permissions

If the remote application user cannot read or write files you just copied, the fix is almost always ownership, not opening up permissions. Transfer files as the deploy user, then hand ownership to the service account that needs it.

sudo chown -R www-data:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;

Directories need the execute bit (755) to be traversable, while regular files only need read and write (644). This keeps the web server able to serve files without exposing them to every user on the system.

Verify your setup

Confirm the transfer completed and both sides match in file count and total size.

du -sh /var/www/example.com
ssh deploy@203.0.113.10 "du -sh /var/www/example.com"

Run an rsync dry-run one more time. If output is empty, both directories are identical.

rsync -avzn --delete /var/www/example.com/ deploy@203.0.113.10:/var/www/example.com/
Warning: The --delete flag removes files on the destination that no longer exist on the source. Always run with --dry-run first before adding --delete to a real sync.

Common issues

SymptomCauseFix
Permission denied (publickey)SSH key not copied to remote authorized_keys, or wrong key specifiedRe-run ssh-copy-id and confirm with ssh -v to see which key is offered
rsync: connection unexpectedly closedrsync binary missing on the remote server, or firewall blocking the SSH port mid-transferInstall rsync on both ends; check firewall rules allow the SSH port continuously, not just on connect
Connection refusedSSH daemon not running, wrong port, or firewall dropping the portCheck systemctl status sshd on the remote host and confirm the port with ss -tlnp
Host key verification failedRemote server's SSH host key changed (rebuild, reinstall, or IP reuse) or known_hosts is staleRemove the old entry with ssh-keygen -R 203.0.113.10 then reconnect and verify the new fingerprint manually
scp: No such file or directoryRemote destination directory does not existCreate it first with ssh user@host mkdir -p /path/to/dir
rsync permission denied writing to destinationDestination directory owned by a different user than the SSH login accountFix ownership with chown on the destination, or sync into a directory the login user owns, then move with correct ownership
Transfer is very slowCompression enabled on already-compressed files, or high latency linkDrop -z for binary files like images or archives; use --bwlimit to control bandwidth instead of fighting latency

Next steps

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need file transfers scheduled and monitored 24/7, keeping it healthy is a different job. See how we run infrastructure like this for European teams.
#scp #rsync #ssh #file-transfer #linux #sftp #ssh-keys #backup

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. Fully managed, with one fixed contact who knows your setup.

You get one fixed contact who knows your setup

Rotterdam 02:49 · reachable in a message, no ticket form

Learn more: managed cloud infrastructure