SCP Secured Copy

Published Feb 25, 2021
Updated Mar 10, 2021
By Simon
Table of contents

When working with servers without a GUI dashboard such as cPanel the SCP command is something you will need to get used to. Even if you do have a cPanel you will find being able to move files from one remote server to another remote server directly much faster than downloading to your local machine and then uploading to another remote machine.

In this article, we will look at the 3 scenarios you may come across when working with files and directories on a system. In the end, we will also look at a few options that can be used with SCP.

The 3 scenarios are

  1. From your local system to the remote system
  2. From a remote system to the local system
  3. From a remote to a remote

It is also assumed you have SSH key pairs set up but if you don't you could skip the key pair and use a password at the prompt. In the examples, I will show the SSH key example first and the password prompt example second. To do a remote server to a remote server using SCP you will need to set up SSH keys.

Using the SSH key you need to proceed the key with the -i option. This is the identity option that is passed directly to SSH.

You will also need to have the user and the IP for the server.

From your local system to the remote system

When uploading the files from the desktop they will go into the home directory of the user. You could place them directly in a known path by adding the path.

scp -i ~/.ssh/private_key ~/Desktop/  user@123.456.789.101:~/ 

scp user@123.456.789.101:~/ 

From a remote system to the local system

You will need to know the location or the path of the file to access it. As with uploading, the path will be from the home of the user.

In this example, we are downloading the file from the /var/www/ directory to the desktop on a Mac.

scp -i ~/.ssh/_key user@123.456.789.101:/var/www/ ~/Desktop

scp user@123.456.789.101:/var/www/ ~/Desktop

From a remote to a remote

Apparently, there is a way to do this from the local machine, but it has stumped me as you need to pass the login details of both servers to do this. If you know how this is done then drop a message in the comments.

That said if you log into the remote server of the file you want to transfer and then locate the file you what to transfer to you can then run the same command from desktop to remote this work.

In this example, we are transferring from the current directory on a remote server to the home of another remote server. You need to have logged into remote server 1 and have the public key from server 1 on server 2.

You will need to use SSH for this.

scp -i ~/.ssh/server_1_key  user_server_2@123.456.789.101:~/ 

When Using SCP you can also pass options to the command like any other Linux command, some common option you may use are.

Useful Options to use with SCP

They have to be either capitalized or lowercase as shown.

-i  used with SSH key
-r  recursive.
-v  Verbose, shows the transfer
-C Compress only during transfer

Other options I don't use that much but are available.

-l 800 bandwidth limit
-p preserve the attributes of the file; perms, timestamp, ownership
-q quiet mode suppress the output

SCP is a powerful utility and when managing your own VPS is definitely something you will want to use from time to time.
For other server tips, check out the Tools section where I cover Git, Composer and other CLI tools.

Thanks for reading.