How to Use SCP Command to Securely Transfer Files in Linux 2023

How to Use SCP Command to Securely Transfer Files in Linux

Looking for a secure way to copy files and directories between two locations? Look no further than SCP (Secure Copy) – a powerful command-line utility that can handle all your file transfer needs. With SCP, you can easily and securely copy files and directories with just a few simple commands. Whether you’re transferring sensitive business data or just need to move some files between personal devices, SCP is the perfect solution.

SCP allows you to transfer files in three different ways: from your local system to a remote system, from a remote system to your local system, or between two remote systems from your local system. So whether you need to move files to or from a remote system, or even between two remote systems, SCP has got you covered.

SCP encrypts both the files and password during transmission, ensuring that sensitive information remains protected from prying eyes. In this tutorial, we’ll guide you through the basics of using scp with practical examples and comprehensive explanations of the most frequently used scp options. With our step-by-step guide, you’ll be able to transfer data quickly and safely.

SCP Command Syntax

To understand SCP command first let us understand some basic syntax.

The syntax for the scp command follows this pattern:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

The following command consists of:

  • “OPTION” field pertains to scp options like cipher, ssh configuration, ssh port, limit, recursive copy, and more.
  • [email protected]_HOST:]file1” field pertains to the source file.
  • [email protected]_HOST:]file2” refers to the destination file.

For local files, use absolute or relative paths, and for remote files, specify the user and host. With these guidelines in mind, you can use the scp command effectively.

SCP provides a number of options. Here are some of the most popular SCP options:

  • -P – lets you specify the SSH port for the remote host.
  • -p – preserves file modification and access times.
  • -q – suppresses the progress meter and non-error messages.
  • -C – compresses data as it’s sent to the destination machine.
  • -r – copies directories recursively.

By understanding these SCP options, you can fine-tune your file transfers to suit your specific needs. Whether you’re moving large directories or just a handful of files, these options can help ensure a smooth and efficient transfer process.

Requirements of using scp

If you want to copy files using scp, you need to authenticate on the remote systems with an ssh key or password. The scp command distinguishes between local and remote locations using a colon (:) separator. To copy files, you must have at least read permissions on the source file and write permissions on the target system. Be cautious when copying files that have the same name and location on both systems since scp overwrites files without warning. For large file transfers, it’s best to run the scp command inside a screen or tmux session.

How to Copy Files and Directories Between Two Systems with scp

There are several ways for copying a file to two different locations or systems on Linux, such as copying a local file to a remote system, copying a remote file to a local system, copy a file between two remote systems. In any of these cases, you can easily use the scp command to securely copy the file.

How to Use the scp Command to Copy a Local File to a Remote System

Run the following command to copy a file from a local to a remote system:

scp file.txt [email protected]:/remote/directory

Make sure to replace “file.txt” with the name of the file you want to transfer, “remote_username” with the username of the remote server, and with the IP address of the server. If you don’t specify a remote directory, the file will be transferred to the remote user’s home directory.

Once you run the command, you’ll be prompted to enter the user password, and the transfer process will begin.


[email protected]'s password:
file.txt                             100%    0     0.0KB/s   00:00

To copy a file with its original name, simply omit the filename from the destination location. However, if you wish to save the file with a different name, you must specify the new filename:

scp file.txt [email protected]:/remote/directory/newfilename.txt

To specify a port other than the default 22 for SSH on a remote host, use the -P argument. For instance, to transfer a file called “file.txt” to the directory /remote/directory on a remote host with IP address and SSH listening on port 2322, use the following command:

scp -P 2322 file.txt [email protected]:/remote/directory

If you want to copy a directory, the process is quite similar to copying files. However, there is one significant distinction – you must use the -r flag to indicate that the operation should be recursive. To transfer a directory from a local machine to a remote system, you can use the -r option with the scp command:

scp -r /local/directory [email protected]:/remote/directory

How to Use the scp Command to Copy a Remote File to a Local System

To copy a file from a remote system to a local one, simply use the remote location as the source and the local location as the destination. To illustrate, let’s say you want to copy a file called file.txt from a remote server with an IP address of In this case, you can use the following command:

scp [email protected]:/remote/file.txt /local/directory

Please note that if you haven’t set up a passwordless SSH login to the remote machine, you’ll be prompted to enter the user password.

How to Use the scp Command to Copy a File Between Two Remote Systems

If you’re looking for a file transfer solution without the need for logging in to one of the servers, consider using scp over rsync. With scp, you can transfer files directly from one remote machine to another, as in the command below:

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

This command will copy the file /files/file.txt from a remote host to /files directory on the remote host

Note that you’ll be prompted to enter the passwords for both remote accounts and the data will be transferred directly between the two remote hosts.

To direct your traffic through the machine where the command is executed, consider using the -3 option. You can implement it by typing the following command:

scp -3 [email protected]:/files/file.txt [email protected]:/files

By including the -3 option, you can conveniently transfer files from one remote host to another without having to download the files to your local machine first. This method can be particularly useful when you need to transfer large files or when you have limited local storage space.


In this comprehensive guide, you have discovered the step-by-step process of utilizing the powerful scp command to effortlessly duplicate files and directories.

Additionally, it’s highly recommended to configure an SSH key-based authentication method to avoid repeatedly entering passwords when connecting to your Linux servers. This simple setup will save you significant time and energy.

Furthermore, you can enhance your efficiency by streamlining your workflow by defining all your connections in the SSH config file, particularly if you frequently connect to the same systems.

If you have any questions related to this tutorial feel free to comment below.



Hi, this is James, a tech specialist and a core member of the TrioTeam. I like to research and write content about various interesting things especially tech-related stuff, and also have an interest in philosophical discussion. I have been writing solutions for technical problems, how-to tutorials, technology reviews, tools and websites, and so on for TrioGuide.

We will be happy to hear your thoughts

Leave a reply