SCP Command in Linux: How to Use It, with Examples

SCP Command in Linux: How to Use It, with Examples

The SCP command in Linux allows you to copy files over ssh connections. This is pretty useful if you want to transport files between computers.

Table of Contents

The scp (Secure Copy) command uses SSH to transfer data from one host to another and uses SSH’s same authentication and security. However, the command relies on SSH for data transfer, requiring an ssh key or password to authenticate on the remote systems.

When transferring data with scp, both the files and password are encrypted so that anyone snooping on the traffic doesn’t get anything sensitive. So, this is one of the most secure ways to transfer data on a network.

The scp command in Linux can be used in 3 ways:

  • To copy from a remote server to a local machine.
  • Also, to copy from a local machine to a remote server.
  • To copy from a remote server to another remote server.

SCP Command Syntax

The syntax for the scp command is:

scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2

Some of the most widely used scp command options include:

  • -P: Specify server SSH port
  • -p: Preserves permissions, modes, and access time of files (note the lower-case)
  • -q: Quiet mode, don’t display progress or messages
  • -C: Compress the data during transmission
  • -r: Recursive – include subdirectories and their contents
  • -i: Identity file or private key

How to Use the SCP Command in Linux

Transfer Local File to Remote Server

Copy file.txt from the current directory of the local system to the remote server’s /tmp directory.

scp file.txt [email protected]:/tmp/

Transfer File from Remote Server to Local Machine

The following command will copy /tmp/file.txt from the remote server to the local machine under the user’s home directory.

scp [email protected]:/tmp/file.txt /home/user

Transfer Local Directory to Remote Server Recursively

You can use the -r option in the scp command in Linux to recursively copy the entire directory from one system to another.

The following command will copy the /home/user/myfiles directory from the local machine to the remote server’s /tmp directory.

scp -r /home/user/myfiles [email protected]:/tmp/

Transfer Directory from Remote Server to Local Recursively

The following command will copy /tmp/serverfiles directory from the remote server to the local machine under the user’s home directory recursively.

scp -r [email protected]:/tmp/serverfiles /home/user

Transfer Multiple Files to Remote Servers

In the following example, the files file1.txt and file2.txt from the source host are copied to the remote server’s /tmp directory.

scp file1.txt file2.txt [email protected]:/tmp/

Increase Transfer Speed by Enabling Compression

You can increase the transfer speed by enabling the compression using the -C option. It will automatically allow compression at the source and decompression at the destination host.

The following command will copy the /home/user/myfiles directory from the local machine to the remote server’s /tmp directory recursively with enabled compression.

scp -r -C /home/user/myfiles [email protected]:/tmp/

Specify Different SSH Port

There can be cases where the SSH port is changed on the destination host, so using the scp command in Linux, you can specify the SSH port number using the -P option.

The following command will copy file.txt from the current directory of the local system to the remote server’s /tmp directory using port 2222.

scp -P 2222 file.txt [email protected]:/tmp/

Preserves Permissions, Modes, and Access Time of Files

Use the -p option in the scp command to preserve permissions, access time, and modes while copying files.

The following command will copy file.txt from the current directory of the local system to the remote server’s /tmp directory and will keep its properties.

scp -p file.txt [email protected]:/tmp/

Use Identify File in SCP Command

When using an SSH Key instead of a password during the SSH session, the -i flag allows you to select the file from which the identity (private key) for public-key authentication is read.

The following command will copy file.txt from the current directory of the local system to the remote server’s /tmp directory using the my_second_indent.pem private key file.

scp -i my_second_indent.pem file.txt [email protected]:/tmp/

Conclusion

In this tutorial, you learned how to use the scp command on Linux to copy files and directories. This is especially useful as a replacement for FTP, which is inherently insecure by default.

You may also want to set up an SSH key-based authentication and connect to your Linux servers without entering a password.

For more about the scp command in Linux, consult its manual page.

Bobby Borisov
Bobby Borisov

Bobby is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

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