How to Use the scp Command on Linux
The scp
command makes copying files between Linux computers easy and secure. It uses SSH security, but best of all, it is simple. If you can use cp
, you can use scp
.
The Secure Copy Protocol and scp
Let’s define a couple of terms: there’s SCP and there’s scp
. The uppercase SCP stands for the Secure Copy Protocol. The lowercase scp
stands for secure cp
. In other words, SCP is a protocol and scp
is a program.
scp
was designed to be a safe and secure means of copying files between remote Linux computers. It uses SSH to establish secure connections. SSH, or secure shell, is a cryptographic network protocol often used to access and log in to remote Linux computers. On Linux distributions, SSH functionality is provided by OpenSSH.
SCP is somewhat long in the tooth, and concerns have been aired concerning its use in the present day. Since OpenSSH version 8.8, SCP has been considered deprecated. Modern implementations of scp
default to using the Secure File Transfer Protocol by default. SSH is still used for the secure connection, but the file transfers are handled by SFTP. This is all invisible and happens magically under the hood, and the scp
syntax has remained the same.
The rsync
program is preferred over scp
, but you may encounter a computer that doesn’t have rsync
installed, and for which you don’t have root privileges meaning you can’t go ahead and install it. For copying files from computer to computer on a self-contained network, scp
is perfectly fine. For scp
to work, you must have SSH running on all of the computers you’ll be copying to and from.
To see the version of OpenSSH installed on your computer, type:
ssh -V
Copying a Single File
Like the standard cp
command, scp
copies files from the source location to the target location. To copy a file to a remote computer, you must know the IP address or network name of the remote computer. You must also have the credentials for a user account that has write privileges for the location you’re sending the file to.
To send a file called “sample.txt” to a computer called “fedora-34” on the local network, the syntax is:
scp ./sample.txt dave@fedora-34.local:/home/dave/Downloads/
The command is made up of:
- scp: The scp command
- ./sample.txt: The file we’re going to send. This is in the current directory.
- dave@: The user account on the remote computer we’re going to send the file to.
- fedora-34.local: The network name of the remote computer.
- :/home/dave/Downloads/: The location to copy the file to on the remote computer. Note the colon “:” that separates the computer name and the path.
You’ll be prompted to enter the password for the account on the remote computer, and then the file is copied.
If you want the file to have a different name on the remote computer, you can add a filename to the target path. To copy the same file and have it named “different-file.txt”, use this syntax:
scp ./sample.txt dave@fedora-34.local:/home/dave/Downloads/different-file.txt
The scp
command will silently overwrite existing files, so be careful when you’re copying files. If a file already exists on the target computer with the same name as the file you’re copying, it will be overwritten and lost.
If the target computer isn’t using the default SSH port of 22, you can use the -P
(port number) option to provide the appropriate port number.
Retrieving a Single File
To copy a file from a remote server, simply put the remote server as the source, and put the local path where you want the file copied as the target. We’re going to copy a file called “development-plan.md” from the remote computer to the current directory on the local computer.
scp dave@fedora-34.local:/home/dave/Downloads/development-plan.md .
If you add a filename to the local path, the file is copied and given that name.
scp dave@fedora-34.local:/home/dave/Downloads/development-plan.md ./dp-1.md
The file is copied but renamed to our specified filename.
ls -hl *.md
Copying Multiple Files
Copying multiple files in either direction is easy. You can list as many source files as you like. Here, we’re copying two markdown files and a CSV file.
scp ./dp-1.md ./dp-2.md ./dp-3.csv dave@fedora-34.local:/home/dave/Downloads/
The three files are copied to the remote computer. You can also use wildcards. This command does exactly the same thing as the last command.
scp ./dp. dave@fedora-34.local:/home/dave/Downloads/
Recursively Copying Directories
The -r
(recursive) option lets you copy entire directory trees with a single command. We’ve placed two files in a directory called “data” and created a directory called “CSV” inside the “data” directory. We placed a CSV file in the “data/CSV” directory.
This command copies the files and recreates the directory structure on the remote computer.
scp -r ./data dave@fedora-34.local:/home/dave/Downloads/
Copying Files Between Remote Servers
You can even instruct scp
to copy files from one remote server to another. The syntax is pretty simple. You provide the account name and network address of the source server and the account name and network address of the target server. The files are copied from the source server and copied to the location on the target server.
Although the syntax is simple, ensuring everything else is in place takes a bit more thought. Obviously, the location you’re trying to copy the files to on the remote server must be accessible by the user account you specify on the command line. And that user account must have write permissions on that location.
A more subtle prerequisite is that SSH access must be set up between your local computer and the source computer, and also between the source and target servers. Ensure that you can use SSH to log in to the target server from the source server. If you can’t do that, scp
won’t be able to connect.
Setting up SSH keys so that you can use authenticated but passwordless access is by far the preferred method. Using passwords becomes messy very quickly, and because you’re prompted for the password for each user account—it prevents you from fully automating the process with a script.
We set up SSH keys for the user accounts we’re using on each remote server. This provided seamless SSH access to the other server, for those two users. This allows us to transfer files in either direction, using those two user accounts.
To copy files from the “davem” user account on a Manjaro computer to the “dave” account on a Fedora computer, via an scp
command issued from our local Ubuntu computer, the syntax is:
scp davem@manjaro20-0-1.local:/home/davem/man. dave@fedora-34.local:/home/dave/
We’re silently returned to the command line. There’s no indication anything happened. Working on the premise that no news is good news, scp
only reports on errors for this remote to remote copying. On checking the Fedora computer we can see that the files from the Manjaro computer have been copied and received.
By default, the files are copied directly from the source computer to the target computer. You can override this using the -3
(three-way) option.
With this option, the files are transferred from the target to the source, through your local computer. For that to happen, there needs to be seamless SSH access from your local computer to the target computer.
scp -3 davem@manjaro20-0-1.local:/home/davem/man. dave@fedora-34.local:/home/dave/
There’s still no indication anything has happened, even when channeling the files through your local computer. The proof of the pudding, of course, is to check the target computer.
Other Options
The -p
(preserve file attributes) will keep the original file creation, ownership, and access flags on the transferred files. They’ll have the same metadata as the original files on the source computer.
If you see error messages, try repeating the command and use the -v
(verbose) flag to see detailed information about the transfer attempt. You ought to be able to spot the point of failure in the output.
The -C
(compress) option compresses the files as they are copied and decompresses them when they’re received. This is something that dates back to the era of slow modem communications between computers. Reducing the size of the payload could reduce transmission times.
Nowadays, the time taken to compress and decompress the files is likely to take longer than the difference between the compressed and uncompressed transmissions. But because scp
is best used to copy files between computers on the same LAN, transmission speed shouldn’t be much of a concern.