How to Use SCP Command to Securely Transfer Files

  Use code KB4KDO0L9 to receive a 10% recurring discount on any server.

The SCP command can be used to securely transfer file between servers. In this article, we'll provide practical examples of how to use this command.

How the SCP Command Works

The Secure Copy (SCP) command uses the Secure Shell (SSH) process to facilitate file transfers between two devices. SCP uses the authentication system used in SSH to verify hosts. 

How to install SCP

The SCP command comes pre-installed on Mac and Linux, so no additional installation is typically required. If you try and use the SCP command and receive an error such as bash: scp: command not found then this means that you need to install SCP on either your local or remote server.

Fedora or Red Hat Enterprise Linux and clones (CentOS:

yum -y install openssh-clients

Debian or Ubuntu:

apt-get install openssh-client

How to use SCP

The general syntax for SCP Linux or Mac command is as follows:

SCP Command Syntax

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

SCP Syntax / Command Options

The following flags are available when using the SCP command. You can add flags directly after scp when executing the command such as

scp -r folder root@IP-Address:/home/root

Flag

Description

c

The cipher to be used during the SSH session to encrypt files.

C

Compress files that are transferred during the SCP session.

F

Specifies a per-user configuration file for the SSH session.

i

When using an SSH key instead of a password during the SSH session, the -i flag allows you to specify the identity file of the SSH key.

l

Limit the amount of bandwidth used during the session, in Kbps.

p

Preserve file access and modification times from the local machine.

P

The SSH port of the remote host. Required when the remote host is using a non-standard SSH port.

q

Suppresses the progress meter.

r

Recursively copy directories being transferred

Here are some SCP examples to help you use this command to accomplish some common tasks.

Copy a Local File to a Remote System with the scp Command

In this example, we've navigated into the directory on the local system which we want to transfer to the remote server. We will transfer file.zip to the remote username root at IP-Address and the file will be placed into the /home/root directory.

scp file.zip root@IP-Address:/home/root

Copy a Local Directory and All Files to a Remote System with the scp Command

In this example, we've navigated into the directory on the local system which we want to transfer to the remote server. We will transfer the directory folder and all its files / folders to the remote username root at IP-Address and the folder will be placed into the /home/root directory.

scp -r folder root@IP-Address:/home/root

Copy a Remote File to a Local System using the scp Command

In this example, we're transferring the file file.zip from a remote server with the username root at IP-Address and move the file into our local directory /home/root

scp root@IP-Address:/home/root/file.txt /home/root

Copy a Remote Directory to a Local System using the scp Command

In this example, we're transferring the directory named folder from a remote server with the username root at IP-Address and move the file into our local directory /home/root

scp -r root@IP-Address:/home/root/folder /home/root

Copy a File Between Two Remote Systems using the scp Command

In this example, we'll copy file.zip from the user root at IP-Address and move it to IP-Address-2 in the /home/root directory.

scp root@IP-Address:/files/file.zip root2@IP-Address-2:/home/root

Conclusion

The SCP command is an easy way to transfer files directly between two servers. You now have all the knowledge you need to execute this command with ease!

About The Author

generic avatar icon