How to encrypt transfer files using SCP command

SCP stands for Safe copy. is a command line program that allows you to securely copy files and directories between computers

3 min read
By myfreax
How to encrypt transfer files using SCP command
How to encrypt transfer files using SCP command

SCP is Safe copy. Is a command-line program that allows you to securely copy files and directories between computers.

With scp, you can copy files or directories from a local system to a remote system. From remote system to local system. Between two remote systems of the local system.

When scp is used to transfer data, the files are encrypted, so anyone snooping on the traffic cannot see its contents.

In this tutorial, we'll show you how to use the scp command with practical examples and detailed instructions for the most common scp options.

Before we start using the scp command, let's review the basic syntax. The scp command syntax takes the following form.

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

OPTION scp is an optional option, such as password, ssh configuration, ssh port, restriction, and recursive copy

[user@]SRC_HOST:]file Source file. [user@]DEST_HOST:]file2 Destination file.

The local file should be specified using an absolute or relative path, and the remote file name should contain the user and host name.

scp provides a number of options for controlling its behavior. The most widely used option is.

-P Specifies the ssh port of the remote host. -p Reserves the time for modifying and accessing files. The -q option disables the display of progress and non-error messages.

-C This option forces scp to compress the data when it is sent to the target computer. -r This option instructs scp to copy directories recursively.

The scp command relies on ssh for data transfer and therefore requires an ssh key or password to authenticate on the remote system.

Colon: is the scp method to distinguish the local location from the remote location.

To be able to copy files, you must have at least read permission to the source file and write permission to the target file system.

Be careful when copying files with the same name and location between two systems; scp can overwrite files without warning.

When transferring large files, it is recommended to run the scp command in a screen or tmux session.

scp Copies the local file to the remote

To copy a file from the local to the remote system, please run the command scp file.txt remote_username@ / remote/directory.

Here, file.txt is the name of the file we want to copy, remote_username is the username of the remote server, and is the IP address of the remote computer.

/remote/directory is the directory where you want to copy files to the remote computer. If you do not specify a remote directory, the files will be copied to the remote user's home directory.

If the file name is omitted from the destination location, the original file name is retained. If you want to save the file with a different name, you need to specify the file name.

When copying local files to a remote machine, if you have not already set up a passwordless SSH login for the remote machine.

you will be asked to enter the user password and then the transfer process will begin.

# target location to omit filename SCP file. TXT remote_username@ # / remote directory target location specified filename SCP file. TXT remote_username@ / remote/directory/newfilename. TXT
remote_username@'s password:
file.txt                             100%    0     0.0KB/s   00:00

If the SSH service of the remote host is not listening on the default port 22, you can specify the port using the -P parameter option.

The commands for making directories are very similar to those for copying files. The only difference is that you need to recursively copy with the -r option.

# -p specify SSH port 
scp - 2322 p file.txt remote_username@ directory 

# -r recursively copied 
scp - r/local/directory remote_username@

scp Copies remote files to the local system

To copy files from a remote to a local system, use the remote location as the source and the local directory as the destination location.

For example, to copy a file named file.txt from a remote server whose IP is, run the following scp command.

If you have not already set up a passwordless SSH login for the remote machine, you will be asked for a user password.

scp remote_username@ /local/directory

scp Copying files between remote systems

Unlike rsync, with scp, you can transfer files from one remote machine to another without logging into one of the servers.

The following command will copy the file /files/file.txt from remote host to directory /files on remote host

You will be prompted for passwords for both remote accounts. Data will be transferred directly from one remote host to another.



In this tutorial, you learned how to copy files and directories using the scp command. You might also want to set up SSH key based authentication and then connect to the Linux server without entering a password.

If you periodically connect to the same system, you can simplify your workflow by defining all connections in an SSH configuration file.