Nowadays, there are many ways to transfer files using cloud services. Still, the safest way to do this is not to use Dropbox, Google Drive, or anything like that.
If you want maximum security when moving files from one computer to another, you will need to know how to do SCP on Linux. Here we explain how to do it.
The basics behind the secure copy (Secure Copy: SCP)
Secure Copyor scp, is a Linux command used to copy files between servers securely. It encrypts those files, so that no one spying on your network traffic can intercept and read them. SCP is built on the Secure Shell (SSH) protocol, which typically uses the strongest encryption available.
SCP has been wildly popular for years. It’s easy to use, highly secure, and comes pre-installed 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 available (and free) for you.
Preparing to use SCP on Linux
Before going any further, make sure you have SSH server installed on your Linux server. If it isn’t, you’ll need to install it. On Ubuntu, you can install the OpenSSH server using apt.
- Open the Terminal on your Linux server.
- Issue the command sudo apt update to ensure you have the most current fonts available for installation.
- Next, install openssh with the command sudo apt install openssh-server.
Make sure that any computer you want to use scp you have an SSH server installed.
How to Use the SCP Command 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 is even possible to copy an entire directory with a single command scp Both your files and your password are encrypted, so you don’t have to worry about someone snooping on your traffic getting something sensitive.
Basic command syntax scp is the next:
scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2
- OPTION – options of scp such as the encryption to use, the ssh configuration, the ssh port, the limit, the recursive copy, etc.
- [[email protected]]SRC_HOST:file1 – The source file being copied.
- [[email protected]]DEST_HOST:]file2 – The destination to copy the file.
Some of the options you can use are:
- -P – Specifies the SSH port of the remote host.
- -p – Maintains file access and modification 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 which copies the directories recursively.
Be sure to be very careful when copying files that share the same name and location on both systems. The scp command is pretty unforgiving, and will overwrite files without any warning.
A simple SCP transfer between two computers
Let’s say I have a file called resume.pdf on my local PC, and I want to copy it to my Linux server. To do this, I will open the Terminal or any other command line application, and change to the directory where I 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 [email protected]:/home/jeff/documents/
Linux will ask me to enter the password of my user account on the remote server, and then it will transfer my file.
You will notice that the above commands did not include the remote server file name. In this case, the scp command automatically uses the original file name.
The other important thing to note is how you refer to file paths using scp. For the local machine, you can use the relative path, but you must provide the full absolute path to the remote server. For example, the relative path:
It could look like this using the absolute path:
Do even more with SCP
As noted, you can even copy entire directories using a single command scp This is how it looks.
scp -r ~/documents/spreadsheets [email protected]:/home/jeff/documents
Assuming that the directory spreadsheets does not yet exist on the remote server, the directory on my local computer, along with all the files and directories it contains, will be copied to /home/jeff/documents/ on the server. It will appear as a new directory, spreadsheetsinside my directory documents.
On the other hand, if you need to retrieve a file from the remote server to your local computer, you could issue a command like this
scp [email protected]:/home/jeff/documents/resume.pdf ~/documents
If you need to copy files between two remote servers, you can do it with scp. Instead of specifying a local source or destination, you will enter both the source and destination as remote servers.
scp [email protected]:/home/jeff/documents/resume.pdf [email protected]:/home/bonner/documents
In this case, you will be prompted to enter the passwords for both remote accounts. Even if you issued the command from your local desktop, the data will be transferred directly from one remote host to the other. If you prefer the data to go through the machine on which you issued the command, use the option -3:
scp -3 [email protected]:/home/jeff/documents/resume.pdf [email protected]:/home/bonner/documents
Fully incorporate SSH and SCP into your workflow
you should realize that ssh and scp they can be very powerful tools, now that you see how to SCP on Linux. There is much more that the commands are capable of beyond what is 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 correct usernames and even commonly used command line options.