SSH or Secure Shell is a network communication protocol used for remote login or controlling other remote computers. SSH is used to create a secure connection between the local computer and the remote server. In short, SSH protocol connects two computers securely over an unsecured network. Ubuntu does come built-in with an SSH server but it can be installed manually through the official repository. This article will cover the detailed procedure to install an SSH server on Ubuntu.

Quick answer

To install SSH server on Ubuntu 24.04, run sudo apt install openssh-server, then check it with sudo systemctl status ssh. If UFW is enabled, allow SSH with sudo ufw allow OpenSSH before connecting from another computer.

How to Install SSH Server on Ubuntu 24.04

SSH server does not come pre-installed on Ubuntu and it is not readily available to be installed. However, Ubuntu includes an SSH server in its official repository and it can be installed using the apt package manager. Check the following steps to install the SSH server on Ubuntu 24.04.

Step 1: Update System Repositories

Before installing the SSH server on Ubuntu, make sure all the local packages are up to date. To do so, press CTRL + Alt + T shortcut key to open the Terminal and run this command:

sudo apt update && sudo apt upgrade
Terminal output of sudo apt update and upgrade before installing OpenSSH on Ubuntu 24.04

Step 2: Install SSH Server

Once the local repositories are updated, install the SSH server on Ubuntu by running the below command:

sudo apt install openssh-server -y
Terminal output of sudo apt install openssh-server on Ubuntu 24.04

Step 3: Enable SSH Server

After installing the SSH server, make sure that the SSH server is enabled. If the SSH server is not enabled, run this command to enable it:

sudo systemctl enable ssh

Note: When the SSH server gets enabled, it will continue to run whenever Ubuntu boots up:

Terminal output of sudo systemctl enable ssh on Ubuntu 24.04

Note: To disable the SSH server on Ubuntu, run this command:

sudo systemctl disable ssh

Disabling the SSH server will also stop it from starting at boot:

Step 4: Start the SSH Server

After enabling the SSH server, you can start the SSH server by running this command:

sudo systemctl start ssh
Terminal output of sudo systemctl start ssh on Ubuntu 24.04

Alternatively, you can also enable and start the SSH server immediately with a single command, which is given below:

sudo systemctl enable --now ssh
Terminal output of sudo systemctl enable --now ssh on Ubuntu 24.04

Step 5: Verify SSH Server Installation

Once the SSH server is started, you can verify its installation and running status by executing the below command:

sudo systemctl status ssh
Terminal output of sudo systemctl status ssh showing OpenSSH server active on Ubuntu 24.04

How to Configure or Enable the Firewall on Ubuntu 24.04

Till now you have installed the SSH server on Ubuntu and made it run successfully. Now is the time to use the SSH server to establish a connection with the remote computer or server. But, before making a connection with the server, make sure that ufw (Uncomplicated firewall) is active and able to pass the traffic to the SSH server. To enable the ufw firewall and pass the traffic to the SSH server, check the following steps.

Step 1: Enable the UFW Firewall on Ubuntu

First, enable the ufw firewall on Ubuntu for the SSH server by running this command:

sudo ufw enable
Terminal output of sudo ufw enable activating the firewall on Ubuntu 24.04

Step 2: Allow the SSH Server to Pass the Traffic through the Firewall

Once the firewall is enabled, execute the given command to allow the inbound SSH connections so that traffic can pass through it:

sudo ufw allow ssh
Terminal output of sudo ufw allow ssh adding SSH firewall rule on Ubuntu 24.04

Step 3: Check Firewall Status

After activating the firewall, verify whether it is running and passing the traffic to the SSH server through the execution of the below command:

sudo ufw status
Terminal output of sudo ufw status showing SSH allowed on Ubuntu 24.04

Note: To disable and remove the firewall from Ubuntu by running this command:

sudo ufw delete allow ssh

How to Connect to the Remote Server using SSH on Ubuntu 24.04

Upon completing the previously provided steps, you have installed and enabled the SSH server on Ubuntu. Now, you are all set to connect and log in to the remote server using the SSH server protocol. To log in to the remote server, you must have the server IP address or the domain name, and the user created on the server.

Note: The SSH server must be installed on both the remote computer and the computer through which the user is making a connection.

To log in to the remote server using the IP address, run this command:

ssh username@ip-address

To log in to the remote server using the domain name, run this command:

ssh username@domain-name

Set up SSH key authentication

SSH key authentication is more secure than passwords and eliminates the need to type a password each time you connect. The process generates a key pair: a private key that stays on your local machine, and a public key that you copy to the server.

On your local machine, generate an Ed25519 key pair:

ssh-keygen -t ed25519 -C "[email protected]"

Press Enter to accept the default file location (~/.ssh/id_ed25519). Adding a passphrase is optional but adds a second layer of protection if your private key is ever exposed.

Copy the public key to the remote server:

ssh-copy-id username@server-ip

This appends your public key to ~/.ssh/authorized_keys on the server. Test that key-based login works before making any further changes:

ssh username@server-ip

Once key login is confirmed, you can disable password authentication to block brute-force attacks. Open /etc/ssh/sshd_config and set:

PasswordAuthentication no

Save the file, then restart SSH for the change to take effect:

sudo service ssh restart

How to Configure SSH Server on Ubuntu 24.04

Besides installing and using the SSH server to connect with the remote server, you can also configure or make changes to it. Configuration is often practiced to secure the SSH server by changing its ports and passwords. The SSH server’s configurations are stored in the sshd_config file which you can edit through the nano editor.

However, it is recommended to create a backup of the sshd_config file before making any changes, so in case you corrupted the original file you always have its copy. To create a backup of the sshd_config file run this command:

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.initial
Terminal output of sudo cp creating a backup of sshd_config on Ubuntu 24.04

Once the backup of the sshd_file is created, run this command to edit the sshd_file with the nano editor to configure it:

sudo nano /etc/ssh/sshd_config

In the sshd_file configuration file, first of all, change the port number to a more secure one. For instance, change the port number to 50782 to make it more secure. To do so, uncomment the Port line by removing the # symbol and specify the port number 50782:

Note: It is recommended to set the port number from the range of 49152 to 65535:

Nano editor showing Port 50782 configured in sshd_config on Ubuntu 24.04

Moving forward, change the password authentication mode to a more secure one. To do so, uncomment the PubkeyAuthentication by removing the # symbol and specifying the value Yes, as shown below:

Nano editor showing PubkeyAuthentication yes in sshd_config on Ubuntu 24.04

You can also remove the exception of only allowing the root user to login to the server, by uncommenting the PermitRootLogin section and specifying the value no to it, as shown below:

Nano editor showing PermitRootLogin no in sshd_config on Ubuntu 24.04

Additionally, you can also prohibit the users from login to the server with empty passwords. To do so, locate the PermitEmptyPasswords section and uncomment it by removing the # symbol and specify the value No to it, as shown below:

Nano editor showing PermitEmptyPasswords no in sshd_config on Ubuntu 24.04

You can set the maximum number of authentication entries to stop the user from coming up with different combinations. Doing so will protect your system from being used by unauthorized users who do not have the password and save your important data from stealing. To set the maximum number of authentication entries, simply uncomment the MaxauthTries by removing the # symbol and providing it the number of maximum tries, for instance, I have set the maximum authentication tries to 6, as shown below:

Nano editor showing MaxAuthTries 6 in sshd_config on Ubuntu 24.04

To save the changes made to the configurations file, press the CTRL + O shortcut and then press the Enter key.

Once you are done with the SSH configuration settings, run this command to restart the SSH service to save the changes made to the configuration file:

sudo service ssh restart
Terminal output of sudo service ssh restart on Ubuntu 24.04

Common SSH commands on Ubuntu 24.04

Task Command
Connect to a server ssh username@ip-address
Connect on a custom port ssh -p 50782 username@ip-address
Copy a file to the server scp file.txt username@ip-address:~/
Copy a file from the server scp username@ip-address:~/file.txt .
Run a single command remotely ssh username@ip-address "ls -la"
Check SSH service status sudo systemctl status ssh

How to Uninstall SSH Server from Ubuntu 24.04

If you are done with using the SSH server or want to remove it from Ubuntu for any other reason. Check the following steps to uninstall the SSH server from Ubuntu 24.04.

Step 1: Stop the SSH Server

To remove the SSH server, first, stop the SSH server on Ubuntu from running, to do so run the below command:

sudo systemctl stop ssh
Terminal output of sudo systemctl stop ssh on Ubuntu 24.04

Step 2: Disable the SSH Server

Once the SSH server is stopped, then, execute the below command to disable the SSH server on Ubuntu:

sudo systemctl disable ssh
Terminal output of sudo systemctl disable ssh on Ubuntu 24.04

Step 3: Uninstall SSH Server from Ubuntu

After stopping and disabling the SSH server, you can uninstall the SSH server from Ubuntu by running this command:

sudo apt remove openssh-server -y
Terminal output of sudo apt remove openssh-server on Ubuntu 24.04

Before opening SSH access

SSH makes remote login easy, but it also exposes a login service to the network. Use strong account passwords or SSH keys, keep the server updated, and only allow SSH through the firewall when you actually need remote access.

For a home or lab machine, confirm the Ubuntu computer’s IP address before connecting. For an internet-facing server, review the SSH configuration carefully and avoid enabling password login unless you understand the risk.

Related Ubuntu 24.04 guides