15 ways to use the SCP command to transfer data in Linux

If you have used the Linux CP command to copy files and directories, then you will find SCP not much different. The main additional parameter is the remote destination host-name. We dig deep into the best possible ways to effectively use the SCP command while transferring the data between the systems.

Generally, when you want to copy files and directories from one server/localhost machine to another, you need to use file-sharing services like Samba(SMB) or NFS. Even though they are reliable, it is a tedious process to use when copying one time files.

That is because you need to set up and configure a Samba or NFS server. That brings forth the need to understand and use the SCP Linux command.

SCP (Secure Copy) command is a Linux utility used to copy files/directories between hosts securely. It uses SSH (Secure Shell) to transfer and authenticate users. Therefore just like when using SSH, you are required to know the credentials of your remote hosts.

If you are well-versed with the Linux CP command used to copy files and directories, then SCP is not much different. The only added and useful parameter is the remote destination host-name. Take a look at the syntax below.

scp file_name_to_copy username@destination_host:destination_directory_path

In the destination_host parameter, you need to enter the IP address of your remote host. If you don’t know how to find it, run the ipconfig command on the terminal.

Best ways to use the SCP command in Linux

In this article, we are going to look at 15 useful SCP commands you can use in your day to day interaction with the Linux systems. We shall use Ubuntu 18.04 LTS Server virtual machine as our remote host.

1. Copy a single file from the local computer to a remote host

It is one of the most straightforward commands. I have a file called Hello.scp on our local computer, that we are going to copy to our remote host. See the command below.

scp Hello.scp tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copy a single file from the local host to a remote host
Copy a single file from the local host to a remote host

On the right side, we have our localhost, and on the left is our Ubuntu Server. If you are doing this for the first time, you should see a warning asking you whether you want to continue connecting, type ‘yes.’ You should see a prompt to enter your remote host password. By executing an ls command on the remote host, we see we have copied our file successfully.

2. Copy multiple files

In the previous command, we have seen how we can copy a single file. To copy multiple files, use the syntax in the command below. We are going to copy three files, Hello1, Hello2, and Hello3.

scp Hello1 Hello2 Hello3 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copy multiple files using SCP
Copy multiple files

By running ls command on the remote host, we see we have successfully copied the files.

3. Copy a directory recursively

Other than files, we can use SCP to copy a directory from the local computer to a remote destination. Use the syntax in the command below. We are going to copy the folder, FOSSTUTS.

scp -r FOSSTUTS tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copy a directory using SCP command
Copy a directory using SCP command

Note, when performing any action on an entire directory, we will need to include the -r parameter, which stands for recursive.

4. Copying files with the verbose output

Including a verbose parameter in our SCP syntax prints all the process SCP is doing in the background. Let’s copy multiple files to our remote host using the -v (verbose) parameter.

scp Hello1 Hello2 Hello3 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copying files with SCP with the verbose output
Copying files with the verbose output

From the above image, you can see we have much output from the command we ran on our local computer. It gives you all the details concerning the operation carried out currently.

5. Copy files across two remote hosts

Assuming you are managing multiple remote servers and you want to copy a file from one server to another. Look at the syntax in the command below.

scp tuts@192.168.43.96:/home/tuts/FOSSLINUX/Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copy files across two remote hosts
Copy files across two remote hosts

We are copying the file Hello1 from the host 192.168.43.96 to the host 192.168.83.132.

6. Compressing and copying files

To speed up the copying process and save on bandwidth, you can compress your files with the -C parameter. The data is compressed on your local computer on the fly and decompressed on the remote host. See the syntax below.

scp -vC Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Compress and copy files using SCP command
Compress and copy files

We have included the -v (verbose) parameter so that you can see what is taking place.

7. Managing the bandwidth

Copying a file to a remote host can be explained as an upload. If the upload bandwidth is high, it might affect other processes running in the background. You can limit the bandwidth to use in the copying process using the -l parameter. See the command below.

scp -l 100 Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Manage bandwidth with SCP
Manage bandwidth

The bandwidth you assign is in kilobytes. Therefore, in the above command, we are using a maximum of 100Kb/s to carry our copy process.

8. Use a custom port numbering

Like explained above, SCP uses SSH to carry out the process of copying files. By default, SSH runs on port 22; however, if you configured an SSH server on the remote server to run on a different port, you can specify this in your SCP command. See the command below.

scp -P 22 Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Use a custom port number with SCP
Use a custom port number with SCP

Since I have not configured any SSH server, we leave the port number to 22 for this post.

9. Copying and preserving file attributes

In case you want to copy a file and preserve attributes like permissions, modification times, access times, etc., use the -p parameter with the SCP command. See the command below.

scp -P 22 Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copy and preserve file attributes using SCP command
Copy and preserve file attributes

Note, in this example, the ‘p’ is a lower case, unlike when using a custom port number.

10. Suppress SCP output with the -q parameter

Suppose you don’t want to print the SCP output, error notifications, warning, or even the progress meter. You can achieve it using the -q parameter, which suppresses all SCP output.

scp -q Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Suppress SCP output with the -q parameter.
Suppress SCP output with the -q parameter.

From the image above, the only SCP output is the password prompt. The -q parameter suppresses all other outputs.

11. Use SCP to copy a file from a remote host to your local computer.

For most of the examples above, we are copying files from our local machine to a remote server. Now, suppose you want to copy a file from the remote server to your local computer. You should use the SCP command with a little tweak in the usual syntax. Look at the command below.

scp tuts@192.168.83.132:/home/tuts/FOSSLINUX/serverFile.txt /home/tuts/FOSSLINUX

Use SCP to copy a file from a remote host to your local computer
Use SCP to copy a file from a remote host to your local computer.

By running an ls command on our local computer, we see that the ‘serverFile.txt’ copied successfully.

12. Copy files and directories without using passwords

From the examples above, we see that whenever we need to copy a file, we get a prompt to enter a password. We can skip this step by generating SSH keys to use during the authentication process.

To generate an SSH key, use the command below.

ssh-keygen -t rsa

The system should generate an SSH key. You can see the process taking place from the output in the terminal. Now, we need to copy the key to our remote host to use for authentication. Use the command below.

ssh-copy-id root@192.168.4.200

Generate SSH keys
Generate SSH keys

Now let’s try copying a file from our local computer to our remote host.

scp Hello1 tuts@192.168.83.132:/home/tuts/FOSSLINUX

Copy a file without a password
Copy a file without a password

From the image above, we see there was no prompt to enter a password.

13. Use a different cipher

SCP uses the AES cipher/encryption to copy files securely. However, you can specify a different cipher scheme using the -c parameter. Note, the c is a lower case unlike in compression where the C is in upper case. See the command below.

scp -c aes128-gcm@openssh.com TESTFILE tuts@192.168.83.132:/home/tuts/FOSSLINUX

Use a different cipher with SCP
Use a different cipher

We have specified the encryption scheme we want to use as AES. You can also use others like blowfish and many more.

scp -c blowfish TESTFILE tuts@192.168.83.132:/home/tuts/FOSSLINUX

14. Use the SSH key file instead of a password

SCP allows you to use a key file instead of a password to complete the authentication process using the -i parameter followed by the key file. See the command below.

scp -c privateKey.pem TESTFILE tuts@192.168.83.132:/home/tuts/FOSSLINUX

Use a key file instead of a password
Use a key file instead of a password

15. Use an SCP shell script to copy files

Assuming you have to use the SCP command regularly, you can write a shell script to simplify the whole process. In this article, we shall write a script that reads the destination host from the destfile.txt.

The destfile.txt file
The destfile.txt file

Below is our SCP script

echo "STARTING SCP SCRIPT"
echo
echo -e "Enter the path to the file you wish to copy:\c"
read file

for dest in `cat /tmp/destfile.txt`; do
scp -rC $file ${dest}:/tmp/

done

Save the file with a ‘.sh.’ extension since it’s a bash script. Make the script executable using the chmod command, as shown in the image below, before executing it.

Use an SCP Shell script
Use an SCP Shell script

Conclusion

Those are 15 SCP practical examples that you can apply to make your life easy working with Linux systems. Do you have an SCP command you want to share with our readers? Leave a comment below. If you find this article resourceful, please share the link with a friend.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,648FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.

[Guide] apt vs apt-get commands, and which one to use?

Most Linux users, both veterans, and newbies, often get confused about what the difference between the Linux commands apt, and apt-get are and when they should use one or the other.

5 Best Notepad++ Alternatives for Linux

Notepad++ has been the de facto standard for source code editors for nearly 16 years, almost since its creation in 2003.  For Windows users, that is.  For years, Linux users had no source code editor that compared to Notepad++ with all its bells and whistles, such as code folding, scripting, markup languages, syntax highlighting, auto-completion for programming (limited).

Installing an updatable Manjaro Linux on a USB Flash Drive

In this tutorial, we will show step by step how to install a Manjaro on a USB flash. For those new to this distro, it is a light-weight distro that is based on Arch Linux. Arch Linux is known for its simplistic approach and is typically made for Linux pros, but Manjaro bridges the gap between user-friendliness and rock-solid performance.