Introduction

The SCP or Secure copy command is a method of encrypting the transmission of files between systems. It’s a variant of the cp, or copy command, with the “S” standing for secure.

SCP includes encryption over an SSH (Secure Shell) connection. This ensures that even if the data is intercepted, it is protected.

SCP can:

  • Copy files within the same machine
  • Copy files from a local host to a remote host
  • Copy files from a remote host to a local host.
  • Copy files between two different remote servers.

This guide will walk you through how to transfer or copy files using the SCP command.

Prerequisites

  • A secure shell login on the server
  • (optional) Root access on both the client and server
  • A secure shell login on the server system

How to Securely Copy Files Using SCP

Copy File From Local to Remote Server with SCP

The SCP command allows the use of wildcards.

Use the tilde character ~/ to stand for the user’s home/user directory.

You can specify a string of text with the * sign.

For example: /~/*.txt

This would direct SCP to copy all files in the home directory that end in .txt.

Typically, you do not need to specify the location for a file in your current directory. If you are in your /home/user directory, and you want to copy the test.txt file to the server, you can enter the following:

scp test.txt username2@destination:/location2

To copy all .txt files into username2’s home directory, enter the following:

scp *.txt username2@destination_host:/~/

If you only specify the destination directory, SCP will leave the filename as-is.

To change the filename: Define a new filename in the destination:

scp test.txt username2@destination_host:/user/home/user1test.txt

This example copies the test.txt file from the local machine, then save it as user1test.txt in the user directory of the destination system.

If a remote system is configured to listen to SSH requests on a port other than the default Port 22, Use the –P switch to specify the port:

scp –P 1234 test.txt user2@destination_host:/location2/

This copies test.txt from your local system to the destination host using Port 1234.

Copy From One Remote Host to Another

You aren’t limited just to connecting between your local machine and a remote server.

To copy from one remote system to another:

scp user1@host1.com:/files/test.txt user2@user2.com:/files

This would replicate the test.txt file from the /files directory on host1.com to the /files directory on /host2.com. (The system will prompt you to enter the password for user1 and user2 before the operation can complete.)

Copying Large Files With SCP

If you’re copying large files, run the command in a terminal multiplexer (like screen or tmux).

If the operation is interrupted, the multiplexer will allow you to resume the copy without having to start over.

You can check whether your system has screen installed by running the following in a terminal:

screen ––version

SCP Usage Considerations

The SCP command does not check the destination location before writing. Any files in the destination with the same name will be overwritten without notification.

You will be prompted for a password when you hit enter.

Use the password for the user on the remote system(s).

Managing Permissions

On the source system, you’ll need to use an account with read access to the file(s) you want to copy.

On the destination system, you’ll need to use an account with write access to the directory where the file(s) will be saved. If you run into errors while copying, you can try a root user account to troubleshoot permissions.

SCP Command Examples

The command to use SCP is:

scp [options] username1@source_host:/location1/file1 username2@destination_host:/location2/file2

Some common options include:

  • –P – Specify server SSH port
  • –p – Preserve the timestamp for modification and access (note the lower-case)
  • –q – Quiet mode, don’t display progress or messages (will still show errors)
  • –C – Compress the data during transmission
  • –r – Recursive – include subdirectories and their contents

The section immediately following the options is the source of the file you want to copy. You can copy from your system to the remote system, or vice-versa.

The next section specifies the location the file is being copied to.

For example:

scp user@local_system:/home/user/test.txt admin@remote_system:/home/user

This would copy the test.txt document from the user directory on the local system, and place a copy in the admin’s user account directory on the remote system.

Another SCP Example:

To copy a file from a remote host to a local host:

scp user@from_host:file.txt /local/directory/

Conclusion

In this guide, you have learned what the SCP Command is and how to use it to secure the transmission of files.

This is especially useful as a replacement for FTP, which is inherently insecure by default. The secure copy protocol command also follows regular command-line and SSH functionality, helping to create a seamless command set for managing files between Linux machines.