Cantech Knowledge Base

Your Go-To Hosting Resource

How to Use the SCP Command in Linux?

Introduction to SCP

The SCP (Secure Copy Protocol) method helps securely transfer files between computers, especially in networks where security is important. It uses encryption to protect the data during the transfer so no one can tamper with it. Even if someone tries to intercept the data, they won’t be able to read it.

It uses the SSH (Secure Shell) protocol to transfer files securely by authenticating both the sender and the receiver. Thus, only authorized users can access the files. Once the connection is made, no one can change or view the data. It also eliminates the need to enter passwords every time.

SCP is also relatively easy to use and does not require much setup. Another advantage is that you can transfer multiple files at once. It can even copy entire directories.

This article will give a solid understanding of how to use the scp command to transfer files and directories securely between local and remote servers. You can also combine various options to make file transfers more efficient, secure, and customized to your needs.

Getting Ready: Setting Up the Environment

Firstly, set up your environment and then we will get into the technical details of using scp. We will work with two Ubuntu 24.04 instances, but you can follow the instructions for any Linux-based system.

Before starting with the scp command –

  • Deploy Two Ubuntu Instances

    o Set up two Ubuntu 24.04 instances (one as local machine, and the other will act as the remote     server)
    o You will need to make sure the limited user login option is enabled on both machines.

  • SSH Access

    o Access both instances using SSH (Secure Shell) so you can control the remote machine from         your local machine.

  • Update Your System

Your system must always be up-to-date. Run the following commands on both servers to update them –

sudo apt update && sudo apt upgrade

Your setup is complete! Now, you can start using scp.

The Basic Syntax of the SCP Command

The syntax of the scp command could seem a bit difficult at first, but it is actually quite simple once you understand it. See the general structure below.

scp [options] [source] [destination]

Here,

[options] part is optional and includes flags that modify the behavior of the command.
[source] means the file or directory that you want to copy.
[destination] is the location where you want to copy the file or directory.

Let us get to know various SCP Command Options

You can use them to control how files are transferred. Some of the most common options include:

  • -P

Specifies the port on the remote server to use.

  • -p

This option preserves the original file’s timestamps, access times, and permissions.

  • -r

Copying a directory? You can recursively copy all its contents with this option.

  • -q

Means quiet mode. It suppresses non-error messages, so you only see what is important.

  • -C

To enable compression and help speed up the process.

  • -l

Limits the transfer rate in kilobits per second.

  • -v

Displays detailed information and helps with debugging (Verbose mode)

  • -i

Specifies the path to a private key file when you are using SSH key authentication.

Setting Up with Sample Files and Directories

Now, let’s test the scp command with some sample files and directories on both your local and remote servers.

=> On Your Local Server

  • Create three .txt files –
touch file1.txt file2.txt file3.txt

Create a directory named dir

mkdir dir

=> On Your Remote Server

  • Create three .txt files
touch file4.txt file5.txt file6.txt
  • Create two directories – dir1 and dir2
mkdir dir1 dir2

Now, we have some files and directories. Next, we will transfer them.

Transferring Files and Directories with SCP

=> Copying Files from Local to Remote Server

Use the below command –

scp file1.txt username@remote_host:/path/to/destination

Here,

file1.txt is copied from your local machine to the remote server’s specified directory. By default, it will go to the remote user’s home directory if the directory is not specified.

Type ‘yes’ and confirm the remote server’s authenticity when prompted. Enter your password if required.

=> Copying Files from Remote to Local

You can use this command –

scp username@remote_host:/path/to/file4.txt /local/destination

This command securely copies file4.txt from the remote server to your local machine.

=> Copying Directories

You can use -r option to recursively copy entire directories (with all files and sub-directories).

  • Copy a directory from your local machine to the remote server with the below command –
scp -r dir username@remote_host:/path/to/destination
  • To copy a directory from the remote server to your local machine –
scp -r username@remote_host:/path/to/dir1 /local/destination

Using SCP with SSH Key Authentication

scp uses password-based authentication, but for increased security and convenience, you can set up SSH key-based authentication. This way, you can transfer files without entering a password each time.

=> Generating an SSH Key Pair On Your Local Machine

ssh-keygen -t rsa -b 4096

This creates a public and private key pair. Accept the default location or specify a custom location to store the keys.

=> Copying the Public Key to the Remote Server

Use the ssh-copy-id command as below –

ssh-copy-id username@remote_host

Thus, you can now SSH into the remote server without being asked for a password.

Transferring Files Using SCP with SSH Key Authentication

As the SSH key authentication is set up, you can now transfer files more easily.

Example –

Copy file2.txt from your local server to dir2 on the remote server with the below command –

scp file2.txt username@remote_host:/path/to/dir2/

(You will not be prompted for a password.)

=> Copying Directories with SSH Key Authentication

You can also recursively copy entire directories with SSH key-based authentication:

scp -r username@remote_host:/path/to/dir2 /local/destination

This command will copy the entire dir2 directory from the remote server to your local machine without needing to enter a password.

Advanced SCP Options

Below are some of the advanced options that give you more control over your transfers –

-p option to copy a file and preserve its modification time, access time, and permissions –

scp -p file3.txt username@remote_host:/path/to/destination

=> Enabling Compression

-C option to enable compression to speed up file transfers (especially for large files) –

scp -C file1.txt username@remote_host:/path/to/destination

=> Limiting Bandwidth

Transferring a large file and want to limit the bandwidth usage? Use the -l option followed by the bandwidth limit in kilobits per second –

scp -l 500 file2.txt username@remote_host:/path/to/destination

=> Running SCP in Verbose Mode

Use the -v option to run scp in verbose mode to debug or get detailed information about the transfer –

scp -v file3.txt username@remote_host:/path/to/destination

=> Using a Specific Private Key

With multiple SSH keys, the -i option can specify a particular private key for authentication –

scp -i /path/to/private_key file1.txt username@remote_host:/path/to/destination

The below command shows the default SSH private key location when you run it in user home directory

$ ls -l /.ssh/

=> Transferring Files Over a Custom Port

Use the -P option to specify the port numbers with the remote server running SSH on a custom port,

scp -P 22 file3.txt username@remote_host:/path/to/destination

=> Use tar and scp to transfer archived files.

Compress the dir directory and send it to a remote server using this command over SSH –

$ tar czf - dir | ssh linuxuser2@remote_server 'tar xzf - -C /home/linuxuser2/dir1

The remote server extracts the files into dir1 inside the home directory. This method makes file transfer faster and more efficient.
SCP have various benefits but it also has some limitations. It does not have as many features as some other file transfer protocols like SFTP. You cannot pause or resume a transfer. So, with the connection lost during the transfer, the process will stop. You will have to start over.

Conclusion

SCP is a reliable and secure way to transfer files between computers. It is a solid choice for quick, secure file copying between systems. Managing files and transferring them between remote servers is a critical task that sysadmins, developers, and users have to do frequently in Linux. The scp (secure copy) command stands out due to its ability to securely move files over a network. It ensures that data integrity is maintained during the transfer process. cp only works locally, but scp can transfer files between local and remote machines with an encrypted connection.

April 22, 2025