How to enable SSH on Ubuntu (for 20.04, 22.04)

Enable SSH on Ubuntu 20.04 and 22.04

It is possible to enable SSH on Ubuntu 20.04 and Ubuntu 22.04 in only six steps. Secure Shell (SSH) is a network protocol that allows secure remote access to servers and other devices over an unsecured network. It’s an essential tool for system administrators and developers who need to manage their servers remotely. SSH default port is 22, but it can also be changed in the configuration file. The ssh daemon ‘sshd’ is running in the background listening for incoming SSH connections. Enabling SSH on Ubuntu 20.04 and the newer version, Ubuntu 22.04, is a straightforward process, and this guide will walk you through the steps.

Step 1: Update Your System

Before enabling SSH, it’s always a good practice to ensure your system is up-to-date. Open a terminal on your Ubuntu system and run the following commands:

# apt update
# apt upgrade

Step 2: Install SSH Server

SSH server should be installed on your Ubuntu system by default. However, if for some reason it’s not installed, you can install it using the following command:

# apt install openssh-server

After installing the SSH server, you can check its status to ensure it’s running. You can run the following command:

# systemctl status ssh

You should receive a similar output:

ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
     Active: active (running) since Fri 2024-01-12 23:40:03 UTC; 1 month 7 days>
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 665 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 732 (sshd)

If it’s not running, you can start it using: sudo systemctl start ssh

Step 3: Configure SSH (Optional)

While SSH comes with sensible default configurations, you can still edit the configuration according to your needs. The SSH server configuration file is located at /etc/ssh/sshd_config. You can edit this file using your favorite text editor:

# nano /etc/ssh/sshd_config

To change the SSH port number, you will need to edit the Port 22 line in this file, writing the port that you want to use instead of port 22. You can also allow or block SSH root login by editing the line:

PermitRootLogin yes

Make sure to save your changes and restart the SSH service for the modifications to take effect

# systemctl restart ssh

You can also generate an SSH key-pair and copy it to the remote server to log in using an SSH key. To generate the SSH key use the following command:

# ssh-keygen

You will be prompted where to save the key, you can click Enter to use the default location:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/john/.ssh/id_rsa):

Then you will be asked to enter a passphrase twice that will be used to log in with the SSH key:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

You should receive a similar output:

Your identification has been saved in /home/john/.ssh/id_rsa
Your public key has been saved in /home/john/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:ka16bpyjT0beec..

To transfer the SSH key to the remote server, run the following command:

# ssh-copy-id greg@remote_ip

You will be prompted to enter the password of the remote server, in this case for the user greg. And you should receive a message that one key has been added. To test, you can try logging in again with ssh greg@remote_ip.

Step 5: Allowing SSH Through the Firewall

If your Ubuntu system has a firewall enabled, such as UFW, CSF or even iptables rules that block the SSH port. You’ll need to allow SSH connections. For ufw, you can do this by running:

# ufw allow ssh

This command will enable SSH traffic on the default SSH port (22). If you’ve changed the SSH port in the configuration file, you need to specify that port instead. To allow the port 22 in your iptables, you can run the following command:

# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Step 6: Testing the SSH Connection

Once SSH is enabled and configured, you can test the connection from another machine. Use the following command to connect to your Ubuntu system via SSH:

# ssh username@hostname_or_IP

Replace username with your username on the Ubuntu system and hostname_or_IP with the hostname or IP address of your Ubuntu system. You’ll be prompted to enter your password, and upon successful authentication, you’ll gain remote access to your Ubuntu server.

To enable SSH on Ubuntu 20.04 and Ubuntu 22.04 is a straightforward process that involves installing the SSH server package, ensuring it’s running, configuring it if necessary, and allowing SSH traffic through the firewall. Once set up, SSH provides a secure and efficient way to manage your Ubuntu servers remotely. If you like this post, please share it with your friends if they’re having trouble with enabling SSH.

Leave a Comment