How to Securely Transfer Files Between Linux Systems

If you run Linux virtual machines, moving files between them is a basic requirement. In this article, we talk about scp (secure copy command) that encrypts the transferred file and password so no one can snoop.

The scp tool relies on SSH (Secure Shell) to transfer files, so all you need is the username and password for the source and target systems. Another advantage is that with scp you can move files between two remote servers, from your local machine in addition to transferring data between local and remote machines. In that case you need usernames and passwords for both servers.

This article will be focusing on transferring files between Linux VMs in details. Let’s get started.

Creating Linux Instances (VMs)

First of all, you need to create Linux virtual instances, same as reported here, with small differences you have to keep in mind, which are:

  1. Firstly, you need to create a security group that contains
  • TCP IP Protocol, 22 (SSH) Port Range, Your computer pubic IP Remote IP Prefix.
  • TCP IP Protocol, 80 (HTTP) Port Range.
  1. The source we need is Ubuntu-16.4-LTS
  2. In the security group section, don’t forget the newly created one besides the default security group.
  3. Import your computer public key.
  4. After launching the instances you need successfully, associate a floating IP for each of these instances.

Running Linux Instances (VMs) Using SSH

Running the virtual machines you have created is too simple, just open your terminal and execute the following command:

ssh ubuntu@<the VM floating IP>

Remember that each machine has to be in a separate terminal window.

And now we are ready to go!

Transferring Files Between Linux Systems (VMs)

Set Up SSH Keys

Generating a key pair provides you with two long string of characters: a public and a private key. You can place the public key on any server, and then unlock it by connecting to it with a client that already has the private key. When the two match up, the system unlocks without the need for a password. You can increase security even more by protecting the private key with a passphrase.

1. Create the RSA Key Pair

$ ssh-keygen -t rsa

2. Store the Keys and Passphrase Once you have entered the Gen Key command, you will get a few more questions:

Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): 

You can press enter here, saving the file to the user home (in this case, my example user is called ubuntu).

Enter passphrase (empty for no passphrase): 

It’s up to you whether you want to use a passphrase. Entering a passphrase does have its benefits: no one will be unable to log in to its associated accounts until they figure out the passphrase, buying the hacked user some extra time. The only downside, of course, to having a passphrase, is then having to type it in each time you use the key pair.

3. Copy the Public Key

cat .ssh/

And copy!

Repeat this set up SSH keys for all instances.

3. Give the VMs Access Paste the public keys that you have copied in the authorized_keys file:

sudo nano .ssh/authorized_keys

Add the public keys of the VMs that you want to transfer files to, from both sides. Now they can communicate with each other without fear!

Transferring Files With SCP

Here our important moment comes! You can simply transfer/ copy files between the two Linux VMs by replacing the items in [square brackets] with the actual data:

scp [path of file to send] root@[receiver's IP]:[target directory]

Here is what it looked like if I wanted to send the file happy_weekend.txt in the /home/journals directory to the /home/test directory of the system with the internal IP

scp /home/journals/ happy_weekend.txt ubuntu@

You will be asked if you really want to proceed. Type in yes and hit Enter to confirm. You will then be asked for the receiver’s root password. Type it in and hit Enter again. The copied file should then be accessible on the receiver’s targeted directory, although you should not have any permissions to write or execute it. If you want to grant you full permissions, use:

sudo chmod 777 /home/journals/ happy_weekend.txt