The scp command in Linux is used to copy files and directories to or from a remote system. It works very similarly to the cp command, except that it copies files to or from other systems that are either on your local network or somewhere over the internet.
The scp command works through the SSH protocol. In other words, both systems must have SSH set up in order to use the scp command. As you may already know, the SSH protocol creates a secure and encrypted tunnel between your local system and another system, which means that files you copy with the scp command are not liable to be intercepted.
The SCP command is based on the BSD RCP protocol. This protocol is used for file transfers between connected hosts on the local network or over the Internet. The actual data transfer and user authentication is done via Secure Shell (SSH). This in turn provides end-to-end encryption for all data transfers.
As you can imagine, being able to copy and paste files to or from remote systems is extremely useful. However, it’s not the only command in Linux that is capable of this. For complex file transfers or complete backups, it’s recommended you use something more like the rsync command. The scp command is more ideal for quick and simple transfers of one or a few files.
In this guide, you’ll learn how to use the scp command in Linux through examples. Follow along below to learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the scp command on Linux
Using the scp command in Linux to copy a file to a remote system
scp command in Linux Basic Examples
- Let’s look at a simple example where we use the scp command to copy a local file named
file.txtto a remote server with IP address
$ scp file.txt email@example.com:/home/linuxconfig
In this example, we are authenticating with the username
10.1.1.20. After supplying the password or authenticating with RSA keys, our file
file.txtwill be copied from the local system into remote directory
- If we had wanted our original file to adopt a new file name on the remote system, we can specify that in our command.
$ scp file.txt firstname.lastname@example.org:/home/linuxconfig/newname.txt
- To copy a directory instead of a file to the remote system, you will need to use the
-roption in your scp command.
$ scp -r Downloads email@example.com:/home/linuxconfig/
In this example, we are copying our local
Downloadsdirectory into the remote
- So far, we have seen how to copy files and directories from our local system to the remote system. But, what about the other way around? If we want to copy a remote file into our local system, all we need to do is reverse the order of the command, as seen below.
$ scp firstname.lastname@example.org:/home/linuxconfig/file.txt /path/to/destination
In this example, we are copying remote file
/home/linuxconfig/file.txtinto our local directory at
You can always use the man command to read more about the scp command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
In the section below, we’ve collected a few of the more advanced options that you can use with the scp command in Linux. Make no mistake, scp is pretty simple as far as Linux commands go. However, some of the options below are not as frequently used but still fill a certain niche and are handy to know.
scp command in Linux Advanced Examples
- If the remote system is not running SSH on the default port of 22, you will need to specify another port number with the
-Poption in your scp command. In this example, the remote system is running SSH on port 2210.
$ scp -P 2210 file.txt email@example.com:/home/linuxconfig
- Interestingly, you can also copy files from one remote system to yet another remote system. Just be prepared to authenticate with both servers before the transfer will initiate.
$ scp user1@host1:/files/file.txt user2@host2:/files
In this guide we have learned about the scp command. It’s a great tool to know when you already have an SSH connection between your local system and a remote server. It’s probably one of the easiest and painless ways to quickly copy files to or from a remote server. To make things even more convenient, you can always set up RSA key authentication, which will bypass the password prompt entirely while also increasing security.