What is SSH and how to transfer files with SSH from one machine to another? It’s very easy and highly suggested because it’s fast and more importantly it’s secure.
So stay sharp cause we will be going through step by step how to transfer data from localhost to a remote server using SSH protocol.
SSH stands for Secure Shell and the SSH protocol port is 22. SSH is a very popular network communication protocol used for communication between 2 machines.
They can both be on different servers; can both be the 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 execute shell commands remotely.
Prerequisites!
- Familiar with bash scripting shell commands. (Check Here)
- Should know how VMWare works
- Should have git installed and thus have access to GitBash or PuTTY would also work
Why use SSH to transfer files instead of other tools?
In a sentence, to prevent interception.
It’s because the data you transfer has a high risk of getting intercepted or “tapped” by someone leading to data leaks.
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.
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 connect 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).
First, we create public 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 the windows search bar) and execute the following command:
ssh-keygen –t rsa –b 4096
It’s going prompt you with some questions, just hit enter on all of them and public and private keys would be generated.
The above image shows the public and private keys generated, along with their stored location.
Now, spin up the VM, restart the SSH service, and fetch the IP address and username. Like so:
The command “ip add” will show you the IP address and other configurations and the “whoami” command will tell you the current username which is ‘daniyalakbar’ in this case.
Now we must install and start the 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.
Next, 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.
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 copied the public key onto 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, simply use the SSH protocol to have access to the remote server from your localhost 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.
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 SCP command, which stands for Secure Copy.
Let’s have a breakdown of how the SCP command works.
Enough reading, let’s go ahead and transfer files with SSH protocol.
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. Initially, it did not have anything in the desktop directory.
On the left side, I have my windows gitBash terminal opened. Where I used the SCP command to send the text file to the Linux server.
Afterward, I executed the command for listing all the files, and 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 protocol and how to transfer files with SSH to remote servers. You might want to read How to link to GitHub with SSH.
Leave your reviews in the comments below. Have a great one!
Recent Comments
Categories
- Angular
- AWS
- Backend Development
- Big Data
- Cloud
- Database
- Deployment
- DevOps
- Docker
- Frontend Development
- GitHub
- Google Cloud Platform
- Installations
- Java
- JavaScript
- Linux
- MySQL
- Networking
- NodeJS
- Operating System
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Understanding Mutex, Semaphores, and the Producer-Consumer Problem
- 13 October, 2024
- 10 min read
Process scheduling algorithm – FIFO SJF RR
- 14 September, 2024
- 8 min read
How to Implement Multithreading in C Language
- 8 September, 2024
- 9 min read