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.
| Tool | Best for | Key limitation |
|---|---|---|
| scp | One-off copies, small file sets, scripting simple transfers | No resume, no delta transfer, copies everything every time |
| rsync | Large directories, repeated syncs, backups, incremental updates | Slightly more complex syntax |
| sftp | Interactive browsing and transfer over SSH, like an FTP session | Manual, 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 rsyncsudo dnf install -y openssh-clients rsyncSet 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_ed25519Copy 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.10Test that key-based login works without a password prompt.
ssh -i ~/.ssh/id_ed25519 deploy@203.0.113.10 "echo connected"~/.ssh directory at 700, and public keys at 644.chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pubIf 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/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
.envrsync -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.htmlssh 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.comssh 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/Common issues
| Symptom | Cause | Fix |
|---|---|---|
| Permission denied (publickey) | SSH key not copied to remote authorized_keys, or wrong key specified | Re-run ssh-copy-id and confirm with ssh -v to see which key is offered |
| rsync: connection unexpectedly closed | rsync binary missing on the remote server, or firewall blocking the SSH port mid-transfer | Install rsync on both ends; check firewall rules allow the SSH port continuously, not just on connect |
| Connection refused | SSH daemon not running, wrong port, or firewall dropping the port | Check systemctl status sshd on the remote host and confirm the port with ss -tlnp |
| Host key verification failed | Remote server's SSH host key changed (rebuild, reinstall, or IP reuse) or known_hosts is stale | Remove the old entry with ssh-keygen -R 203.0.113.10 then reconnect and verify the new fingerprint manually |
| scp: No such file or directory | Remote destination directory does not exist | Create it first with ssh user@host mkdir -p /path/to/dir |
| rsync permission denied writing to destination | Destination directory owned by a different user than the SSH login account | Fix ownership with chown on the destination, or sync into a directory the login user owns, then move with correct ownership |
| Transfer is very slow | Compression enabled on already-compressed files, or high latency link | Drop -z for binary files like images or archives; use --bwlimit to control bandwidth instead of fighting latency |
Next steps
- Configure SSH key authentication and security hardening
- Fix SSH too many authentication failures error on Linux
- Implement backup rotation policies with automated cleanup
- Configure Linux system backup automation with rsync and systemd timers
- Set up SSH bastion host with jump server configuration
- Automate rsync file sync with cron and logging