How to use scp command in Linux to transfer files securely using ssh

On Unix or Linux operating systems, the scp utility, stands for secure copy, is similar to the more famous command, cp, but is used to transfer files and directories between hosts on a secure encrypted network.

Since it relies on ssh for data transfer, it offers the same security and uses the same authentication ssh. The scp command will prompt for passwords for authentication (if needed), unlike rcp.

In this article, we will dive into the world of secure transfer of files in Linux and learn how to use scp command. You will see how to use this utility through detailed explanations and example use cases of the commonly used scp switches and options..

Important to know before you start

1 Since scp relies on ssh for data transfer, an ssh key or password is therefore required for authentication on the remote systems (linux file transfer ssh).

2 Make sure beforehand to have write permission on the target system and read permissions the source file.

3 Note that the colon (:) is used by the scp tool to distinguish between remote and local locations.

4 scp will overwrite files without prior warning when copying files that have the same location and name on either sides.

Read: How to quickly send a text from Ubuntu to Android using a three-line Bash script

SCP Linux Command Syntax

In order to start using the scp command, you need to understand its basic syntax.This takes the form below:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

OPTION : Represents the scp options like ssh configuration, cipher, limit, ssh port, recursive copy …etc.

[user@]SRC_HOST:]file1 : Represents the source file.

[user@]DEST_HOST:]file2 : Represents the destination file

Like any other command, the scp tool has some options that are used to control its behavior. The most common ones are the following:

-P : Indicates the ssh port of the remote host to connect to .

-p : Specifies that files modification, modes and access times are preserved from the original file.

-q : Needed to suppress non-error messages and progress meter.

-C: Forces scp to compress the data as it passes the -C flag to ssh in order to enable compression of the encrypted secure connection.

-r: Tells the scp command to copy entire directories recursively.

-S : Program name to use for the encrypted connection. Since ssh is used, the program must therefore understand ssh switches.

-v: Verbose mode. Enables scp and ssh to output messages about their progress. This is useful in debugging connection, configuration and authentication problems.

Read: How to transfer a file from a remote computer to a local machine on Linux/Ubuntu

Transferring a local file to a remote destination

Before we start using scp, let’s copy files from one folder to another using cp. We all know how to achieve this using the cp command :

cp /home/net2_user/mysong.mp3 /home/net2_user/audio

Which copies the file mysong.mp3 in the home directory of the user net2_user to the audio directory in the home folder of the same user, i.e. net2_user.

On a similar fashion, an scp command example which can be used might look like the following :

scp /home/net2_user/mysong.mp3

Where this will upload the file mysong.mp3 to the server, using net2_user as the login name, into the remote directory /home/net2_user/audio. Note here that scp will prompt you for net2_user’s remote password before initiating the upload. If a remote directory has not been provided, the file will be copied to the home directory of the remote user. Not providing a filename on the destination folder, will make scp copy the file with the original name. If you want to save the file under a different name, you need to specify the new file name.

You may be interested to read: How to install OpenSSH server on Ubuntu ?

Transferring a remote file to a local destination

In order to download files, you could mention a remote location as the source location. For instance :

scp var wpcf7 = {"apiSettings":{"root":"https:\/\/\/wp-json\/contact-form-7\/v1","namespace":"contact-form-7\/v1"},"cached":"1"};