How can I securely transfer files from a remote server to my local machine using the SCP command in Linux?

How can I securely transfer files from a remote server to my local machine using the SCP command in Linux?

05.10.2023
Author: HostZealot Team
2 min.
758

When managing a server from a PC, it’s often the case that you need to transfer between both machines. On the one hand, it can be a bit of a difficulty for newer users since you can’t just drag files from your server and drop them on your PC. On the other hand, there are various ways to do that, and mastering one will solve your problems in the most cases.

Today we are going to dedicate our attention to one particular approach, namely the SCP command in Linux.

Decoding SCP Commands: A Comprehensive Guide

SCP stands for Secure Copy Protocol. It’s a command that provides a secure way of copying files between a remote server and a local machine over the SSH (Secure Shell) protocol, as opposed to the more basic cp (copy) command. With this, you can not only transfer information in a safe way but also have a piece of mind when sending sensitive data so no one can eavesdrop on it.

Demystifying SCP Syntax

Like many other commands, SCP is not a single command to perform a single action. Instead, there is a whole bunch of commands involved in SCP syntax, allowing you to perform various operations related to file transferring.

The essential structure for SCP syntax is:

scp [OPTIONS] [[user@]src_host:]file1 [[user@]dest_host:]file2

The “src_host” is the file or directory you wish to transfer, “dest_host” stands for where you want to transfer your content. Both elements can be either local or remote, that is, SCP supports bidirectional transfers.

“Options” stands for different permissions you can grant to your command. Among them:

  • P (capital) – choose the port to connect the remote server.
  • p (minuscule) – add the info about the moment of time to facilitate finding the operation later on.
  • r – transfer the whole directory specified.
  • q – add this if you don’t want to get any notifications about the completion of the transferring procedure.
  • C – compress the information while copying.

In fact, the SCP command is rather versatile. Besides sending files from your remote host to your local host, it can be used inversely or to share files between 2 remote hosts.

Before diving deeper into specific cases of SCP command involvement, it’s essential to check two conditions:

  • SSH must be present on each device.
  • You must also have root access to each device.

Essential SCP Commands Unveiled

Moving Files from Local Host to Remote Server

First, let’s look at how to send a file or a directory from the local host to your remote server. Apply:

scp /path/to/local/file username@remote_ip:/path/to/destination

“/path/to/local/file” stands for the path for the files that are to be transferred, “username” is the username of the remote server, “remote_ip” should contain the address of the remote server, “path/to/destination” is the desired location on the remote server.

Tip: Instead of typing the whole home directory each time, you can just type the ~ (tilda) sign as the destination.

Also, the same command be used to copy several files. For example, you can copy all files from the same directory that share a common extension. For this, specify *.[file extension] instead of the file. To copy all .txt files, for instance, type *.txt.

scp /path/to/local/*.txt username@remote_ip:/path/to/destination

You can also save the file under a different name by adding the desired name after the destination.

scp /path/to/local/painauchocolat.txt username@remote_ip:/path/to/destination/croissant.txt

In the example above, the “painauchocolat.txt” will be saved as a “croissant.txt”.

Transferring Files from Remote to Local

The corresponding command for sending files from your remote server to your local machine is built in a similar way:

scp username@remote_ip:/path/to/remote/file /path/to/local/destination

Substitute username, “remote_ip”, “/path/to/remote/file”, and “/path/to/local/destination” as appropriate in the previous cases.

Tip: If your files are located in the home directory, there is no need to specify the path. Just type your filename and its location as well as the destination will be set as the home directory.

scp username@192.168.137.59:painauchocolat.txt

With this command, you can send the painauchocolat.txt from the home directory of your remote server to the home directory of your local computer.

Copying Files between Remote Hosts

A good thing about SCP protocol is that it not only allows copying files between a local and a remote host but also enables file exchange between two remote servers. To do this, use the following syntax:

scp username@remote_ip1:/path/to/source/file username@remote_ip2:/path/to/destination

Replace username, remote_ip1, remote_ip2, /path/to/source/file, and /path/to/destination accordingly.

Keep in mind that it doesn’t necessarily has to be an identical user. You can send files from one user account to another by entering the corresponding user names.

Managing Multiple File Transfers

You can also use SCP to transfer groups of files and entire directories. For transferring directories, you’ll need to append the -r flag to the SCP command:

scp -r /path/to/local/directory username@remote_ip:/path/to/destination

If you want to copy separate files from a directory, just specify several file names at the source path. Like this:

scp /path/to/local/file1 file2 file3 file_andsoon username@remote_ip:/path/to/destination

Important Takeaways

Permissions: Make sure that you have the necessary permissions for transferring files. This has to be the reading permission for source files and the writing permission for the end directory.

SSH: Since SSH is involved in SCP, you’ll need the corresponding password.

Files with identical names: Make sure there are no files in the target directory whose names are identical to those you’re copying unless you want the SCP to replace them without asking you.

The colon: Don’t forget to write the colon sign (:) between the source and the target locations.

Home direction: If you transfer files to or from the home direction, you may omit specifying it and type file names right away.

Concluding Thoughts

Transferring files is a common routine you are likely to perform at some point in time when dealing with remote servers. SCP is a common way to do it that is rather convenient and secure at the same time. We hope that the article above has redundantly clarified the syntax behind these features and no you’ll face no troubles while transferring your files. Take care! 

Related Articles