Install SSH server Ubuntu 22.04

SSH is the primary method of remote access and administration on Ubuntu 22.04 Jammy Jellyfish and other Linux systems. SSH is a client-server service providing secure, encrypted connections over a network connection. After installing Ubuntu 22.04, it may be one of the first things you want to configure.

In this guide, we’ll go over the step by step instructions to install and configure SSH on Ubuntu 22.04. This will help you whether you just want to connect to remote systems via SSH or you want your own system to accept incoming connections as well.

In this tutorial you will learn:

  • How to install SSH client and server on Ubuntu 22.04
  • How to start and stop the SSH service
  • How to allow SSH service through ufw firewall
  • How to change the listening port of SSH server
Install SSH server Ubuntu 22.04
Install SSH server Ubuntu 22.04
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 22.04 Jammy Jellyfish
Software OpenSSH server and client
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install SSH server Ubuntu 22.04




Follow the step by step instructions below to get SSH installed on your system and use it to initiate remote connections or accept incoming connections.

  1. Install the OpenSSH Server package for your system by executing the following command in terminal.
    $ sudo apt install ssh
    
  2. The SSH service can be controlled through systemd. Use the following systemctl commands to configure and control the service.To start or stop the SSH server:
    $ sudo systemctl start ssh
    AND
    $ sudo systemctl stop ssh
    

    To restart the SSH server:

    $ sudo systemctl restart ssh
    

    To enable (make SSH start automatically at system boot), or disable the SSH server:

    $ sudo systemctl enable ssh
    AND
    $ sudo systemctl disable ssh
    
  3. Check whether the SSH server is running by using the following systemctl status command.
    $ systemctl status ssh
    


    The systemctl command output indicates that the SSH service is currently running
    The systemctl command output indicates that the SSH service is currently running
  4. If you have the default Ubuntu firewall ufw enabled, you will need to execute the following command to allow SSH connections through the firewall.
    $ sudo ufw allow ssh
    
  5. Connect from a remote client to your SSH server. First, obtain an IP address of your SSH server. To do so execute the below ip command:
    $ ip a
    

    In case you wish to connect to your SSH server over the internet, you well need to obtain your external IP address:

    $ echo $(wget -qO - https://api.ipify.org)
    

    Obtaining local IP address with ip command on Ubuntu 22.04
    Obtaining local IP address with ip command on Ubuntu 22.04

Connect to Ubuntu 22.04 SSH server




After following the steps above, we can connect to our SSH server remotely by using the following ssh command syntax:

$ ssh username@hostname-or-ip-address

For example, the following command will connect to the Ubuntu 22.04 SSH server with an IP address 10.0.2.15 as the user linuxconfig:

$ ssh linuxconfig@10.0.2.15
Connecting to SSH server remotely. For a first time connection you will need to accept ssh fingerprint by typing "yes"
Connecting to SSH server remotely. For a first time connection you will need to accept ssh fingerprint by typing “yes”

Change default SSH port

This is optional, but for security reasons it is recommended to change the default SSH port 22 to some other arbitrary port number above 1024.

To do so, edit the /etc/ssh/sshd_config configuration file as an administrative sudo user. For example, to change the default SSH port number from 22 to 7575 (random port number) open the SSH config file and make the following change.

  1. Edit the SSH config file with nano or your preferred text editor.
    $ sudo nano /etc/ssh/sshd_config
    
  2. Then, change the #Port 22 line to Port 7575.
    Port 7575
    


    Changing the default SSH port number via SSHD configuration file
    Changing the default SSH port number via SSHD configuration file
  3. Once you have made the appropriate change, open a firewall port to correspond with the new SSH port:
    $ sudo ufw allow 7575/tcp
    
  4. To apply the change to your SSH server, use this systemctl command to restart it:
    $ sudo systemctl restart ssh
    
  5. In order to remotely connect to a specific SSH Server port number, use the -p ssh command line option. Example:
    $ ssh -p 7575 linuxconfig@10.0.2.15
    

Closing Thoughts

In this guide, we saw how to install an SSH server on Ubuntu 22.04 Jammy Jellyfish Linux. We also learned how to control the service with systemctl commands to start, stop, restart, enable, or disable the service. Lastly, we went over the steps to change the default SSH port, which is recommended for increased security. Remember to always use a good password for users on your system, so the accounts don’t get compromised through SSH.