How to Transfer Data Using SSH Protocol

So, what is SSH and how do we use it to transfer data from one machine to another. It’s very easy and highly suggested because its fast and more importantly its secure. So stay sharp cause we will be going through step by step on how to transfer data from remote server using SSH protocol. 

SSH stands for Secure Shell and its usually run-on port 22. SSH is a very popular network communication protocol used for the communication between 2 machines. They can both be on different servers; can both be local or more likely case is 1 being a local machine and the other being a remote server machine. SSH provides a secure connection between the 2 machines. It is used to get access to that remote machine and executing shell commands remotely. 

Prerequisites!

  • Familiar with bash scripting shell commands.
  • Should know how VMWare works
  • Should have git installed and thus have access to GitBash or PuTTY would also work 

Why would you need SSH to transfer data instead of other tools?

In one word, interception. It’s because the data you transfer has a high risk of getting intercepted or “tapped” by someone leading to data leak and many repercussions. SSH provides a secure connection between the 2 machines so that they can safely perform tasks without the risk of their data packets getting intercepted. 

Security Breach via Interception
Security Breach via Interception

Real Life Scenarios where SSH is Applicable:

  • It can be used for transferring data from your laptop to your webhosting where your website is located.
  • It can also be used to transfer your website contents and files to another hosting server, if you’re changing hosting plans let’s say. Its rather fast then manually transferring it.
  • It can be used to deploy a server or any cloud feature or even run your virtual machines on Cloud Services like Amazon AWS or Google Cloud Platform.

So, let’s get into it and see how SSH works.  

What we need?

  • Public key
  • Private key
  • Username and Ip address of the remote server
  • SSH service installed and running on both machines
  • Cup of coffee

Okay so what I’ve done here is, I’ve run a Linux Virtual Machine using VMWare on my windows laptop and connecting both using SSH protocol. let’s say we want to send files from our local machine (in this case Windows operating system) to the remote server machine (which is Linux virtual machine in this case).  

we need to create public key and private keys. Both of which will be created using just a single line of command. However, keep in mind that only the public key will be copied and saved to the remote machine, the one we need to connect with. And the private key will not be shared with anyone and only be kept within our local machine I.e., Windows operating system.  

To create the keys, open up PuTTY or gitBash (to open git bash, git must be installed then simply search ‘gitbash’ in windows search bar) and execute the following command: 

				
					ssh-keygen –t rsa –b 4096 
				
			

It’s going prompt you some questions, just hit enter to all of them and public and private keys would be generated. 

Generating SSH Key Pairs on Local Machine
Generating SSH Key Pairs on Local Machine

The above image shows that the public and private keys are now generated and also tells us the location to where the keys have been stored in. Then spin up the VM and restart the SSH service and fetch the IP address and the username. Like so:  

Copying the IP Address of the Remote Server
Copying the IP Address of the Remote Server

The command “ip add” will show you the IP address and other configurations and “whoami” command will tell you the current username which is ‘daniyalakbar’ in this case. 

Now we must install and start SSH service on the Linux server. Mind you, I’m using Ubuntu based Linux distro, so execute the following commands in the terminal: 

				
					sudo apt-get remove --purge openssh-server 
sudo apt-get update 
sudo apt-get install openssh-server 
  
sudo systemctl start ssh.socket 
sudo systemctl start ssh.service 
sudo systemctl restart ssh.service
				
			

This will make sure that your SSH service is up and running. Now we need to copy the public key which we created on the Windows machine into the Remote server. To copy only the public key, we need to run the following command.  

				
					ssh-copy-id daniyalakbar@192.168.170.128 
				
			

NOTE: it should be obvious that the username and IP address for everyone would be different, so do not copy the above username and IP address exactly.  

Copying the Public key to the Remote Server for SSH Protocol
Copying the Public key to the Remote Server for SSH Protocol

When you run the above command, it will ask you to enter the password. This is the password that you have been using so far for logging into the remote server manually. For me, I had set it to “123” when I was setting up the Linux VM. After this step, we are basically done. 

So, what we did just now was to copy the public key on to the remote server by using the remote server’s username and IP address, meanwhile the private key is still with us. Don’t worry about the public key getting into the hands of others, as long as the private key is safe within your windows OS, only you would be able to login to the remote server using SSH protocol. 

Now to simply use the SSH protocol to have access to the remote server from your local machine (windows OS). Execute this command in gitBash or PuTTY: 

				
					ssh daniyalakbar@192.168.170.128 
				
			

After you have run the above command, you’ll notice that it will log into the remote server without asking for any password. This is because it already authenticated you because your local machine running windows had the Private Key with it. It will look like this.

Securely Accessing Remote Server Without Password Using SSH Protocol
Securely Accessing Remote Server Without Password Using SSH Protocol

As you can see, I’ve logged into my Linux remote server. And now I have full access to all the contents in the server machine. 

How to Transfer Data from Local Machine to Remote Server Using SSH Protocol:

It’s really easy to do this actually, once you’ve successfully managed to connect the machines using SSH. We will use the command scp which stands for Secure Copy. Let’s have a breakdown of how scp command works.  

Describing scp Command for SSH Protocol
Describing scp Command for SSH Protocol

Enough talking, Let’s go ahead and send some file.  

Transferring data from Local Machine to Remote Server Using SSH
Transferring data from Local Machine to Remote Server Using SSH

In the above case, I’ve created a blank text file named “abc.txt” which is in my windows local machine, and I need to send this to my remote server in the desktop folder.  

As you can see on the right side of the above image, it’s my Linux VM machine which initially did not have anything in the desktop directory and then on the left side, I have my windows gitBash terminal opened. Where I’ve used the SCP command to send the text file to the Linux server, after which, when I executed the command for listing all the files, the remote server indeed had that text file “abc.txt” inside the Desktop directory. 

 

And that’s all! 

I hope you understood the concept of SSH and how to send or transfer files to remote servers using SSH protocol. Leave your reviews in the comments below.  

Have a great one!