Linux commands and shells »How to use scp command in Linux

Scp is a vital tool for any system administrator. Just like cp, its local counterpart, scp is used to copy files or directories. Unlike cp, scp works on systems that transfer files to or to a remote server, or even between remote servers. It also provides protection, encryption and optionally compressing files during transport.

Beginning of work

To complete this tutorial, you need the following:

  • 1 Node (cloud or dedicated server) with any Linux distribution with SSH installed.

Scp is a component of any full SSH installation as long as the server is running OpenSSH or another SSHD. Includes almost any virtual or dedicated server, as SSH is the primary access method for securely connecting to remote servers.

Textbook

Scp is a complex command, but the basics are easy to remember. This is the basic form of any challenge. Note that you can use the -P flag to specify the port to use.

scp source_file_path destination_file_path

Let’s say you are running SSHD on a non-standard port, which is good security practice. The scp utility connects to port 22 of the SSHD, typically. But you could use a different port:

scp -P 2222 source_file_path destination_file_path

Here are some examples of its use. Let’s say you want to copy /root/example.txt from localhost to remote server by placing it in / home / user folder.

scp /root/example.txt [email protected]:/home/user/

Now let’s download the example.txt file from the remote server to the local / root directory.

scp [email protected]:/home/user/example.txt /root/

In addition, multiple files can be specified. The command will download both foo.txt and bar.txt to the / home / user directory on the remote host.

scp foo.txt bar.txt [email protected]:/home/user/

You can also download entire catalogs. This is how you can download the contents of the / root / backup directory, recursively, into the / root / backup folder on the remote host.

scp -r /root/backup/ [email protected]:/root/backup/

Some files, such as text and binary files, are compressed in transit. If processor power allows and bandwidth is limited, the -c flag will transfer compressed data, unpacking at the other end.

scp -rC /root/backup/ [email protected]:/root/backup/

Sometimes, transferring the contents of a file is not enough. You can save other metadata such as file update times and modes. The -p flag is for this purpose.

scp -p /root/backup/ [email protected]:/root/backup/

Output

Regardless of performing a simple transfer or architecture of a comprehensive backup strategy, the scp command is essential for any good sysadmin. For the above reasons, it should be easy to create an scp command to do any of these tasks. If this guide was helpful to you, kindly share it with others who may also be interested.

Sidebar