HomeTechnologyNewsHow to use the scp command in Linux

How to use the scp command in Linux

- Advertisement -
- Advertisement -
- Advertisement -
- Advertisement -

fatmawati achmad zaenuri / Shutterstock.com

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 cpyou can use scp.

The secure copy protocol and scp

Let’s define a couple of terms: there is SCP and there is scp. The capitalized SCP stands for Secure Copy Protocol. the lowercase scp means safe cp. In other words, SCP is a protocol and scp it’s a program

scp it was designed to be a 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, OpenSSH provides SSH functionality.

SCP is an old thing, and concerns have been raised about its use today. As of OpenSSH version 8.8, SCP is considered deprecated. modern implementations of scp default to using Secure File Transfer Protocol by default. SSH is still used for the secure connection, but file transfers are handled by SFTP. All of this is invisible and happens magically under the hood, and the scp the syntax has remained the same.

the rsync The program is preferred to scp but you might find a computer that doesn’t have rsync installed, and for which you don’t have root privileges, which means you can’t go ahead and install it. To copy files from one computer to another on a standalone network, scp it’s perfectly fine for scp for this to work, you must have SSH running on all the computers you will copy to and from.

To see the version of OpenSSH installed on your computer, type:

ssh -V

Get the version of OpenSSH

Copy a single file

as the standard cp domain, scp copy files from font location to objective Location. To copy a file to a remote computer, you must know the remote computer’s IP address or network name. You must also have the credentials of a user 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/

Copy a single file to a remote computer

The command is composed of:

  • scp: The scp command
  • ./sample.txt: The file we are going to send. This is in the current directory.
  • David @: 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 copy the file to on the remote computer. Note 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 a filename 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

Copy a single file to a remote computer with a new name

the scp The command will silently overwrite existing files, so be careful when copying files. If a file already exists on the destination computer with the same name as the file you are copying, it will be overwritten and lost.

If the target computer is not using the default SSH port of 22, you can use the -P (port number) option to provide the appropriate port number.

Single File Recovery

To copy a file of a remote server, just put the remote server as the source and put the local path where you want the file to be copied as the destination. We are going to copy a 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 .

Copy a single file from a remote server to the current directory on the local computer

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

Copy a single file from a remote server to the current directory on the local computer with a new name

The file is copied but renamed to our specified file name.

ls -hl *.md

Copy multiple files

Copying multiple files in any direction is easy. You can list as many source files as you like. Here, we are copying two markdown files and a CSV file.

scp ./dp-1.md ./dp-2.md ./dp-3.csv [email protected]:/home/dave/Downloads/

Copy multiple named files to a remote computer

All 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/

Copy multiple files to a remote computer using wildcards in the file name

Recursive copy of directories

the -r The (recursive) option allows you to copy entire directory trees with a single command. We put two files in a directory called “data” and create a directory called “CSV” inside the “data” directory. We put 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 [email protected]:/home/dave/Downloads/

Copy a directory tree to a remote computer

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 provide the account name and network address of the source server and the account name and network address of the destination server. Files are copied from the source server and copied to the destination server location.

Although the syntax is simple, making sure everything else is in place requires a bit more thought. Obviously, the location you’re trying to copy the files to on the remote server must be accessible to the user account you specify on the command line. And that user account must have write permissions to 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 destination servers. Make sure you can use SSH to login to the destination server from origin server. If you can’t do that, scp will not be able to connect.

Setting up SSH keys so you can use authenticated access but without a password is by far the preferred method. Using passwords gets complicated very quickly, and because you are 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 use 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 a scp command issued from our local Ubuntu computer, the syntax is:

scp [email protected]:/home/davem/man. [email protected]:/home/dave/

Copy files from one remote server to another.

We quietly return to the command line. There is no indication that anything has happened. Starting from the premise that the absence of news is good news, scp it only reports errors for this remote-to-remote copy. Checking the Fedora computer we can see that the files from the Manjaro computer have been copied and received.

Files from Manjaro computer received on Fedora computer

By default, files are copied directly from the source computer to the destination computer. You can override this using the -3 (three way) option.

With this option, the files are transferred from the destination to the source, through your local computer. For that 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/

Copy files from one remote server to another, through the local computer

Still no indication that anything has happened, even when funneling the files through your local computer. The proof of the pudding, of course, is to verify the target computer.

Other options

the -p (preserve file attributes) will keep the original file creation, ownership, and access marks on transferred files. They will 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) to view detailed information about the transfer attempt. You should be able to detect the point of failure at the output.

the -C The (compress) option compresses files as they are copied and decompresses them when they are 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.

Today, the time required to compress and decompress files is likely to be greater than the difference between compressed and uncompressed streams. But why scp best used for copying files between computers on the same LAN, transmission speed shouldn’t be much of a concern.

RELATED: How to backup your Linux system with rsync

- Advertisement -
- Advertisement -
Stay Connected
Must Read
- Advertisement -
Related News
- Advertisement -
%d bloggers like this: