How to use the SCP command to transfer files
If you use SSH on your server, it is possible to securely transfer and copy files and directories from your local machine onto a remote machine using the scp
command line tool. SCP stands for Secure Copy Protocol and it can be used to:
- Copy files from your local system to a remote system
- Copy files from a remote system to your local system
- Copy files between two remote systems from your local system
scp
is by default bundled with nearly every Linux distribution, as well as most BSD distributions and modern versions of Windows 10.
When transferring files with scp
, both the file and password are encrypted so it is impossible for anyone looking at network traffic to view the file or password.
Command syntax
By default, the SCP tool assumes you are using port 22
, unless you specify a command-line option. The SCP command line syntax is laid out like this:
scp [OPTIONS] [SOURCE] [DESTINATION]
[OPTIONS]
- Any options you want to specify go here. This is where the options for SSH ports, recursive copying, etc, go. The man page has a full list.
[SOURCE]
- The name of the source file.
[DESTINATION]
- The name of the destination file.
Some example options are:
-P
- The destination server's ssh host port. If you changed the ssh port on your host, use this to specify the port. Otherwise, you don't need it.
-p
- Preserves file modification and access times.
-q
- "Quiet mode", this will hide the progress meter and non-error messages.
-C
- Forces scp
to compress the data as it sends it to the destination machine.
-r
- Copies recursively.
Local files can be specified using an absolute or relative path whereas remote file names need a user and host specification.
Copying a Local File to a Remote System with scp
Here is a basic example command showing how to copy a file from a remote host to a local host:
scp path/to/local-file.ext user@destination-host:path/to/server/remote-file.ext
This command will copy the file path/to/local-file.ext
to path/to/server/remote-file.ext
on the destination server, which is named destination-host
. We also specify a user account named user
. Note that the user account must have permission to the remote path specified in the command. It will also need ssh access - if you can't login to the account through ssh, scp will not work!
When executing the command, a prompt will be displayed requesting the password for the destination server's user account:
user@destination-host's password:
Once entered, the file will begin copying. Unless you specified -q
, scp
should display its progress:
user@destination-host's password: file.ext 100% 0 0.0KB/s 00:00
Copying a Remote File to a Local System with scp
To copy a file from a remote machine to your local machine, the command is very similar. To do so, execute this command:
scp user@destination-host:path/to/file.ext path/to/local-file.ext
This command works in the exact same way, except now, the remote user, host, and path are specified before the local path.
Copying a Remote File to a Remote Destination with scp
You can also use scp
to copy a file between two remote hosts. The command is like this:
scp user1@destination-host1:path/to/file.ext user2@destination-host2:path/to/file.ext
In this case, two remote users need to be specified, and each has to have access to their respective destination servers. A password prompt will be presented for both users.
Connecting scp to another port
If SSH on the destination server is running on a port that is not the default 22
, you can specify the port using the -P
argument:
scp -P 2222 path/to/local-file.ext user@destination-host:path/to/server/remote-file.ext
In this example, we connect to port 2222
, but you will change this to whatever port your SSH is running on.
Copying directories
If you need to recursively copy folders and their contents, you can specify the -r
argument:
scp -r path/to/local-folder user@destination-host:path/to/remote-folder
Using a keypair file
If your server uses keypair files instead of passwords, you can use your keypair file to connect using the -i
argument. These are most common on remote cloud servers, but more configured servers typically use them.
You can specify a keypair file like this:
scp -i path/to/keypair.pem path/to/local-file.ext user@destination-host:path/to/remote-file.ext
If your keypair file requires a password, it will be presented.
Using multiple SCP options
You can also specify multiple arguments with SCP. The following command will copy a folder from a remote host to a local machine, using a keypair file, connecting to port 2222
, while preserving file properties and suppressing output:
scp -p -q -P 2222 -i path/to/local/keypair.pem -r path/to/local-folder user@remote-host:path/to/remote-folder
You should now be ready to use SCP!