Skip to content
Back to Series SSH from Scratch
Sysadmin

Secure File Transfer with scp, sftp, and rsync over SSH

Every way to move files between servers securely: scp, interactive sftp, and rsync for large syncs, complete with a comparison and best practices.

A
Alif Afriza Author
· · 3 min read
Secure File Transfer with scp, sftp, and rsync over SSH

Moving files between servers is a daily task for administrators. Three main tools use SSH for this: scp, sftp, and rsync. All of them are encrypted and authenticated by the SSH you already secured in the previous article.

scp: Copying Files the Simple Way

scp (secure copy) copies files in one go:

scp file.txt pengguna@server:/home/pengguna/
scp -r folder/ pengguna@server:/home/pengguna/

-r for folders. Other useful options: -P <port> (change the port), -p (preserve modification times). scp works best for light one-off copies.

sftp: Interactive Transfer, FTP-Style but Secure

sftp gives you an interactive session, similar to FTP but encrypted:

sftp pengguna@server

Commands inside the session resemble an old-school shell: get file (fetch from the server), put file (send to the server), cd, ls, mkdir. Bring up the list with ?. For a single file with no interaction:

sftp pengguna@server:file.txt ./

rsync: Efficient, Incremental Synchronization

When you need to move a lot of data or sync repeatedly, rsync excels: it transfers only the differences (delta) and can compress. Over SSH you simply add -e ssh (or use the default ssh):

rsync -avz -e ssh folder/ pengguna@server:/home/pengguna/folder/
  • -a archive (recursive + preserve metadata)
  • -v verbose
  • -z transfer compression
  • --delete delete files at the destination that do not exist at the source

Be careful with --delete: the wrong direction can wipe data on the destination side. Run -n (dry run) first to see what it would do:

rsync -avzn -e ssh folder/ pengguna@server:/tmp/tes/

Checksum and Resume Modes

Evaluate data that is already identical:

rsync -avz --checksum -e ssh folder/ pengguna@server:/target/

For large transfers that may be interrupted, rsync with checkpoint and partial lets the transfer resume from where it stopped:

rsync -av --partial --append-verify -e ssh bigfile.iso pengguna@server:/backup/

Authentication Across All These Tools

All three tools use the same SSH mechanism, so a public key that is already installed works right away. For automation, make sure the key is used non-interactively (without a passphrase, or with an agent). Keep private keys away from repositories or public scripts.

When the SSH port is different, specify it with -P (scp/sftp) or -e "ssh -p PORT" (rsync):

scp -P 2222 file.txt pengguna@server:/tmp/
rsync -avz -e "ssh -p 2222" folder/ pengguna@server:/tmp/

A Quick Comparison

ToolBest forNotes
scpOne-off, small copiesSimple, no resume
sftpInteractive sessionsManual commands
rsyncSynchronization, backup, large transfersEfficient, has dry-run

Choose based on need, not habit. For routine backups, rsync is the primary choice.

Wrapping Up

You can now move files to a server securely and efficiently. Three tools, one foundation: SSH. In the next article we will learn to manage the entire configuration through ~/.ssh/config and an agent to make life tidier.

Next in the curriculum: up next, managing client configuration with ~/.ssh/config and ssh-agent.

References

  • The scp(1) and sftp(1) man pages, OpenSSH.
  • The rsync(1) man page, the rsync project (options and sync modes).
  • rsync documentation, rsync.samba.org/documentation.html.
  • SSH, The Secure Shell: The Definitive Guide, O’Reilly Media (file transfer chapter).
Open for Contributors

Your ideas deserve to be read by thousands of people.

ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.

Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.

Discussion & Comments