How to Use SCP Command for File Transfer

Secure copy protocol (SCP) is a method that is based on Secure Shell (SSH) protocol, offering secure transfer of computer files between a local host and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol.

SCP Linux command is used by the administrators to streamline the data transfer process between Linux servers as no log-in is needed into a server. It offers better security as compared to File Transfer Protocol (FTP) and complies with PCI Security Council Standards.

Understanding SCP SSH Command Syntax

The syntax to perform a file transfer between two remote systems using the SCP command looks as follows:

scp [other options] [source username@IP]:/[directory and file name] [destination username@IP]:/[destination directory]

Below is the list of commands:

  • other options ‒ modifiers that can be added to the SCP command.
  • source username@I‒ username and IP of the machine that hosts the target file.
  • :/ ‒ tells the SCP command that the user will type in the source directory.
  • directory and file name ‒ the file’s location and name.
  • destination username@IP ‒ username and IP of the destination system.
  • destination directory ‒ the destination directory where the file is saved.

Here’s an example which displays SCP command in real-time:

scp -p root@162.168.1.1:/media/scp.png root@162.168.1.2:/desktop/destination

//User your ip address here

There is no need for the IP address and the destination or source path like /desktop/folder_name for copying to or from localhost.

Copying Files Using the SCP Command

One of the best benefits of SCP is the secure transfer of files between two remote hosts or a remote machine and a local system.

  • Copying From a Local Server to a Remote Host

We’ll copy a local file scp.zip to a remote server called root. The server’s IP address comes after the username.

scp /users/UserName/desktop/scp.zip root@191.162.0.2:/writing/article

In case, you don’t have automatic SSH client confirmation set up, you will need to enter the remote machine’s user password and observe the progress meter. It will look as follows:

root@191.162.0.2’s password:
novel3.zip 100% 0 0.0KB/s 00:00

Let’s assume that the remote server is set to listen for SSH connections on a port other than the default SSH port 22. In such case, you will need to specify that port using the -P option

scp -P 2322 /users/UserName/desktop/scp.zip root@191.162.0.2:/writing/article

If you wish to change the name of the file during the transfer operation, then the command used will be:

scp/users/UserName/desktop/scp.zip root@191.162.0.2:/writing/article/howtoscp.zip

Use the –r option followed by the same command line to copy a directory with multiple files or subdirectories.

scp -r /users/UserName/desktop root@191.162.0.2:/writing/article
  • Transferring a Remote File to a Local Machine

To copy the scp.zip file from the same remote host to the local computer.

scp root@191.162.0.2:/writing/articles/SCP.zip Users/UserName/Desktop

Executing this command will prompt the same SSH login output which will require a password.

  • Safely Move a file between Remote Hosts

To copy files from one remote server to another, enter the  passwords for both accounts after running the below command:

scp root@191.162.0.2:/writing/article/scp.zip root@11.10.0.1:/publishing

The above command copies the source file /writing/article/scp.zip from the first host to the second one.  You can use the -r option to copy folders and specify the folder path instead of the file inside it.

Normally, the file goes straight from the first remote server to the second. In case you want to reroute the operation, add the -3 option as shown below:

scp -3 root@191.162.0.2:/writing/article/scp.zip root@11.10.0.1:/publishing
  • Using the SCP Command with options

There are numerous common options in single-character form (-o) and their descriptive equivalent (–option). The -q parameter prompts the system to run the operation in silent mode. It disables the progress meter output as well as warning and diagnostic messages.

The -v parameter to enable verbose output for easy debug:

scp -v /users/UserName/desktop/scp.zip root@191.162.0.2

The -4 and -6 options define the protocol version, which is either IPv4 or IPv6. IP address requirements that can be configured more comprehensively with the address-family keyword.

scp -6 root@191.162.0.2:/users/UserName/desktop/scp.zip

The -p option conserves modification, access times, and modes from the source file.

scp -p /users/UserName/desktop/scp.zip root@191.162.0.2

Use the -stat option to check the details of the copied files, such as the date of creation and file size.

The -C option enables data compression for large files or directories while the transfer operation is happening. Keep in mind that this option won’t work on compressed files, such as .zip and .rar archives.

scp -C Folder root@191.162.0.2
The -c option specifies the encryption algorithm that the client should use. The values that can be entered are ‘aes256-ctr‘, ‘aes256-cbc‘, and ‘3des-cbc‘. The default option in the shell configuration is ‘AnyStdCipher’.
scp -c 3des-cbc /users/UserName/desktop/scp.zip root@191.162.0.2
The secure copy protocol (SCP) makes securely copies files from one remote host to another through an encrypted connection. This remote file transfer method encrypts data with a secure shell, maintaining the confidentiality of the transmitted information.