Help Docs Server Administration Linux Server Administration File and Directory Management in Linux Securely Copying Files with SCP

Securely Copying Files with SCP

SCP lets you securely transfer files over SSH between local and remote hosts, with simple syntax and support for directories, ports, and remote-to-remote copies.

The Secure Copy Protocol, commonly known as SCP, is a valuable tool for copying files between hosts on a network. It is particularly useful for transferring files to or from your server.

One of the key advantages of SCP is that it uses ssh(1) for data transfer. This means it utilizes the same authentication methods and provides the same level of security as SSH. When authentication requires it, SCP will prompt you for passwords or passphrases.

SCP is flexible; any file name specified in the command can include a host and user specification, allowing you to indicate that the file should be copied to or from a specific remote host. Furthermore, SCP even permits copies directly between two different remote hosts.

When you copy a file to a target location where a file with the same name already exists, SCP will replace the existing file’s contents, keeping the original file’s inode (which identifies it within the filesystem).

If the target file does not exist yet, SCP will first create an empty file with the specified target name, and then fill it with the content from the source file. It does not use temporary files for this process.

The basics of SCP commands

The structure of an SCP command is straightforward:

scp $flags $source $destination

Here, $flags are optional parameters (like -r or -P), $source is the file or directory you want to copy, and $destination is where you want to copy it to. When working with remote servers, you can add the IP address or hostname along with the username to either the source or the destination side of the command. The format for specifying a remote file is typically user@hostname_or_ip:/path/to/file.

Common SCP examples

Here are some practical examples of how to use SCP:

To copy a file named “foobar.txt” from a remote server to a directory on your local computer:

$ scp your_user@remotehost.net:foobar.txt /some/local/directory

To copy a file named “foobar.txt” from your local computer to a directory on a remote server:

$ scp foobar.txt your_user@remotehost.net:/some/remote/directory

To copy an entire directory named “foo” from your local computer to a directory named “bar” on a remote server, you need to use the -r flag (which stands for recursive):

$ scp -r foo your_user@remotehost.net:/some/remote/directory/bar

To copy a file named “foobar.txt” directly between two different remote hosts, say from “rh1.net” to “rh2.net”:

$ scp your_user@rh1.net:/some/remote/directory/foobar.txt your_user@rh2.net:/some/remote/directory/

To copy multiple specific files, such as “foo.txt” and “bar.txt”, from your local computer to your home directory on a remote server (represented by ~):

$ scp foo.txt bar.txt your_user@remotehost.net:~

If your SSH server is configured to use a port other than the default port 22, you can specify the port number using the -P flag (note: it’s a capital ‘P’ for port):

$ scp -P 2264 foobar.txt your_user@remotehost.net:/some/remote/directory

To copy multiple files from a remote host to your current working directory on the local host (represented by .):

$ scp your_user@remotehost.net:/some/remote/directory/{a,b,c} . $ scp your_user@remotehost.net:~/{foo.txt,bar.txt} .

Understanding these basic commands and the structure of SCP allows you to securely transfer files between your local machine and your server, or even between two servers.

Was this article helpful?