How to Use the SCP Command on Linux: : Tips and Tricks

 

Linux terminal on a laptop screen.
fatmawati achmad zaenuri/Shutterstock.com

The scp command makes copying recordsdata between Linux computer systems straightforward and safe. It makes use of SSH safety, however better of all, it’s easy. If you should use cp, you should use scp.

The Safe Copy Protocol and scp

Let’s outline a few phrases: there’s SCP and there’s scp. The uppercase SCP stands for the Secure Copy Protocol. The lowercase scp stands for safe cp. In different phrases, SCP is a protocol and scp is a program.

scp was designed to be a secure and safe technique of copying recordsdata between distant Linux computer systems. It makes use of SSH to determine safe connections. SSH, or safe shell, is a cryptographic community protocol usually used to entry and log in to distant Linux computer systems. On Linux distributions, SSH performance is supplied by OpenSSH.

SCP is considerably lengthy within the tooth, and issues have been aired regarding its use within the current day. Since OpenSSH model 8.8, SCP has been thought-about deprecated. Trendy implementations of scp default to utilizing the Safe File Switch Protocol by default. SSH continues to be used for the safe connection, however the file transfers are dealt with by SFTP. That is all invisible and occurs magically beneath the hood, and the scp syntax has remained the identical.

The rsync program is most well-liked over scp , however you could encounter a pc that doesn’t have rsync put in, and for which you don’t have root privileges which means you may’t go forward and set up it. For copying recordsdata from pc to pc on a self-contained community, scp is completely wonderful. For scp to work, you will need to have SSH operating on the entire computer systems you’ll be copying to and from.

To see the model of OpenSSH put in in your pc, sort:

ssh -V

Obtaining the version of OpenSSH

Copying a Single File

Like the usual cp command, scp copies recordsdata from the supply location to the goal location. To repeat a file to a distant pc, you will need to know the IP deal with or community title of the distant pc. You could even have the credentials for a consumer account that has write privileges for the placement you’re sending the file to.

To ship a file known as “pattern.txt” to a pc known as “fedora-34” on the native community, the syntax is:

scp ./pattern.txt [email protected]:/residence/dave/Downloads/

Copying a single file to a remote computer

The command is made up of:

  • scp: The scp command
  • ./pattern.txt: The file we’re going to ship. That is within the present listing.
  • dave@: The consumer account on the distant pc we’re going to ship the file to.
  • fedora-34.native: The community title of the distant pc.
  • :/residence/dave/Downloads/: The situation to repeat the file to on the distant pc. Be aware the colon “:” that separates the pc title and the trail.

You’ll be prompted to enter the password for the account on the distant pc, after which the file is copied.

If you would like the file to have a unique title on the distant pc, you may add a filename to the goal path. To repeat the identical file and have it named “different-file.txt”, use this syntax:

scp ./pattern.txt [email protected]:/residence/dave/Downloads/different-file.txt

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

The scp command will silently overwrite present recordsdata, so watch out whenever you’re copying recordsdata. If a file already exists on the goal pc with the identical title because the file you’re copying, will probably be overwritten and misplaced.

If the goal pc isn’t utilizing the default SSH port of twenty-two, you should use the -P (port quantity) possibility to supply the suitable port quantity.

Retrieving a Single File

To repeat a file from a distant server, merely put the distant server because the supply, and put the native path the place you need the file copied because the goal. We’re going to repeat a file known as “development-plan.md” from the distant pc to the present listing on the native pc.

scp [email protected]:/residence/dave/Downloads/development-plan.md .

Copying a single file from a remote server to the current directory of the local computer

For those who add a filename to the native path, the file is copied and on condition that title.

scp [email protected]:/residence/dave/Downloads/development-plan.md ./dp-1.md

Copying a single file from a remote server to the current directory of the local computer with a new name

The file is copied however renamed to our specified filename.

ls -hl *.md

Copying A number of Recordsdata

Copying a number of recordsdata in both path is simple. You may listing as many supply recordsdata as you want. Right here, we’re copying two markdown recordsdata and a CSV file.

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

Copying multiple named files to a remote computer

The three recordsdata are copied to the distant pc. You can even use wildcards. This command does precisely the identical factor because the final command.

scp ./dp. [email protected]:/residence/dave/Downloads/

Copying multiple files to a remote computer using wildcards in the filename

Recursively Copying Directories

The -r (recursive) possibility enables you to copy complete listing timber with a single command. We’ve positioned two recordsdata in a listing known as “information” and created a listing known as “CSV” contained in the “information” listing. We positioned a CSV file within the “information/CSV” listing.

This command copies the recordsdata and recreates the listing construction on the distant pc.

scp -r ./information [email protected]:/residence/dave/Downloads/

Copying a directory tree to a remote computer

Copying Recordsdata Between Distant Servers

You may even instruct scp to repeat recordsdata from one distant server to a different. The syntax is fairly easy. You present the account title and community deal with of the supply server and the account title and community deal with of the goal server. The recordsdata are copied from the supply server and copied to the placement on the goal server.

Though the syntax is straightforward, making certain all the things else is in place takes a bit extra thought. Clearly, the placement you’re attempting to repeat the recordsdata to on the distant server have to be accessible by the consumer account you specify on the command line. And that consumer account will need to have write permissions on that location.

A extra delicate prerequisite is that SSH entry have to be arrange between your native pc and the supply pc, and likewise between the supply and goal servers. Be certain that you should use SSH to log in to the goal server from the supply server. For those who can’t try this, scp gained’t be capable of join.

Organising SSH keys as a way to use authenticated however passwordless entry is by far the popular methodology. Utilizing passwords turns into messy in a short time, and—since you’re prompted for the password for every consumer account—it prevents you from totally automating the method with a script.

We arrange SSH keys for the consumer accounts we’re utilizing on every distant server. This supplied seamless SSH entry to the opposite server, for these two customers. This permits us to switch recordsdata in both path, utilizing these two consumer accounts.

To repeat recordsdata from the “davem” consumer account on a Manjaro pc to the “dave” account on a Fedora pc, through an scp command issued from our native Ubuntu pc, the syntax is:

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

Copying files from one remote server to another.

We’re silently returned to the command line. There’s no indication something occurred. Engaged on the premise that no information is sweet information, scp solely reviews on errors for this distant to distant copying. On checking the Fedora pc we are able to see that the recordsdata from the Manjaro pc have been copied and obtained.

Files from the Manjaro computer received on the Fedora computer

By default, the recordsdata are copied straight from the supply pc to the goal pc. You may override this utilizing the -3 (three-way) possibility.

With this feature, the recordsdata are transferred from the goal to the supply, by your native pc. For that to occur, there must be seamless SSH entry out of your native pc to the goal pc.

scp -3 [email protected]:/residence/davem/man. [email protected]:/residence/dave/

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

There’s nonetheless no indication something has occurred, even when channeling the recordsdata by your native pc. The proof of the pudding, after all, is to test the goal pc.

Different Choices

The -p (protect file attributes) will maintain the unique file creation, possession, and entry flags on the transferred recordsdata. They’ll have the identical metadata as the unique recordsdata on the supply pc.

For those who see error messages, strive repeating the command and use the -v (verbose) flag to see detailed details about the switch try. You ought to have the ability to spot the purpose of failure within the output.

The -C (compress) possibility compresses the recordsdata as they’re copied and decompresses them once they’re obtained. That is one thing that dates again to the period of gradual modem communications between computer systems. Lowering the scale of the payload may cut back transmission occasions.

These days, the time taken to compress and decompress the recordsdata is prone to take longer than the distinction between the compressed and uncompressed transmissions. However as a result of scp is greatest used to repeat recordsdata between computer systems on the identical LAN, transmission pace shouldn’t be a lot of a priority.

RELATED: Easy methods to Again Up Your Linux System With rsync

Leave a Reply

Your email address will not be published. Required fields are marked *