How to set up an SSH connection to a Linux server
In this article, I’ll explain how to install and set up an SSH environment on a Linux server. When we use a virtual machine or server, we very often want to access those remote hosts using SSH. This article will explain how to initialize and set up those SSH connections. Once you read this, you’ll be able to install SSH on your Linux machine and make a connection to that machine.
Table of Contents
Below is the list of the environments that I use in this article.
- SSH host (Ubuntu20.04)
- SSH client(Macbook)
Step1: Install SSH server on the SSH host
First of all, we need to be sure that the SSH host has installed the
openssh-server, and if it hasn’t, we need to install it. To check if the host has installed it, run the command below on the SSH host. If your SSH host has already installed that package, you can skip this section and go to the next step.
$ apt list --installed | grep openssh-server (If you use CentOS or other RedHat based distribution, use the command below instead) $ yum list installed | grep openssh-server
If you can’t see any results, then it means the host doesn’t have the package “openssh-server”, so we need to install it. Run the command below on the SSH host and install the package.
$ sudo apt install -y openssh-server (If you use CentOS or other RedHat based distribution, use the command below instead) $ sudo yum install -y openssh-server
After you run the command above, please re-run the first command again. This time, you’ll see a result like below.
openssh-server/focal-updates,focal-security,now 1:8.2p1-4ubuntu0.2 amd64[installed]
OK, now the installation is done properly, let’s move to the next step!
Step2: Connect to the remote host using SSH
Now the host is ready to be connected through SSH, so let’s use SSH to connect to the server. The usage of the SSH command is
User name is the name of the user that you want to log in to.
IP address is the IP address of the remote host. Run the command below on the SSH host to check the public IP address of the SSH host.
$ ip a
You will see a result like below. As you see, in my case, the IP address is 192.xxx.xxx.xxx.
Now, we got the IP address, so run the command below on the SSH client and connect to the SSH host. Replace the user name and IP address with yours.
$ ssh email@example.com
If this is the first time to connect to the host, you will be asked to confirm the connection. So in that case, just type “yes” and make the connection.
Congratulations, we could successfully connect to the remote host through SSH. In this article, we explained how to set up the SSH server and how to connect to the SSH host. However, the default setting of the SSH server is not secure and vulnerable to cyber-attacks. So the next step is to modify the configuration so that we can have a secure connection to the remote host.