What is SCP Command in Linux and how to use it?

Posted on June 24th, 2019

As we all know, Linux is one of the most loved operating systems in the world. And it’s an open source Kernel that has a contribution by thousands of programmers worldwide. It has a compelling set of commands that can be used to perform some small tasks very smoothly. One of those commands is scp Command. In this tutorial, I will show you how we can use scp command in Linux to transfer files from one machine to another.

If you are wondering what the full form of scp is, it is secure copy. Before the actual tutorial, I would like to inform you that the IP address of the remote server in this tutorial would be 1.2.3.4 and the IP address of localhost would be 127.0.0.1. So, let’s get started with the tutorial.

scp Command in Linux

SCP command is used to copy files from one Linux machine to another. For example, If you want to move a backup of your website from one VPS(Virtual private server) to another VPS, you can do it with a single command.

Here is the syntax of scp command.

scp {OPTIONS} user@source-host:/path/to/source-file user@destination-host:/path/to/destination-file

The options are optional; You can use the above-given command to copy a file from one machine to another without any options. There are many useful options that we can use that we will discuss further in this tutorial.

Copy file from local to remote and vice-versa

Right now, let’s say we want to copy a file called data.sql from our local machine to a remote server. Then the command for this example would be.

scp data.sql [email protected]:/database_backups/data.sql

Here, we are copying a file from our local machine to the remote machine. But we can also pull a file from a remote machine to our local machine. Here is how we can pull back our example file from the remote server.

scp [email protected]:/database_backups/data.sql ./data.sql

scp with non-default ssh port

Now, Let’s say that the SSH port on the remote machine is not a default port; for instance, let’s assume the SSH port is 2222 instead of 22. Then we can use the -P option in scp command to define the port of the remote server.

scp -p2222 data.sql [email protected]:/database_backups/data.sql

We can also define the SSH key with scp command.

scp command with SSH key

If you want to set the identity file or a private key to perform authentication instead of a plain password, you can use the -i option with the command. In our example, it should look like the following command.

scp -i /path/to/key data.sql [email protected]:/database_backups/data.sql

With SSH keys properly configured, it becomes straightforward to move files from a server to server using scp command. Let’s see how we can recursively copy entire directories

Copy recursively using scp

You can also copy entire directories recursively using scp. In this example, we will copy a directory called backups from local to the remote server using scp. The command, in this case, would be.

scp -r backups [email protected]:~/

This command will copy the entire backups directory recursively from local to remote server.  There are many more options available with scp, but these are the most used ones.

Other options provided by scp

There are many different options available with secure copy that can come handy at times. But these are the options that are not so popular so that we will cover all of them in one section.

If you want to specify a per-user ssh configuration file while using scp, you can use -F option. Also, if you want to set the bandwidth usage limit, use -l flag followed by the limit in Kbit/s.

The last two options that you might want to use is to force scp to use IPv4 or IPv6 addresses. If you’re going to force scp to use IPv4 address only, execute the command with -4 flag. If you are going to force scp to use IPv6 addresses only, run the command with -6 flag.

 

The scp command in Linux is effortless to understand and use. It can be used to save many hours by transferring files directly from one server and another. Let us know if you are facing any issue following this tutorial; we are happy to help.

Leave a Reply