How to Use SCP Command to Securely Transfer Files + Examples

How to Use SCP Command to Securely Transfer Files + Examples

SCP (secure copy) is an advanced version of the CP command, and allows you to securely copy files or directories between two machines via SSH.

SCP Command Uses

  • Copy a file or a directory from a local system to a remote system.
  • Copy a file or directory from a remote system to your local system.
  • Copy a file or a directory between two remote systems from your local system.

To copy or transfer files and folders, you must have the user’s credentials of the remote hosts.

In this article, we are going to explore how to use the scp command to securely transfer files and folders, using practical examples and beginner friendly explanations.

In our case, we will use a Ubuntu 20.04 server as our remote host.

SCP Command syntax

SCP provides several options. The most used options are :

  • -p: Preserves files modification and access time
  • -P: Specifies the remote host ssh port.
  • -q: This option is used to suppress the progress meter and non-error messages.
  • -r: Used to copy directory or directories recursively.

The basic syntax of the SCP command is:

scp file_to_copy [email protected]_address:/destination_path

Let’s discuss the command above in detail:

  • file_to_copy: That is the name of the file that you want to copy on your local computer to the remote host.
  • user: That is the username of the remote host
  • destination_address: This refers to the IP address of the remote host
  • Destination_path: This is the path where you want to store your file on the remote server.

Below is a practical syntax of the SCP command in use. We are copying the “Alien_Movie.mp4” file from our local computer to John’s computer via a network.

scp Movie.mp4 [email protected]:/home/edxd/Desktop/Movies

1. Copy a file from your local computer to a remote host

It is one of the most straightforward SCP commands you can carry out. We are copying a single file (example.txt) in our local computer to our remote host. We will use the command below.

scp example.txt [email protected]:/home/stelixd

On the image below, on the left, we have the Terminal of our local computer, and on the right, we have the console of the Ubuntu server virtual machine. If you are connecting to your remote host for the first time, you will see the prompt Are you sure you want to continue connecting (Yes/no)?. Type Yes and hit Enter.

When we execute the ls command on the server, you can see the example.txt was successfully copied.

2. Copy Multiple Files

Assume we had multiple files on our local computer that we wanted to copy to our remote host – say movie1.mp4, movie2.mp4, movie3.mp4, and movie4.mp4. We will simply include all three files in our SCP command, as shown below.

scp movie1.mp4 movie2.mp4 movie3.mp4 movie4.mp4 [email protected]:/home/stelixd

3. Copy a Directory (Recursively)

To copy a directory and its contents, we will include the -r (recursive) parameter in our SCP command. Let’s copy the Example directory on our local computer to our remote host.

scp -r transfer_folder [email protected]:/home/stelixd

4. Copy Files (Verbose Mode)

By enabling verbose mode with the SCP command, you can see everything happening behind the scenes. For this example, let’s copy three files to our remote host –  movie1.mp4, movie2.mp4, movie3.mp4 and movie4.mp4. We will use the command below:

scp -v movie1.mp4 movie2.mp4 movie3.mp4 movie4.mp4 [email protected]:/home/stelixd

In the image above, you can see the SCP command prints output of all the processes running in the background, including how it tries to establish an SSH session.

5. Copy Files Across Remote Hosts

We have looked at copying files from your local computer to a remote host in previous examples.

Now think of a situation where you want to copy files from one remote host to another.

To do this you can run:

scp [email protected]:/home/edxd/my_file [email protected]:/home/stelixd

Route Traffic Through Computer Issuing Command

We will need to use the -3 option that instructs SCP to route traffic through the PC on which the command is issued, even though it is a 3rd party to the transfer.

In our case, we would execute the command below:

scp [email protected]:/home/edxd/my_file [email protected]:/home/stelixd

6. Compress and Copy Files

If you are copying a large file or multiple files over a slow network, you can use the -C parameter, which will enable SCP to compress the files before transferring them over the internet. That can also greatly save on your bandwidth if bandwidth is an issue.

On reasonably fast connections, this might just slow things down, however.

Note: We have included the -v (verbose) parameter in the command below to see what is happening behind the scenes.

scp -vC example_file [email protected]:/home/stelixd

7. Limit Bandwidth Usage

When you’re copying a file from your local computer to a remote host, think of it as an upload process. Now, if the upload bandwidth used by the SCP command is high, it might impact the network or other processes and devices also uploading data.

Luckily, we can use the -l parameter and specify the bandwidth used by the SCP command in Kilobits/sec (kbps).

Important: Even though we specify the transfer rate in Kilobits/sec (kbps), the transfer rate will be displayed in Kilobytes/sec (KB/s). A Kilobyte is made up of 8 Kilobits. So when we specify, say scp -l 1600, we’re saying we want a transfer rate of 1600 Kilobits, which in KB/s means 1600 / 8 = 200 KB/s.

As another example, if you want to limit transfer to 300 KB/s, you multiply 300 * 8 = 2400 Kilobits. So we run scp -l 2400.

In the command below, we will set our SCP transfer bandwidth to 800 Kb/s (800 * 8 = 6400 Kilobits/sec).

scp -l 6400 example_file [email protected]:/home/stelixd

8. Specify Port Number

By default, SSH uses port 22. For security reasons and other related measures, some people prefer changing this default port and configuring a custom port to work with the SSh service. You will need to specify the custom SSH port in your SCP command using the -P parameter in such a situation.

We haven’t configured any custom port in our case, and we will just enter port 22.

scp -P 22 example_file [email protected]:/home/stelixd

9. Set a Different Cipher

To copy files securely over a network, SCP uses the AES encryption algorithm. To specify a different algorithm, use the -c parameter. For this example, we will use the aes128-ctr cipher.

scp -c aes128-ctr example_file [email protected]:/home/stelixd

10. Automate File Transfers Using a SCP Shell Script

If you have files or directories that you copy regularly, you can create a simple bash script and save the time of writing those long commands on the terminal.

There are various ways you can write scripts to use the SCP command.n this example I’ll create a simple script that requests a target file or directory that I manually input, then it loops through some destinations in a file (that I’ve set beforehand), and transfers the file I entered to those destinations.

I created a file called destination_address.txt in the /tmp directory. This text file will hold the [email protected]_address:/destination of our remote host.

In this case, I want to transfer files to my remote server, in the ~/remote_transfers folder.

[email protected]:~/remote_transfers/

Now let’s create our bash script:

echo "=================="
echo "Running SCP Script"
echo "=================="
echo -e "Enter the path to the file you wish to copy:\c"
read file

for dest in `cat /tmp/destination_address.txt`; do
scp -r $file ${dest}

done

We will save our script as scp_script.sh. To make the script executable, remember to run the command chmod +x scp_script.sh.

To run it I’ll run::

sudo bash scp_script.sh

Conclusion

This post has given you a detailed guide on using the SCP command on Linux to transfer files from your local computer to a remote host. We have only looked at 10 SCP commands. If you have any questions or requests, then feel free to let us know in the comment section.

1 Shares:
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
danny
danny
11 days ago

what console do you use?

You May Also Like
Rm Command in Linux
Read More

Rm Command in Linux

The rm command is a very important command used to remove files and directories in Linux. The rm…