Securely connecting two computers using SSH is fairly simple and powerful. But if you are new to the world of computers, especially GNU/Linux, it could be a bit tricky to get around some basic stuff. In this tutorial, I’m going to teach you the step-by-step process to communicate between two computers using SSH.
Basics
The idea is pretty simple. There are two Linux computers; let’s call them A and B. To access computer A from B (connecting to A using B’s machine), we need to place B’s public key inside A. Likewise, if we want to access B from A (connecting to B using A’s machine), we need to place A’s public key inside B. Additionally, we need to install OpenSSH client and server on both computers. After that, the ssh service should be running when we attempt to connect to either computer.
Installation
If you are using Debian-based operating systems such as Ubuntu, Linux Mint, Pop!_ OS etc., you can use the following command:
sudo apt install openssh-client openssh-server
If it’s already installed. Well, that’s good; you can proceed to the next step. Make sure you install them on both computers.
Now create ssh keys on computer A. In order to do that, type the following command:
ssh-keygen -t rsa -b 4096 -C "For Computer B"
-t: Specifies the key type (RSA in this case).
-b: Specifies the key length (4096 bits is a good choice for security).
-C: Provides a comment for the key (usually your email address, but for our
convenience I've written "For Computer B").
And now repeat the process on computer B (i.e., generating ssh keys)
The ideal place to store your ssh keys is ~/.ssh directory. If you don’t have .ssh already on your home directory, you can create it by typing the following commands:
cd
mkdir .ssh
Remember, .ssh will be a hidden directory. If you want to make it visible on your
file manager, press Ctrl+h
or if you want to see all the hidden files and directories on
terminal, type ls -a
.
In your .ssh directory, you can see your public and private keys, typically named as id_rsa.pub (public key) and id_rsa (private key). You should never ever share your private key with anyone.
Now create a file named authorized_keys on both machines. And that’s where the public keys of both machines go. Copy computer A’s public key to authorized_keys of computer B and copy computer B’s public key to authorized_keys of computer A. Now the ground is set.
To enhance security, type the following commands:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Next, start the ssh service on both computers. To do that, type the following command:
sudo systemctl start sshd
or
sudo systemctl start sshd.service
If you want to start sshd service automatically when you boot up your computer, type the following command:
sudo systemctl enable sshd
or
sudo systemctl enable sshd.service
To connect to either computer, you need to know their username and private or public ip address. If your computers are on the same LAN, just type the following command:
ifconfig
If you are connecting to the Internet via WiFi, you can check the result
starting from wlan0 (or something similar to that, such as wlan1 and so on),
there you’ll see inet 192.168.x.x
and that’s your IP address.
If the remote computer is not on the same LAN, you can get the IP address by
typing curl ipinfo.io
on your terminal or remote computer’s terminal. Or you
can just visit whatsmyip.com to get your public IP.
Disclaimer: Connecting to a remote computer using **public** IPs could have some complications depending on the remote computer's firewall settings, network issues, etc., In that case, further reading and debugging skills are required.
Once you know your IP addresses on both of your computer, it’s time to connect.
If you want to connect to computer B from computer A, you type the following command:
ssh <username of Computer B>@<IP address of Computer B>
For example,
ssh [email protected]
If you want to connect to computer A from computer B, you type the following command:
ssh <username of Computer A>@<IP address of Computer A>
For example,
ssh [email protected]
After you connect to either computer, you can do all the things on the remote computer’s shell as a legit user.
That’s all. You can also check out my other post on how to manage multiple SSH keys to supplement that with this blog.
Follow me on other social media