VPS Tutorials
Transfer files to a VPS: scp, rsync and sftp with safe resume
Choose scp, rsync or sftp for uploads, backups and migration. Understand trailing slashes, exclusions, bandwidth limits, deletion previews, permissions and reliable automation, with integrity checks and slow-transfer diagnosis.

Uploading a website, retrieving a backup and moving logs between servers all need a reliable file-transfer method. scp, rsync over SSH and sftp use the SSH connection, so no FTP port is required. Choose according to whether you need a quick copy, incremental synchronization or interactive browsing.
| Capability | scp | rsync over SSH | sftp |
|---|---|---|---|
| Typical use | A few files | Repeated directory synchronization | Interactive file management |
| Incremental transfer | No | Yes | No block-delta synchronization |
| Resume | No native resume | Retained partial data can be reused | reget/reput for suitable partial files |
| Exclusion rules | No general filter system | Yes | Not an rsync-style filter system |
| Rate limiting | -l, in Kbit/s | --bwlimit, normally KiB/s | -l, in Kbit/s |
| Remote requirement | Normally a working SFTP subsystem in modern OpenSSH | Compatible rsync and SSH command access | A working SFTP subsystem |
- Use
scpfor occasional straightforward copies. - Use
rsyncfor directory synchronization, exclusions and partial transfers. - Use
sftpwhen you want to inspect remote directories interactively.
Install a supported, security-patched rsync on both sides. On Debian/Ubuntu, the server-side example is:
# 服务器上
sudo apt install -y rsync
Otherwise the remote command can fail with bash: rsync: command not found. An SFTP-only account may support scp/sftp but intentionally prohibit rsync's remote command.
# 上传文件
scp local.txt [email protected]:/srv/app/
# 上传目录(-r 递归)
scp -r ./dist [email protected]:/srv/app/
# 下载
scp [email protected]:/var/log/nginx/error.log ./
# 端口是大写 P(小写 p 是保留权限,别搞混)
scp -P 2222 local.txt [email protected]:/srv/app/
# 用指定密钥
scp -i ~/.ssh/id_ed25519 local.txt [email protected]:/srv/app/
Replace documentation IPs, root and paths with the intended host and least-privileged account. Verify the SSH host key. Uppercase -P selects the port; lowercase -p preserves supported timestamps/modes, not the port.
Since OpenSSH 9.0 in 2022, scp normally uses SFTP instead of the legacy scp/rcp protocol. Quoting that previously targeted the remote shell, such as scp host:"/path/with\ space.txt" ., can behave differently. Quote the complete path for your local shell:
scp '[email protected]:/srv/my app/config.yml' ./
Tilde paths such as ~user/file depend on server support; OpenSSH added [email protected] in 8.7. Prefer an explicit path if the server lacks that extension.
For a reviewed legacy compatibility requirement, -O selects the older protocol:
scp -O local.txt [email protected]:/srv/app/
-O changes transport and quoting behavior; use it deliberately, rather than disabling modern defaults for routine transfers.
- Large files: scp has no native continuation; an interrupted 5 GB copy at 90% may need to start again.
- Many small files: protocol and metadata operations can dominate; scp does not necessarily create a new SSH session for every file, but can still be inefficient.
- Repeated directory copies: scp sends selected files again instead of synchronizing changes.
rsync selects changed files, normally using size and modification time, and can transfer changed blocks across SSH rather than resending the whole file. Behavior depends on options and transfer mode; local copies commonly use whole-file transfer. It does not automatically create a database-consistent backup.
# 带斜杠:把 src 里的「内容」同步到 dst
rsync -av src/ dst/
# 不带斜杠:把 src「这个目录本身」放进 dst
rsync -av src dst/
If src/ contains a.txt and b.txt:
| Source form | Result with an existing destination directory |
|---|---|
rsync -av src/ dst/ | dst/a.txt and dst/b.txt |
rsync -av src dst/ | dst/src/a.txt and dst/src/b.txt |
A source slash means its contents; no slash includes that directory. Confirm the real destination path and inspect a dry run before a deployment.
rsync -avz --progress ./dist/ [email protected]:/srv/app/current/
-aequals-rlptgoD: recursion, symlinks, permissions, times, groups, owners and device/special files, subject to permissions. It does not include hard links, ACLs or extended attributes; review -H/-A/-X when required.-vadds verbose output.-zcompresses data. It may help text, code or SQL exports but wastes CPU on already compressed media or archives.--progressshows per-file progress. For many small files, reduce output or use supported aggregate progress;-vmay already be enough.
rsync -avzP --partial-dir=.rsync-partial \
[email protected]:/backup/db-2026-09-28.sql.gz ./
The dated backup filename is an example, not a current backup claim. Remove compression when transferring an already compressed archive unless measurements justify it.
-Pmeans--partial --progress.--partialretains partial data for reuse on the next attempt; it does not guarantee blind byte-for-byte appending.--partial-dirputs partial files in a separate directory. Protect that directory and keep it outside publicly served content.
--append-verify is suitable only when its append assumptions are justified, such as a known matching prefix of a stable/append-only source. Do not treat it as universally safer. If the source changed unpredictably, use ordinary verified synchronization from a consistent snapshot instead. Never copy a live database's raw files and assume a resumable transfer makes them valid.
rsync -avz \
--exclude='.git' \
--exclude='node_modules' \
--exclude='*.log' \
--exclude='cache/' \
./project/ [email protected]:/srv/app/
For reusable filters:
# .rsyncignore
.git/
node_modules/
*.log
cache/
.env
rsync -avz --exclude-from='.rsyncignore' ./project/ [email protected]:/srv/app/
The filter file includes .env so local secrets do not overwrite production settings, and .git so repository internals are not deployed. Check application-specific uploads, caches and runtime data too. Filters have ordering/path semantics; test them on the intended source root.
rsync -avz --bwlimit=2000 ./backup/ [email protected]:/backup/
An unsuffixed --bwlimit uses units of 1024 bytes per second in current rsync: 2000 is about 1.95 MiB/s, roughly the source's 2 MB/s illustration. Allow for overhead and version-specific suffixes. Protect application traffic and metered transfer quotas; see traffic monitoring and alerts.
--delete needs particular care; start with --dry-run. --delete deletes destination entries absent from the synchronized source scope. Exclusions and deletion modes also affect what is protected.
Before --delete, run --dry-run or -n:
# 先演练,只打印会做什么,不实际执行
rsync -avz --delete --dry-run ./dist/ [email protected]:/srv/app/current/
# 确认输出符合预期,再去掉 --dry-run
rsync -avz --delete ./dist/ [email protected]:/srv/app/current/
Read the full preview and verify source directory, destination, filters and a recoverable backup. A nonexistent source normally reports an error, but an unintended existing empty directory combined with --delete can erase the destination. --dry-run helps catch that before execution. --delete-after delays deletion and --max-delete=100 caps its count; neither replaces a correct scope and backup.
scp uses -P for a port. rsync's -P is not that option: its -P means partial/progress. Pass SSH options using -e:
rsync -avz -e "ssh -p 2222" ./dist/ [email protected]:/srv/app/
rsync -avz -e "ssh -p 2222 -i ~/.ssh/id_ed25519" ./dist/ [email protected]:/srv/app/
Or define the connection in ~/.ssh/config, following SSH key configuration, and use its alias:
rsync -avz ./dist/ myserver:/srv/app/
-a requests metadata preservation subject to receiving permissions. A local owner such as you:you is not automatically the right production owner; the web process may use www-data or a separate application account.
rsync -avz --chown=www-data:www-data ./dist/ [email protected]:/srv/app/
--chown acts on the receiving side and requires suitable privileges. Prefer deployment into a directory owned by the deployment account, with reviewed read permissions for the service, rather than unnecessary root SSH. A 403 can also involve parent directories or SELinux; see 403 diagnosis.
sftp [email protected]
# 进去之后
ls # 列远端目录
lls # 列本地目录(前面加 l 表示 local)
cd /srv/app # 切换远端目录
lcd ~/Downloads # 切换本地目录
get backup.sql.gz # 下载
put config.yml # 上传
get -r logs/ # 递归下载目录
mkdir newdir # 建目录
rm oldfile # 删文件
bye # 退出
ls/cd refer to remote locations; lls/lcd refer to your computer. get downloads and put uploads. Commands such as rm are real destructive operations on the remote account, not a preview.
For suitable stable partial files:
reget backup.sql.gz # 续传下载
reput bigfile.iso # 续传上传
reget/reput assume the existing prefix matches. Check integrity after resuming and avoid them if content changed. Port selection is sftp -P 2222 [email protected], using uppercase -P.
sftp [email protected] <<'EOF'
cd /srv/app
put config.yml
bye
EOF
The quoted here-document avoids local variable expansion. For automation, use a tested key and host-key policy, BatchMode=yes and an actual sftp -b batch file when you need batch-command failures to abort reliably. Check exit status and do not embed an interactive password expectation.
For external users, design restrictions in sshd_config, such as ChrootDirectory, together with internal-sftp, no arbitrary shell/forwarding and appropriate permissions. Chroot path components must have the required root ownership and not be writable by the confined user; give them a separate writable upload directory. Ordinary SFTP permissions do not hide every readable system path by themselves.
| Tool | Platforms | Typical use |
|---|---|---|
| WinSCP | Windows | Transfers, synchronization and scripting |
| FileZilla | Windows, macOS, Linux | SFTP browsing and transfer |
| Termius | Supported desktop/mobile platforms | Integrated sessions and transfer |
| MobaXterm | Windows | Terminal and file management |
| Cyberduck | macOS and Windows | SFTP and supported object storage |
- Select SFTP, not plaintext FTP. FTP can expose credentials and data without separate transport protection.
- Prefer verified SSH keys; see key setup. Inspect storage/synchronization of private credentials.
Some graphical tools support filters and resume, so do not assume they all lack them. For large repeated synchronization, rsync usually gives more explicit control and reproducible command-line behavior.
- Network and TCP behavior: a 200 ms international link may be limited by window size, packet loss or congestion even when nominal bandwidth is high. Measure before changing tuning. Parallel chunks can help some workloads but require safe reassembly and integrity checks.
- CPU/cipher cost: on a small VPS, inspect CPU use and compare supported modern ciphers:
rsync -avz -e "ssh -c [email protected]" ./bigfile [email protected]:/data/
[email protected] benefits from AES hardware support when available; it is not automatically faster on every CPU. Do not weaken cryptography for an assumed speed gain.
- Storage: inspect
iostat, including latency and queue depth as well as%util. A high utilization value alone is not a complete performance diagnosis on modern parallel devices. See disk I/O diagnosis. - Redundant compression:
-zrarely helps compressed images, video, zip or gz.
Practical choices:
- Use
rsync -Pwith reviewed partial-file behavior for resumable transfers. - Consider
gzipfor compressible SQL exports/logs; savings vary and are not guaranteed to exceed half. - Compare direct transfer with protected object storage for very large moves, considering cost, quotas and retention; see when object storage helps.
- Compare a hash on both sides.
md5sumcan detect accidental changes, but prefersha256sumfor a modern integrity check; a hash from an untrusted source does not establish authenticity.
rsync -avz --delete --dry-run ./dist/ [email protected]:/srv/app/current/
# 确认无误后去掉 --dry-run
rsync -avz --delete ./dist/ [email protected]:/srv/app/current/
Review deletion scope first. Synchronizing the live directory in place is not an atomic deployment; use a new release directory and a tested switch when availability requires it. Keep secrets and uploads outside the replaced build output.
rsync -avzP --bwlimit=5000 \
[email protected]:/srv/backups/ ~/vps-backups/
Check local space, protected destination and the backup's actual consistency. Copying a backup is not a restore drill.
# 在旧服务器上执行,直接推到新机器
rsync -avzP /srv/app/ root@新IP:/srv/app/
Run on the old host and replace the destination IP. Configure a restricted credential and verify the new host key; do not copy your personal private key onto an untrusted server. A direct path may be faster, but measure it. See VPS migration.
rsync -avz --include='*.log' --exclude='*' \
[email protected]:/var/log/nginx/ ./logs/
This filter selects matching files at the source level. For nested log directories, include directory traversal with --include='*/' before the exclusion. Logs can contain sensitive data; restrict destination access.
rsync -avz --files-from=<(find /srv/app -type f -mtime -7 -printf '%P\n') \
/srv/app/ [email protected]:/srv/app/
The example requires Bash process substitution and GNU find; -mtime -7 is based on file modification age, not a calendar date. Run where /srv/app is the source. For arbitrary filenames, prefer a NUL-separated list:
find /srv/app -type f -mtime -7 -printf '%P\0' > /tmp/app-recent-files.list
rsync -av --from0 --files-from=/tmp/app-recent-files.list /srv/app/ deploy@HOST:/srv/app/
Use a protected, uniquely named list in shared environments, verify HOST and review the selected paths before execution.
rsync: command not found: install patched rsync on both ends. sudo apt install -y rsync is the Debian/Ubuntu example.
Permission denied (publickey): diagnose SSH authentication first with Permission denied troubleshooting.
rsync: failed to set times on ...: Operation not permitted: the receiver may lack permission to set requested times. If preserving them is unnecessary, --no-times changes that request; otherwise fix intended ownership rather than granting broad privileges.
Wrong owner or 403: review --chown and any carefully scoped chown -R, plus directory permissions and service policy; see 403 troubleshooting.
No space left on device: check the receiving filesystem:
df -h # 空间
df -i # inode(小文件特别多时会先耗尽这个)
Check blocks, inodes and quotas; see disk cleanup and inode exhaustion.
Interrupted connection: retain appropriate partial data with -P, then confirm integrity and source stability. Diagnose SSH disconnections.
Only a few dozen KB/s: inspect an accidental --bwlimit, storage/CPU contention, network loss and provider limits. See VPS throttling diagnosis.
Is transfer encrypted? These SSH-based modes encrypt the SSH hop. Verify host identity and protect both endpoints; plain rsync daemon mode is a different transport and is not covered by this statement.
What about symlinks? -a preserves the link. -L follows links to their contents; -L can broaden the copied scope or encounter cycles. Review untrusted links and destination boundaries.
Windows rsync? Use a supported WSL installation, for example wsl rsync ..., or a maintained compatible package such as cwRsync. WinSCP synchronization is another workflow with different behavior. Verify installed OpenSSH Client for scp/sftp instead of assuming every Windows image has it.
How do I verify a file? Compare hashes from trusted endpoints:
# 本地
sha256sum bigfile.tar.gz
# 服务器
sha256sum /srv/bigfile.tar.gz
On macOS use shasum -a 256; on Windows use Get-FileHash with SHA256. rsync's -c changes file-selection checks to content-based comparison and adds disk work; it is not the only integrity mechanism used during transfer.
Can servers transfer directly? Run rsync on an authorized source host:
rsync -avzP /data/ root@另一台IP:/data/
Replace the target placeholder, authorize a restricted key and verify network/host identity. A direct route is not necessarily faster in every geography.
How do I schedule synchronization? Use a script plus cron/systemd timer, explicit paths, noninteractive authentication and a trusted known_hosts file. Capture failures and prevent overlapping runs; see scheduled tasks that do not run.
Is a mirror a backup? rsync --delete can propagate deletions. Keep separate time-based backups, offsite retention and tested recovery; follow a backup restore drill.