Server AdministrationUpdated November 24, 2026·5 min read

How to Enable and Secure SSH on Ubuntu

Three commands enable SSH on Ubuntu:

sudo apt install openssh-server
sudo systemctl enable --now ssh
sudo ufw allow OpenSSH

To secure SSH on Ubuntu, you need a few more steps, and the order matters. Get key login working, then switch off password and root login, and only then think about moving the port. Done in the wrong order, each of those steps can lock you out of your own server. The steps below keep you safe at every point.

Before you start: the lockout rule

Keep your current SSH session open until you have tested each change from a second terminal. If a setting is wrong, that open session is how you fix it.

If you do get locked out, you are not stuck. On our servers, a VPS has a web-based VNC console and a dedicated server has IPMI/KVM remote management, and both give you a login prompt without SSH.

Step 1: Install and start the SSH server

Ubuntu Server does not always include the SSH server. Install it, start it, and make it start at boot:

sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
sudo systemctl status ssh

The status output should say active (running). If package management is new to you, our complete apt guide explains how it works.

Step 2: Open the firewall

Installing the server does not open a firewall port. If ufw is active, it drops SSH connections without any message, even while the service runs. Run sudo ufw allow OpenSSH, which uses the application profile that the package ships with:

sudo ufw allow OpenSSH
sudo ufw status

Step 3: How to secure SSH on Ubuntu with key login

A password can be guessed. A key cannot, in any practical sense, which is why key login is the foundation of SSH security. On your own computer, create a key pair and copy the public half to the server:

ssh-keygen -t ed25519
ssh-copy-id user@host

Now open a second terminal and log in. If you get in without being asked for the account password, the key works. Do not continue until it does, because the next step removes your fallback.

Step 4: Turn off password and root login

First make sure you can log in as a normal user who can run sudo. Once root login is off, that account is your way in. If you do not have one yet, see how to add a user to sudoers on Ubuntu and test it before you continue.

Both settings live in the sshd_config file, the SSH server's main configuration:

sudo nano /etc/ssh/sshd_config

Set these two lines:

PasswordAuthentication no
PermitRootLogin no

Check the file for errors, then restart. A broken file can fail to load with no obvious message, so the check is worth the two seconds:

sudo sshd -t
sudo systemctl restart ssh

Test a fresh login from the second terminal before you close the first.

Step 5 (optional): Change the default SSH port

Moving SSH off port 22 cuts the number of automated login attempts in your logs. It is not a security measure on its own: key login and disabled passwords are what protect you. If you want the quieter logs, follow this order.

  1. In /etc/ssh/sshd_config, set a port above 1024, for example Port 2222.

  2. Check the file, open the new port, and restart:

    sudo sshd -t
    sudo ufw allow 2222/tcp
    sudo systemctl restart ssh
    
  3. Confirm which port SSH is listening on (our Linux network commands cheat sheet covers ss and related tools):

    sudo ss -tlnp | grep sshd
    

    On recent Ubuntu releases, SSH can be started by a systemd socket (check your Ubuntu version if you are not sure which one you run). If it still listens on port 22, run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket, then check again.

  4. From the second terminal, connect on the new port:

    ssh -p 2222 user@host
    
  5. Only once that works, close the old port:

    sudo ufw delete allow 22/tcp
    

To skip typing -p every time, add this to ~/.ssh/config on your own computer, then connect with ssh myserver:

Host myserver
    HostName host
    Port 2222

When something goes wrong

Symptom Likely cause Fix
Connection timed out Firewall blocks the port, or SSH listens on a different port Check sudo ufw status and sudo ss -tlnp
Connection refused The SSH service is not running sudo systemctl status ssh, then sudo systemctl enable --now ssh
Permission denied (publickey) The key is not in ~/.ssh/authorized_keys for that account Add it from your open session or the console
A change did nothing The service was not restarted, or the file has an error sudo sshd -t, then sudo systemctl restart ssh

Where to run it

SSH is usually the only way into a server without a screen. Every OrbitServers VPS comes with full root access, so you can change sshd_config and the firewall exactly as shown here. See current OrbitServers VPS plans.

Was this article helpful?

Written by Julius · Product Manager