SSH is a famous way to connect to remote machines securely and easily. You can use SSH to connect to your cloud server/VM or a friend’s PC or almost anyone remotely to perform certain activities you want. For instance, I use SSH to connect to my Google Cloud Console so that I can manage my VM via my terminal rather than opening an ssh connection in the browser.
Your reason can be different but in the end, the goal is to connect to a remote machine locally. There are different ways on how you can connect to a remote server via SSH but as per me, the best way is to do it with SSH keys. SSH keys are the best as they are secure, easy to set up and can be publicly shared too (not private keys though).
I use SSH keys to access my Sourceforge, GitHub, Servers and lot more. Now, without wasting any more time let’s see how to set up and use SSH.
First of all, install OpenSSH server which will allow you to use SSH.
sudo apt-get install openssh-server
Now, let’s proceed to set up SSH.
Setting up SSH
Now to use SSH you are required to authorize your machine via SSH keys so that it can connect to the remote machine. To do this, first of all, you will be required to generate SSH keys on your local machine. Enter the below command to generate SSH keys.
I will recommend to stick with defaults and leave every field empty for the sake of this tutorial.
There will be two keys i.e. private key and public key. Remember that you should not share the private key with anyone unless you know what are you doing. Public key can be shared and there is absolutely no harm even on pasting it on your profile. We will require your public key for further setup. Get your public key by the command below.
Your key will be really long and will start from ssh -rsa and will contain the name of your PC too. Copy it all as you will be required to paste it on the remote server to allow access to your machine.
Visit your remote server now. In case the remote server is of a friend, relative or someone else you can share your public key and ask them to run the below command.
echo your_key public_key_string >> ~/.ssh/authorized_keys
Replace your_key with the key you just copied. You can check my example in the image below.
This command will authorize your computer to connect to the remote computer. Now you will require the IP address of the remote computer and the username you want to use. You can view the IP address of the remote computer using any of the below commands. Again, if the remote PC belongs to someone else, ask them to do this part and tell the IP address to you. Remember not to share the IP address with anyone unless you know what you are doing.
ifconfig or ip addr show
Your IP address will be listed as ip addr. In case there is more than one IP address, just share the first one. In case you are using Google Cloud Computing, you can view your IP address of the VM as an external IP in the panel.
Now it’s time to connect to the remote machine. I expect that all settings already default and you haven’t messed with the configurations of SSH yet.
Replace username with the name of the user you want to connect as and replace ipaddr with the IP address you just obtained.
You might see a warning. Enter your confirmation as yes and if everything goes right you will be in. If you are going to use SSH to connect to that remote machine, again and again, I recommended you to better create a script with the command and add the script as an alias in bashrc. This is just optional and will speed things up next time you want to connect to the same machine.
Using SSH is a very easy and secure way to connect. SSH keys are used almost everywhere and you will need them at some point in time. I recommend having a high-speed internet connection at both ends to avoid any speed related issues. There could be possible errors which I might not have encountered. Feel free to ask your doubt and I will resolve it ASAP. Till then, keep connecting via SSH ????