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
cpYou can use
Secure copy protocol and scp
Let’s define a few terms: there is SCP and there it is
scp. SCP capital letters indicate Secure Copy Protocol. lowercase letters
scp means safe
cp. In other words, SCP is a protocol and
scp is a program.
scp was designed as a safe and secure means of copying files between remote Linux computers. It uses SSH to establish a secure connection. SSH, or secure shell, is an encrypted networking protocol that is often used to access and log on to remote Linux computers. It provides SSSS OpenSSH functionality on Linux distributions.
SCP is rather long in the teeth and concerns have been broadcast about its use today. As of OpenSSH 8.8, SCP is considered obsolete. Modern implementation
scp the Secure File Transfer Protocol is used by default. SSH is still used for secure connections, but file transfers are handled by SFTP. It’s all invisible and it’s happening magically under the hood
scp the syntax remained the same.
rsync the program takes precedence over
scp but you may come across a computer that doesn’t
rsync installed and for which you do not have root privileges, which means that you cannot install it. To copy files from a computer to a computer on a separate network,
scp is absolutely fine. For
scp for it to work, you must have SSH running on all the computers to and from which you will copy.
To view the version of OpenSSH installed on your computer, enter:
Copying a single file
scp will copy files from source location to 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 credentials for a user account that has write permission 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 are about to upload. This is in the current directory.
- dave @: The user account on the remote computer to which we are going to send the file.
- fedora-34.local: The network name of the remote computer.
- : / home / dave / Downloads /: The location to which the file is to be copied on the remote computer. Notice the colon “:” that separates the computer name and 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 a file name to the destination path. To copy the same file and name it “different-file.txt”, use this syntax:
scp ./sample.txt [email protected]:/home/dave/Downloads/different-file.txt
scp the command silently overwrites existing files, so be careful when copying files. If a file with the same name as the file you are copying already exists on the destination computer, it will be overwritten and lost.
If the destination computer does not use the default SSH port 22, you can use it
-P (port number) and enter the appropriate port number.
Loading a single file
To copy a file of remote server, simply specify the remote server as the source and enter the local path where you want to copy the file as the destination. Copy the file named “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 will be copied and assigned that name.
scp [email protected]:/home/dave/Downloads/development-plan.md ./dp-1.md
The file is copied but renamed to our specified file name.
ls -hl *.md
Copying multiple files
Copying multiple files in both directions is easy. You can specify any number of source files. 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/
These three files are copied to the remote computer. You can also use wildcards. This command does exactly the same as the last command.
scp ./dp. [email protected]:/home/dave/Downloads/
Recursive directory copying
-r The (recursive) option allows you to copy entire directory trees with a single command. We placed the two files in a directory named “data” and created a directory named “CSV” in 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 instruct
scp to copy files from one remote server to another. The syntax is quite simple. You enter the account name and network address of the source server and the account name and network address of the destination server. The files are copied from the source server and copied to a location on the destination server.
Although the syntax is simple, making sure everything else is in place requires a little more thinking. Obviously, the location to which you are trying to copy files on the remote server must be accessible by the user account that you specify on the command line. And this user account must have write permission to this location.
A finer assumption is that SSH access must be set up between your local computer and the source computer, as well as between the source and destination servers. Make sure that you can use SSH to log on to the destination server from the source server. If you can’t
scp it will not be possible to connect.
Setting up SSH keys so that you can use authenticated access without a password is by far the preferred method. Using passwords very quickly becomes complicated and – because you are prompted for a password for each user account – prevents you from fully automating the process using a script.
We have set up SSH keys for the user accounts we use on each remote server. This ensured seamless SSH access to the second server for these two users. This allows us to transfer files in both directions using these two user accounts.
To copy files from the “crowd” user account on the Manjaro computer to the “dave” user account on the Fedora computer,
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. We are working on the assumption that no news is good news,
scp reports only errors of this remote copy. When we check the Fedora computer, we 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 destination computer. You can overwrite this with
-3 (three-way) variant.
This option transfers the files from the destination to the source through your local computer. For this to happen, SSH must be seamlessly accessed 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 is happening, not even when routing files through your local computer. The proof of the pudding is, of course, the control of the target computer.
-p (preserve file attributes) retains the original flags of file creation, ownership, and access to transferred files. They will have the same metadata as the original files on the source computer.
If you receive error messages, try the command again and use the command
-v (verbose) flag displays detailed information about the transfer attempt. You should be able to recognize the point of failure in the output.
-C (compress) compresses files when they are copied and decompresses them when they are received. This is something that dates back to the era of slow modem communication between computers. Reducing the payload could shorten the transfer 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, the transfer rate should not be too important.
RELATED: How to back up Linux using rsync