SCP command in Linux with Examples

Updated 29 October 2024

As a system administrator or Linux power user, you will regularly need to securely transfer data and backup files between local and remote systems or between two remote systems. The SCP command line utility will assist you in accomplishing this.

In this guide, I'll provide step-by-step instructions on the usage of this tool and in-depth descriptions of the most popular SCP command line options.

What is SCP Command?

SCP short for secure copy protocol is a file transfer network protocol that makes it simple and secure to copy files between two remote systems or between a remote system and a local host.

SCP employs the SSH (Secure Shell) protocol for authentication and encryption, ensuring that the information transmitted is secure from malicious actors.

SCP Syntax

Before we begin using the SCP (secure copy) command, let's go over the basic syntax.

The syntax for the SCP command is as follows:

plain text
SCP [options] [source username@source IP address]:/[directory and file name] [destination username@destination IP address]:/[destination directory]

Here is a breakdown of the syntax:

  • OPTIONS - parameters you can use with SCP command such as ssh port, the cipher used for encrypting the data transfer, IP version to use, and so on. We’ll discuss the commonly used ones later in the article.
  • [source username@ source IP address] username and the system's IP address with the file you want to copy.
  • :/[directory and file name] - the file name and directory where the file is located.
  • [destination username@destination IP address] - the destination username and IP address.
  • :/[destination directory] The destination directory to which the file will be copied.

SCP command Options

The SCP command has several command line options that change its behavior. The most commonly used ones are:

Linux SCP Command Examples

You now understand what the SCP command is and what it is used for. In the sections that follow, we will go over some practical examples of how to use this tool to transfer files.

1. Copy Data from local machine to remote server

In this example, we'll transfer the file log.txt to a specified remote user's home directory. In this case, our remote user is 'linux'.

Image from notes

You will be prompted to enter the user password of the remote server for the transfer process to begin. It will look like this:

Image from notes

Once the transfer process is started, you will see a progress meter that displays the transmission status.

When the filename is excluded in the destination path, the local file is copied to the remote server with the original name.  If you want to save the file to the remote server under a different name you can provide a new filename.

We copied the file log.txt to the remote host user's home directory and renamed it systemlogs.txt.

Image from notes

To recursively copy files or a directory and its subdirectories to the remote server, use the -r option.

Here is an example of copying a directory name logs to the remote system:

Image from notes

2. Copy Data from a remote server to a local host

If you want to copy files from a remote computer to a local host, you can simply achieve that by using the remote location as a source and the local host as the destination.

In this example, we'll transfer the remote file systemlogs.txt from a remote machine with the username Linux to our local host's current working directory which is denoted by the period(.):

Image from notes

It's also possible to recursively copy a directory and its subdirectories from a remote computer by making use of the -r option:

Image from notes

Executing the command will copy directory backups from the remote directory /home/linux to our local host user's home directory ~/. Notice that when copying files from a remote machine the progress meter will not be shown.

3. Copying Data Between Remote Servers

The advantage of using SCP for file transfers is that it allows you to connect to local systems and remote servers.

Let's say we want to copy the file backup.zip from one remote server to another. The command looks like this:

Image from notes

Before the transfer can begin, you will be prompted to enter the passwords for both remote hosts.

Image from notes

Normally, the file will be transferred directly from the first remote server to the second. If you want to route the traffic through your machine, use the -3 option.

Image from notes

4. SCP with a Port Number

The -P (upper case) argument can be used to specify a port if SSH on the remote server is set to  listen on a different port  other than the default port 22:

Image from notes

5. Copy Data without changing the timestamp

If you want to keep preserve file attributes such as the modification times, access times, and file mode bits of the original files use the -p (lower case) option.

Here the source file (original file) logs.txt attributes will not be changed:

Image from notes

Important Key takeaway points.

  • The SCP command relies on ssh to securely copy files, so it needs a password or secure shell keys (ssh keys) to encrypt files and authenticate on the remote systems.
  • You need to have root access or at least write permission on the target system and read permission on the source file to copy files.
  • When copying files that have the same name and location on both systems, be cautious because SCP will overwrite files without giving any warning.
  • SCP distinguishes between local and remote locations using the colon (:).

Conclusion

That's all there is to it; thank you for taking the time to read this article. In this article, we learned about the SCP command in Linux and how to use it to securely copy files between systems.  For more information visit the SCP man pages or simply type "man scp" from the terminal.

Spotted a mistake or want something added? Send me a note.