Migrating data between different machines with scp

Posted on Mon 20 November 2017 in Technology

This blog deals with beginner level commands and tools

Migrating data from one machine to another is easy if both of them are physically accessible and in a same room. Just use a pen drive and it becomes the safest way too. But what if both the machines are further apart? The solution to this problem is what we are going to discuss in this blog.

Let’s recall on of the most basic methods we use to copy file/folder from one location to another. Yes, it’s cp(copy). We use it as cp source destination to copy files from source to destination. In our case we might have source or destination as remote location instead of somewhere in the local machine itself. We’ll use a tool which will look syntactically similar to cp in the beginning know as scp(secure copy).

This way is recommended when you are trying to transfer the data only once from source to destination in a single pass. In order to transfer multiple files/directories and have a better session based control over your connection ftp is recommended.

What is scp?

scp is a tool on Linux which is used to copy files between remote machine or servers. scp uses ssh and utilizes it’s authenticity and security for file transfer which is a win-win for users. So, to use scp you need to setup ssh server on the remote machine.

Transferring files with password

If your remote machine or server has a user and password setup which you use to login then scp will work in the similar way.

scp /tmp/myfile username@0.0.0.0:/tmp/

Here we’re trying to copy myfile from /tmp/ to the machine who’s IP address is 0.0.0.0. You’ll also notice that before the IP address we mention username with which we want to access the remote machine. And finally we add : with the folder to which we want to copy our file. So, /tmp/myfile becomes source and username@0.0.0.0:/tmp/ becomes destination.

When you run the above command you might be asked for a password. Simply enter the password and the copying will begin.

Transferring files with a key

To provide more security a key can be used in place of password using public key cryptography. You need to create a key pair using ssh first on the remote machine and then use it from the machine you are trying to access the remote machine as follows.

scp -i ./mykey.pem /tmp/myfile username@0.0.0.0:/tmp/

You can see the change clearly as we have added -i flag to signify that we’ll be passing a key, followed by mykey.pem which the location of our key. For the first time you’ll be prompted to confirm the connection with a yes. Transfer will begin soon after that without any password.

scp is very easy to use tool for quick transfer without the fuzz of session management. In future blog post I’ll try to simplify the use of tcp as well. You’re free to use whichever suits you most and that my friend is the beauty of FOSS. Embrace it!

Share the blog if you like it
(you can also share to let everyone know how bad I write)