The scp
the command makes copying files between Linux computers easy and secure. It uses SSH security, but best of all, it’s simple. If you can use cp
you can use scp
.
Secure Copy Protocol and scp
Let’s define a few terms: there is a SCP and there is scp
. Capitalization SCP stands for Secure Copy Protocol. Lowercase letters scp
means safe cp
. In other words, SCP is a protocol and scp
is a program.
scp
is designed to be a safe and secure way to copy files between remote Linux computers. Uses SSH to establish secure connections. SSH, or secure shell, is a cryptographic network protocol often used to access and log on to remote Linux computers. On Linux distributions, SSH functionality is provided through OpenSSH.
SCP has been in the tooth for some time, and there have been concerns about its use today. As of OpenSSH version 8.8, SCP is considered obsolete. Modern implementations scp
default use of secure file transfer protocol. SSH is still used for a secure connection, but the file transfer is managed by SFTP. This is all invisible and happens magically under the hood scp
the syntax remained the same.
The rsync
the program has an advantage over scp
but you may come across a computer that does not have one rsync
installed, and for which you do not have root privileges, which means you cannot continue and install it. To copy files from PC to PC on a stand-alone network, scp
it’s perfectly fine. For scp
to work, you must have SSH running on all the computers you will be copying to and from.
To see the version of OpenSSH installed on your computer, type:
ssh -V
Copy a single file
As standard cp
team, scp
copy files from source location to goal location. To copy a file to a remote computer, you need to know the IP address or network name of the remote computer. You must also have credentials for an account that has write privileges for the location to which you are sending the file.
To send a file named “sample.txt” to a computer named “fedora-34” on the local network, the syntax is:
scp ./sample.txt [email protected]:/home/dave/Downloads/
The command consists of:
- scp: scp command
- ./sample.txt: The file we will send. This is in the current directory.
- dave @: The user account on the remote computer to which we will send the file.
- fedora-34.local: The network name of the remote computer.
- : / home / dave / Downloads /: Location to copy the file to a remote computer. Notice the colon: “” that separates the computer name and the path.
You will be prompted to enter the password for the account on the remote computer, and then the file will be copied.
If you want the file to have a different name on the remote computer, you can add the file name to the target path. To copy the same file and call it “different-file.txt”, use this syntax:
scp ./sample.txt [email protected]:/home/dave/Downloads/different-file.txt
The scp
the command will silently overwrite existing files, so be careful when copying files. If the file already exists on the target computer with the same name as the file you are copying, it will be overwritten and lost.
If the target computer does not use the default SSH port of 22, you can use -P
(port number) option to provide the appropriate port number.
Download a single file
To copy a file from remote server, simply put the remote server as the source and set the local path to which you want the file to be copied as a destination. We will copy the file called “development-plan.md” from the remote computer to the current directory on the local computer.
scp [email protected]:/home/dave/Downloads/development-plan.md .
If you add a file name to the local path, the file is copied and given that name.
scp [email protected]:/home/dave/Downloads/development-plan.md ./dp-1.md
The file was copied but renamed to our specified file name.
ls -hl *.md
Copy multiple files
Copying multiple files in both directions is easy. You can specify as many source files as you want. Here we copy two Markdown files and a CSV file.
scp ./dp-1.md ./dp-2.md ./dp-3.csv [email protected]:/home/dave/Downloads/
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. [email protected]:/home/dave/Downloads/
Recursive directory copying
The -r
The (recursive) option allows you to copy entire directory trees with a single command. We placed two files in a directory called “data” and created a directory called “CSV” within the “data” directory. We placed the CSV file in the “data / CSV” directory.
This command copies the files and recreates the directory structure on the remote computer.
scp -r ./data [email protected]:/home/dave/Downloads/
Copy files between remote servers
You can even teach 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. Files are copied from the source server and copied to the location on the target server.
Although the syntax is simple, you need to think a little more to make sure everything stays in place. Obviously, the location to which you are trying to copy files to a remote server must be available to the user account you specify on the command line. And that 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, as well as between the source and target server. Make sure you can use SSH to log in to the target server from the source server. if you can’t scp
will not be able to connect.
Setting SSH keys so that you can use authenticated access, but passwordless access is by far the preferred method. Using passwords gets messy very quickly and – since you are asked for a password for each user account – prevents you from fully automating the process using a script.
We set the SSH keys for the user accounts we use on each remote server. This allowed seamless SSH access to the other server, for those two users. This allows us to transfer files in any direction, using these two user accounts.
To copy files from a “dave” user account on a Manjaro computer to a “dave” account on a Fedora computer, via scp
command issued from our local Ubuntu computer, the syntax is:
scp [email protected]:/home/davem/man. [email protected]:/home/dave/
We quietly returned to the command line. There is no indication that anything has happened. Working on the assumption that there is no news is not good news, scp
only reports bugs for this remote copy. By checking the Fedora computer we can see that the files from the Manjaro computer have been copied and received.
By default, files are copied directly from the source computer to the target computer. You can undo this using -3
(three-way) option.
With this option, files are transferred from the destination to the source, via your local computer. For this to happen, there must be seamless SSH access from your local computer to the target computer.
scp -3 [email protected]:/home/davem/man. [email protected]:/home/dave/
There is still no indication that anything has happened, even when you are channeling files through your local computer. Proof of the pudding is, of course, checking the target computer.
Other options
The -p
(save file attributes) will retain the original tags for creating, owning and accessing files on uploaded files. They will have the same metadata as the original files on the source computer.
If you see error messages, try the command again and use -v
(verbose) flag to see detailed information about the transfer attempt. You should be able to spot the point of failure in the output.
The -C
The (compress) option compresses files as they are copied and decompresses them when received. This is something that dates back to the era of slow modem communication between computers. Reducing the payload size could reduce transmission time.
Today, the time it takes to compress and decompress files is likely to take longer than the difference between compressed and uncompressed transfers. But because scp
is best used to copy files between computers on the same LAN, transfer speed should not be a big concern.
RELATED: How to back up your Linux system using rsync