If you want the ultimate security when moving files from computer to computer, you’ll need to know how to SCP in Linux. Here’s how.
The Basics Behind Secure Copy (SCP)
Secure copy, or scp, is a Linux command used to copy files between servers securely. It encrypts those files, so nobody snooping your network traffic can intercept and read them. SCP is built into the Secure Shell (SSH) protocol, which typically uses the strongest encryption available. SCP has been tremendously popular for years. It’s simple to use, highly secure, and comes preinstalled on most Linux server installations. Some desktop distributions, like Ubuntu, may not have it installed by default. Windows users, fear not, because there are excellent SCP clients readily (and freely) available for you.
Preparing to Use SCP on Linux
Before you go any further, make sure you have the SSH server installed on your Linux server. If it isn’t, you’ll need to install it. Under Ubuntu, you can install the OpenSSH server using apt.
Open Terminal on your Linux server.Issue the command sudo apt update to make sure you have the most current sources available for install.Next, install openssh using the command sudo apt install openssh-server.
Make sure any computer you want to use scp on has an SSH server installed.
How to SCP in Linux
You can use scp to copy files from your local machine to a remote one, from a remote server to your local PC, or from one remote server to another. It’s even possible to copy an entire directory using a single scp command. Both the files and your password are encrypted, so you don’t need to worry about anyone sniffing your traffic getting anything sensitive. The basic scp command syntax is as follows:
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
OPTION – scp options like the cipher to use, ssh configuration, ssh port, limit, recursive copy, and so forth.[user@]SRC_HOST:file1 – The source file being copied. [user@]DEST_HOST:]file2 – The destination to copy the file to.
Some of the options you can use include:
-P – Specifies the remote host SSH port.-p – Keeps file modification and access times.-q – This option will suppress the progress meter and non-error messages.-C – If you include this parameter, it forces scp to compress the data sent to the destination.-r – This tells scp to copy the directories recursively.
Make sure you’re very careful when you copy files sharing the same name and location on both systems. The scp command is quite unforgiving, and will overwrite files without any warning whatsoever.
A Simple SCP Transfer Between Two Computers
Let’s say I have a file named resume.pdf on my local PC, and want to copy it to my Linux server. To do this, I’ll open Terminal or any other command line app, and change to the directory where I’ve previously saved the file. You can even use the Linux GUI file manager to open the directory directly in Terminal. Next, I’ll run the following command:
scp resume.pdf jeff@10.10.0.2:/home/jeff/documents/
Linux will ask me to enter the password for my user account on the remote server, then transfer my file.
You’ll notice that the commands above didn’t include the filename for the remote server. In this case, the scp command automatically uses the original filename. The other important thing to note is how you refer to file paths using scp. For the local computer, you can use the relative path, but you must provide the full absolute path for the remote server. For example, the relative path:
documents/spreadsheets/budget.xlsx
Might look like this using the absolute path:
/home/jeff/documents/spreadsheets/budget.xlsx
Doing Even More With SCP
As noted, you can even copy entire directories using a single scp command. Here’s how that looks.
scp -r ~/documents/spreadsheets jeff@10.10.0.2:/home/jeff/documents
Assuming the spreadsheets directory doesn’t yet exist on the remote server, the directory from my local computer, along with all files and directories inside it, will be copied to /home/jeff/documents/ on the server. It will show up as a new directory, spreadsheets, within my documents directory. On the other hand, if you need to retrieve a file from the remote server to your local computer, you could issue a command such as this:
scp jeff@10.10.0.2:/home/jeff/documents/resume.pdf ~/documents
If you need to copy files between two remote servers, you can do that with scp. Instead of specifying a local source or destination, you’ll enter both source and destination as remote servers.
scp jeff@10.10.0.2:/home/jeff/documents/resume.pdf bonner@10.10.0.5:/home/bonner/documents
In this case, you will be prompted to enter the passwords for both remote accounts. Even though you may have issued the command from your local desktop, the data will transfer directly from one remote host to the other. If you’d rather the data route through the machine on which you issued the command, use the -3 option:
scp -3 jeff@10.10.0.2:/home/jeff/documents/resume.pdf bonner@10.10.0.5:/home/bonner/documents
Fully Incorporating SSH and SCP Into Your Workflow
You should realize that ssh and scp can be very powerful tools, now that you see how to SCP in Linux. There’s plenty more the commands are capable of beyond what’s covered here, including using SSH-key-based-authentication to connect to your Linux servers without entering a password. If you regularly connect to the same servers, you might also consider creating a configuration file to store different SSH options for each remote machine you connect to. This can include everything from the remote IP address to the right usernames and even command-line options commonly used. Comment
Δ