SSH: Few basics and real-time usages
SSH (Secure Shell) protocol, SSH is a software package that enables secure system administration and file transfers over insecure networks. It is used in nearly every data center and in every large enterprise.
In the beginning it was bit difficult to understand as it requires multiple systems and that too ssh enabled to interact and to play with.So after some trial and errors I am quite comfortable with it and will be shaing few tips and steps to understand and work with it, and yes, SSH simplifies lot of mundane manual tasks easily.
Step 1: making sure all required softwares/packages are present
As I was using Windows as my main machine and wanted to interact with Linux servers either running in VMs or in cloud making sure all required softwares/packages are present is a must.
In any ubuntu based distros, making sure ssh is installed and enabled is by running below command:
sudo apt update && sudo apt install openssh-server
- Once installed, the SSH service should be started automatically. If necessary, you can start (or stop, restart) the service manually via command:
$ sudo service ssh start - Verify that ssh service running
$ sudo systemctl status ssh - Configure firewall and open port 22
Before enabling the UFW firewall we need to add a rule which will allow incoming SSH connections. if UFW firewall is enabled before allowing incoming SSH connections you will no longer be able to connect to your Ubuntu server.
To configure your UFW firewall to allow incoming SSH connections, type the following command:
$ sudo ufw allow ssh
- Now we can enable UFW firewall by typing:
$ sudo ufw enable - You can check the status of UFW with the following command:
$ sudo ufw status
Step 2: Creating SSH keys and transfering it to guest/VMs
From recent windows updates SSH tool comes pre-installed, hence no need of putty anymore, we can directly cmd or powershell or my favorite git-bash.
Create a ssh files:
There are multiple ways to create ssh keys and this is the simplest one.
Open up git-bash and enter : ssh-keygen
You can enter the passphrase or skip it by pressing enter
That’s it, you should have 2 keys one *.pub(Public key) and 1 private key in your by default %USERPROFILE%/.ssh folder or any location where you have specified
Step 3: copying ssh keys to server
go to .ssh folder in your %USERPROFILE%
Run below cmd in git-bash:
ssh-copy-id -i ubuntu@192.168.1.200 (replace 192.168.1.200 with your respective server’s ip), It will ask for password once to get access to server/vm and copy ssh pub file.
One thing that took me a lot to grasp is where exactly these keys are stored in vm, and it turns out all ssh keys are stored in below default location on server.
~/.ssh/authorized_keys
Step 4:Access your vm’s via ssh(passwordless)
From now on its very easy to interact with your server/vm:
Just type:
Ssh username@serverip <eg:- ubuntu@192.168.1.200>, and you should be logged in to your guest/vm.
If you have multiple keys and servers to interact with frequently, it’s better to create a config file, which will help you name the servers and hence helps to connect to desired servers quickly.
Create a file with name config, make sure it has no extensions just a config.
Config File’s contents:
Host ubuntuSrv
HostName 192.168.1.234
User ubuntu
IdentityFile C:\Users\username\.ssh\KeyFileHost mint
HostName 192.168.1.114
User mintHost awsVM
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa_vps
IdentityFile ~/.ssh/id_rsa_vps : use this to add your ssh keys saved in different location
This way using config file configuration, you should be able to just pass server name to interact like in my case:
$ ssh ubuntusrv
These are some of the basics one needs to know to interact using ssh keys.
Though the concepts are simple, it took me a while to understand it, hope this article helps to resolve some of your confusions/doubts.