SSH is a network protocol that allows you to connect to your Linux machine. If you are someone working in IT you know servers are present in data centers and to connect with the server you need to use SSH protocol. It is the responsibility of the administrator to install an SSH server and secure it from external intrusion.
In this article, I will walk you through how to install the OpenSSH server in Ubuntu 20.04.
Every Linux machine has an SSH client installed by default. You will connect to any remote server using this SSH client. I am having a fresh copy of the Ubuntu 20.04 machine, which is I am using for demonstration purposes. It is running with IP 192.168.1.7. If I have to connect to this machine from my host machine SSH server should be running. The SSH server will listen at port 22.
Install SSH in Ubuntu
Run the following command to check if the OpenSSH client is installed or not.
$ sudo apt list openSSH-*
If I try to connect to my machine, the connection will be refused since the OpenSSH server is not running and port 22 is not listening.
$ ssh firstname.lastname@example.org ssh: connect to host 192.168.1.7 port 22: Connection refused
To install the OpenSSH server run the following command. You should run the installation command using root or with sudo privilege.
$ sudo apt update $ sudo apt install openssh-server -y
Once the installation is completed you can check the status of ssh service by running the following command. By default, ssh is started after install and enabled to start during reboots.
$ systemctl status ssh
Change SSH Port in Ubuntu
As I said, SSH runs on port 22, which you can confirm by running the following netstat (network statistics) command.
$ sudo netstat -tlnup | grep -i :22
To change the default SSH listening port open the sshd_config file and change port 22 to a different port. I am changing the port to 2222. Restart ssh service once changes are made.
$ sudo nano /etc/ssh/sshd_config $ systemctl restart ssh
Now you can check the listening port for ssh by running the netstat command. From the below image you can see ssh now listens at port 2222.
$ sudo netstat -tlnup | grep -i 22
Now from any other machine, we can connect to this machine. By default SSH client will try to connect to port 22 of ssh server. Since the port is changed from 22 to 2222 we should tell ssh to connect to port 2222 using
$ ssh username@hostname -p 2222
From the above image, you can see when I try to connect by default it connects to 22 and refuses the connection. When I used port 2222 connection is successful.
If you have ufw firewall enabled you have to add a rule for SSH connection to pass successfully through the firewall.
$ sudo ufw allow ssh $ sudo ufw status
That’s it for this article. When installing OpenSSH it is also necessary for you to know how to harden it so your Linux machine always stays secure.