1. Overview

Linux administrators often need to securely transfer important files and directories from local machines to servers or between remote servers. This can be achieved using the scp command, which uses encryption and transfers files and directories via SSH over a network.

In this tutorial, we’ll briefly discuss the purpose and usage of the scp command with examples to help understand how it works.

2. The scp Command

The scp (secure copy) command is a secure command line utility to copy files and directories between systems over a network. It comes pre-installed in the majority of Linux systems through the OpenSSH package.

Moreover, scp is based on the SSH protocol. This means it employs encryption and other network security concepts to enable users to transfer files over the network without worrying about network sniffing. In particular, the data is always transferred in an encrypted manner. However, we require credentials or a key to the destination system for authentication while transferring.

3. Syntax

The syntax for the scp command involves the source, destination, and non-mandatory options:

scp [options] [username1[:password1]@]source_file_or_directory [username2[:password2]@]remote_IP:destination_path

Let’s break down the syntax:

  • [options]: we can add one or more options or flags offered by scp to modify the behavior of file transfers
  • [username1[:password1]@][server1:]source_file_or_directory: path of the source file or directory on the local or remote machine (optional credentials and hostname)
  • [username2[:password2]@][server2:]remote_IP:destination_path: path of the target file or directory on the local or remote machine (optional credentials and hostname)

Next, let’s discuss some of the popular options offered by the scp command:

  • -r: recursively copy entire directories
  • -P: port number to connect the remote host
  • -p: preserve the attributes of the source file after copying
  • -v: display detailed debugging messages while transferring
  • -l: limit the bandwidth to a specific rate in Kilobits per second while sending a file

Of course, more complex scenarios may necessitate specialized options.

4. Basic Examples

Now, let’s explore some basic examples of the scp command to learn its use.

4.1. Copy File From Local Machine to Remote Server

We can transfer a local file to a remote server using the scp command without the need for any additional flags.

For instance, let’s run scp to copy a file named about-baeldung.txt from the local machine to a remote server with the username baeldung:

$ scp /home/baeldung-local/Documents/about-baeldung.txt [email protected]:/home/baeldung/
[email protected]'s password: 
about-baeldung.txt                            100%  248    40.6KB/s   00:00

For further clarification, let’s break down the above scp command:

  • /home/baeldung-local/Documents/about-baeldung.txt is the path to the source file
  • [email protected] represents the IP address and username of the remote server
  • /home/baeldung/ is the destination path on the remote server

Thus, the file is successfully sent to the remote server.

4.2. Copy File From Remote Server to Local Machine

We can copy a file from a remote server to the local server. To do so, we specify the remote server details as the source and the local destination path after that:

$ scp [email protected]:/home/baeldung/Desktop/remote-2-local.txt /home/baeldung-local/Documents
[email protected]'s password: 
remote-2-local.txt                            100%  581   749.0KB/s   00:00

Here, we can notice the remote-2-local.txt is sent from the remote server to the local directory.

4.3. Copy Multiple Files

Additionally, we can copy more than one file from the local server to the remote server by adding multiple source paths in the command:

$ scp /home/baeldung-local/Documents/work.txt /home/baeldung-local/Desktop/imp.txt [email protected]:/home/baeldung/Desktop/
[email protected]'s password: 
work.txt                                      100%  248   367.9KB/s   00:00    
imp.txt                                       100%   12     3.1KB/s   00:00

Evidently, the work.txt and imp.txt files have been successfully sent to the remote server’s Desktop directory.

4.4. Copy All Files

The scp command enables users to copy a directory recursively using the -r option.

To demonstrate, let’s use this flag to copy the transfer directory recursively to the remote server:

$ scp -r /home/baeldung-local/transfer [email protected]:/home/baeldung/Desktop/
[email protected]'s password: 
client-mails.txt                              100%  248   331.0KB/s   00:00    
ideas.txt                                     100%  452     1.0MB/s   00:00    
…

Hence, the transfer directory has been successfully uploaded to the Desktop of the remote server user.

5. Advanced Examples

Moving onward, let’s explore some advanced examples of scp command to discover part of its full potential.

5.1. Copy File via Specific Port

Sometimes, administrators configure a port for SSH connections other than the default port 22 for additional security. In such cases, the scp command facilitates sending files to a remote server via a specific port using the -P option:

$ scp -P 2223 /home/baeldung-local/expenses.ods [email protected]:/home/baeldung/Documents/
[email protected]'s password: 
expenses.ods                                  100%   30KB   7.1MB/s   00:00

Thus, the expenses.ods file was successfully uploaded to the remote server via port 2223.

5.2. Copy File in Quiet Mode

Additionally, the scp command enables users to copy data in quiet mode, which means the file is transferred without displaying a progress meter or errors using the -q flag:

$ scp -q /home/baeldung-local/command.txt [email protected]:/home/baeldung/
[email protected]'s password:

Hence, we can observe that the server sent the file without displaying any progress or information.

5.3. Copy File in Verbose Mode

Simultaneously, the scp command also supports a verbose mode using the -v flag, which means we receive explicit debugging messages about the progress while copying a file or a directory.

For example, we can view information about the progress of copying the expenses.ods file to a remote server using this scp command:

$ scp -v /home/baeldung-local/expenses.ods [email protected]:/home/baeldung/
Executing: program /usr/bin/ssh host 192.168.15.132, user baeldung, command scp -v -t /home/baeldung/
…
expenses.ods                                  100%   30KB   4.6MB/s   00:00    
…
debug1: Exit status 0

This option helps users and administrators debug problems in case of an unsuccessful transfer.

5.4. Specifying Bandwidth Limit While Copying

Further, we can use the -l option to specify the bandwidth limit in kilobits per second while copying a file or directory using the scp command.

For instance, we run this scp command to send a file to the remote server with a bandwidth limit of 400 Kilobits/s (400 Kbits/s ÷ 8 = 50 KB/s):

$ scp -l 400 /home/baeldung-local/command.txt [email protected]:/home/baeldung/Downloads
[email protected]'s password: 
command.txt                                   100%   82    26.9KB/s   00:00

Notably, we observe that the file command.txt is has been copied at a bandwidth of 26.9KB/s, which falls within the specified bandwidth limit of 50KB/s.

5.5. Speeding Up File Copy

Moreover, the scp command offers the -C option, which compresses files while sharing them over the network, resulting in potentially faster transfers:

$ scp -C /home/baeldung-local/passwords.txt [email protected]:/home/baeldung/
[email protected]'s password: 
passwords.txt                                 100%   12     4.3KB/s   00:00

This option may save time, especially when sending large files.

5.6. Copy File Using Specific Cipher

Although it uses the AES128 cipher by default to encrypt files during transfer, scp supports specifying other ciphers using the -c option.

Let’s send a file using the AES256-CTR cipher for encryption:

$ scp -c aes256-ctr /home/baeldung-local/passwords.txt [email protected]:/home/baeldung/
[email protected]'s password: 
passwords.txt                                 100%   12     1.9KB/s   00:00

This option enables users to determine the level and type of encryption they require when copying a file over the network.

5.7. Copy File With Original Attributes

The scp command preserves the original attributes of the source file after the transfer using the -p option:

$ scp -p /home/baeldung-local/expenses.ods [email protected]:/home/baeldung/
[email protected]'s password: 
expenses.ods                                  100%   30KB   2.9MB/s   00:00

After executing the above command. the transferred expenses.ods file retains the same source attributes, such as permissions, modification time, and access time.

5.8. Copy File From One Remote Server to Another

Lastly, the scp command enables users to send files from one remote server to another using the local server.

For this purpose, we specify the credentials or keys and IP addresses of both the source and destination remote servers:

$ scp [email protected]:/home/baeldung/Desktop/remote-2-remote.txt [email protected]:/home/mkubuntu/Desktop/

Upon successful execution, we don’t receive any output.

Of course, for all methods above, we should either have a preconfigured key for public key authentication or the credentials to any of the remote servers. Usually, keys are preferred, but they require extra configuration steps both locally and remotely.

6. Conclusion

In this article, we discussed the scp command for securely sharing files and directories between Linux systems over a network.

First, we explained the main purpose of the command. After that, we continued with basic examples to grasp its functionality. Finally, we covered advanced examples to explore the full potential of scp.