Home

Using SCP on Linux

SCP stands for Secure Copy Protocol — it's the tool you reach for when you need to move files or directories between machines over a secure channel.

Unlike a lot of other transfer methods, SCP runs over Secure Shell (SSH), so the data and the credentials (password or key) are encrypted in flight. Nothing leaks on the wire.

How to use scp

There are three patterns you'll hit in practice.

1. Copy from local to a remote host

bash
scp [options] /local/path/file user@remote-host:/remote/path

Examples — file:

bash
scp /root/project/project.txt [email protected]:/root/project/project.txt

You can rename on the remote side by giving a different filename, or just point at a directory (e.g. /root/project/) to keep the original name.

Directory (use -r for recursive):

bash
scp -r /root/project [email protected]:/root

2. Copy from a remote host to local

bash
scp [options] user@remote-host:/remote/path /local/path

File:

bash
scp [email protected]:/root/project/project.txt /root/project/project.txt

Directory:

bash
scp -r [email protected]:/root/project /root/project

3. Copy between two remote hosts

File:

bash
scp [email protected]:/root/project/project.txt [email protected]:/root/project/project.txt

Directory:

bash
scp -r [email protected]:/root/project/ [email protected]:/root/

Useful flags

bash
-C  enable compression-i  use a private key file (e.g. -i private.key)-l  cap bandwidth in Kbit/s (e.g. -l 1000)-r  recursive required when copying a directory-v  verbose output