How To Copy Files With SCP (SSH) Securely

SCP is a command line program that allows to copy files between computers (hosts) in a very secure manner. This How To Copy Files With SCP (SSH) Securely describes how you can copy files securely between computers.

Index of How To Copy Files With SCP (SSH) Securely 

Environment

This How To Copy Files With SCP (SSH) Securely takes as a basis two computers (hosts). I share the environment with you, as i believe it helps you to understand how you can leverage on this powerful SCP tool for your enviornement.

  • Host A – IP Address 192.168.2.45
  • Host B – IP Address 192.168.2.21
  • I have a number of Maria DB backup files that i like to copy from Host A to Host B
  • Both, Host A and Host B are powerful Raspberry computers that you can buy for a very good price
  • Between both hosts (A and B) the SSH keys are exchanged (trusted)

By the way, the same works of course also on an Apple Mac, such as a cool Mac Book Air or an even cooler Mac Book Pro!

How to copy Files Witch SCP (SSH) Securely

Your idea is to copy files form Host A to Host B (192.168.2.21) in a simple and secure manner. To be honest, nothing is easier than this. The execution of a simple command will get this done for you:

scp [OPTION] [user@]HOST_A:]file1 [user@]HOST_B:]file1

If you like to copy a whole directory, then you like to replace “file 1” by the directory name. (please ensure the directory exists on the destination host):

scp [OPTION] [user@]HOST_A:]/directory1 [user@]HOST_B:]directory1

Now lets make a real life example matching my environment. I have a bunch of files stored on Hose A (192.168.2.45) in the source directory called “~/Backup_OpenHAB_DB/pi/maria-db-backups”. To ensure these files are additionally backed up on another Host B, i copy them with the below scp command into the destination folder called “~/maria-db-backups-ARCHIVE/2020”. To do so is easy, you simply leverage on the below command:

scp /home/myuser/Backup_OpenHAB_DB/pi/maria-db-unserhaus-backups/maria-db-backups.sql.2020* pi@192.168.2.21:/home/pi/maria-db-backups-ARCHIVE/2020

Details:

  • scp – is reflecting the scp command
  • /home/myuser/Backup_OpenHAB_DB/pi/maria-db-unserhaus-backups/ – is reflecting the source directory on Host A (192.168.2.45)
  • maria-db-backups.sql.2020* – is reflecting the file names. In this case all files that start with “maria-db-backups.sql.2020”
  • pi@192.168.2.21: – is reflecting 2 things, 1. the user name on Host B (in this case “pi”) and the IP Address of Host B (192.168.2.21)
  • /home/pi/maria-db-backups-ARCHIVE/2020 – is reflecting the destination directory on Host B (192.168.2.21)

Very simple, isn’t it? Now you simply replace the host names and directories by how it looks like in your environment and this How To Copy Files With SCP (SSH) Securely hopefully was a great help to you. 

Further information about SCP

For the above, and once the command executed, the command line will provide the status which looks like this:

How To Copy Files With SCP (SSH) Securely

SCP (SSH) Copy Print Screen

This will continue up until all files are copied.

There are various additional options that you can use for SCP. Including:

  • -P : – P specifies the remote host ssh port (e.g. the port on Host B if it is not 22 which is the standard SSH port
  • -C : -C forces scp to compresses data whilst sending to the destination host (e.g. in the above Host B)
  • -rThis option asks scp to copy directories recursively (e.g. if there would be additional subdirectories in /home/myuser/Backup_OpenHAB_DB/pi/maria-db-backups/)

As with all Linux programs, also SCP consists of a very good explaining man page that you like to consult for further great options. You can find this in your Linux installation itself by typing “man scp” or in the internet by clicking this link: SCP Man Pages.