How to Enable SSH on Ubuntu 24.04: 6 Easy Steps (Tested)

To enable SSH on Ubuntu you install openssh-server, make sure something is listening on port 22, and open that port in the firewall. On Ubuntu 24.04 there is a twist that older guides miss: SSH is started by socket activation (ssh.socket), so systemctl status ssh says "inactive" right after installation, and changing the port in sshd_config plus systemctl restart ssh does nothing. Every step below was tested on Ubuntu 24.04.5 LTS (OpenSSH 9.6p1, systemd as PID 1) with a second machine as the client.

sudo apt update && sudo apt install openssh-server
sudo systemctl status ssh.socket          # 24.04: the socket listens on port 22
sudo ufw allow OpenSSH                    # BEFORE "ufw enable"
ssh user@server-ip                        # from another machine
# changed Port in sshd_config? on 24.04:
sudo systemctl daemon-reload && sudo systemctl restart ssh.socket

Step 1: check whether you need to enable SSH on Ubuntu at all

A fresh minimal Ubuntu has no SSH server, so the first test before you enable SSH on Ubuntu is a login attempt. The client gets "Connection refused", which means nothing listens on port 22:

server$ systemctl status ssh
Unit ssh.service could not be found.
server$ ss -tlnp | grep :22 || echo nothing on port 22
nothing on port 22

client$ ssh ubuntu-admin@10.89.1.2
ssh: connect to host 10.89.1.2 port 22: Connection refused

"Connection refused" is answered immediately by the server: the host is reachable but no service is listening. A firewall that drops packets looks different ("Connection timed out", see step 4). Cloud images usually come with SSH already running; on a desktop or minimal install you have to enable SSH on Ubuntu yourself.

Step 2: install openssh-server to enable SSH on Ubuntu

server$ sudo apt update && sudo apt install openssh-server
Setting up openssh-server (1:9.6p1-3ubuntu13.20) ...
Created symlink /etc/systemd/system/sockets.target.wants/ssh.socket → /usr/lib/systemd/system/ssh.socket.
Created symlink /etc/systemd/system/ssh.service.requires/ssh.socket → /usr/lib/systemd/system/ssh.socket.

This is the moment most guides call "enable SSH on Ubuntu" done. Look at what was enabled: ssh.socket, not ssh.service. That is how Ubuntu 24.04 ships SSH, and it confuses the usual checks:

server$ systemctl is-enabled ssh.service ssh.socket; systemctl is-active ssh.service ssh.socket; ss -tlnp | grep :22
disabled
enabled
inactive
active
LISTEN 0      4096         0.0.0.0:22        0.0.0.0:*    users:(("systemd",pid=1,fd=36))
LISTEN 0      4096            [::]:22           [::]:*    users:(("systemd",pid=1,fd=38))

ssh.service is "disabled" and "inactive", yet port 22 is open, held by systemd (PID 1). The first connection starts sshd:

Enable SSH on Ubuntu 24.04: ssh.socket listens on port 22 and starts ssh.service on the first login
Real output: before the first login only systemd listens; after it, sshd runs.
client$ ssh ubuntu-admin@10.89.1.2 'echo logged in as $(whoami) on $(hostname)'
logged in as ubuntu-admin on ubuntu-srv

server$ systemctl is-active ssh.service ssh.socket
active
active
server$ systemctl status ssh --no-pager | head -4
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/usr/lib/systemd/system/ssh.service; disabled; preset: enabled)
     Active: active (running) since Sat 2026-10-17 02:24:43 UTC; 657ms ago
TriggeredBy: ● ssh.socket

So when you enable SSH on Ubuntu 24.04, check systemctl status ssh.socket or ss -tlnp | grep :22, not just ssh.service. TriggeredBy: ● ssh.socket confirms the setup.

Step 3: change the SSH port (the socket activation trap)

Many admins move the port right after they enable SSH on Ubuntu. With Port 2222 set in /etc/ssh/sshd_config, the classic restart did not move the port:

server$ grep -n '^Port' /etc/ssh/sshd_config
23:Port 2222
server$ sudo systemctl restart ssh; ss -tlnp | grep -E ":22 |:2222 "
LISTEN 0      4096         0.0.0.0:22        0.0.0.0:*    users:(("sshd",pid=779,fd=3),("systemd",pid=1,fd=36))
LISTEN 0      4096            [::]:22           [::]:*    users:(("sshd",pid=779,fd=4),("systemd",pid=1,fd=38))

client$ ssh -p 2222 ubuntu-admin@10.89.1.2
ssh: connect to host 10.89.1.2 port 2222: Connection refused

The listening socket belongs to ssh.socket, and its port comes from a generator that reads sshd_config at daemon-reload:

server$ sudo systemctl daemon-reload; sudo systemctl restart ssh.socket; ss -tlnp | grep -E ":22 |:2222 "
LISTEN 0      4096         0.0.0.0:2222      0.0.0.0:*    users:(("sshd",pid=809,fd=3),("systemd",pid=1,fd=25))
LISTEN 0      4096            [::]:2222         [::]:*    users:(("sshd",pid=809,fd=4),("systemd",pid=1,fd=38))
server$ cat /run/systemd/generator/ssh.socket.d/addresses.conf
# Automatically generated by sshd-socket-generator

[Socket]
ListenStream=
ListenStream=0.0.0.0:2222
ListenStream=[::]:2222

client$ ssh -p 2222 ubuntu-admin@10.89.1.2 'echo ok on 2222'
ok on 2222

If you prefer the old behaviour (sshd always running, restart ssh applies the port), switch off socket activation:

server$ sudo systemctl disable --now ssh.socket
Removed "/etc/systemd/system/sockets.target.wants/ssh.socket".
Removed "/etc/systemd/system/ssh.service.requires/ssh.socket".
server$ sudo systemctl enable --now ssh.service
Created symlink /etc/systemd/system/sshd.service → /usr/lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /usr/lib/systemd/system/ssh.service.
server$ systemctl is-active ssh.service ssh.socket
active
inactive

Both modes enable SSH on Ubuntu equally well; just know which one your server uses before you change the port. If you change it, also open the new port in the firewall first. The older way on CentOS is in centos change ssh port.

Step 4: allow SSH through the ufw firewall

ufw was installed but inactive on this server, the usual Ubuntu default. When you enable SSH on Ubuntu with ufw active, the order matters. Enabling ufw without an SSH rule is the classic way to lock yourself out:

server$ sudo ufw --force enable; sudo ufw status
Firewall is active and enabled on system startup
Status: active

client$ ssh ubuntu-admin@10.89.1.2
ssh: connect to host 10.89.1.2 port 22: Connection timed out

Now the error is "timed out", not "refused": ufw silently drops the packets. On a remote server you would have no way back except a console. Add the rule first, then enable ufw:

server$ sudo ufw app list; sudo ufw allow OpenSSH; sudo ufw status numbered
Available applications:
  OpenSSH
Rule added
Rule added (v6)
...
[ 1] OpenSSH                    ALLOW IN    Anywhere
[ 2] OpenSSH (v6)               ALLOW IN    Anywhere (v6)

client$ ssh ubuntu-admin@10.89.1.2 'echo ok through ufw'
ok through ufw

The OpenSSH profile opens port 22 only. With a custom port, use sudo ufw allow 2222/tcp. To slow down password guessing, ufw limit blocks an address that opens too many connections in a short time. Eight quick logins from the client:

server$ sudo ufw limit OpenSSH
client$ for i in $(seq 1 8); do ssh ... true && echo "$i ok" || echo "$i blocked"; done
1 ok
2 ok
3 ok
4 ok
5 ok
6 blocked
7 blocked
8 blocked

Connections 6 to 8 were dropped. Keep that in mind for scripts or Ansible runs that open many SSH connections in a row.

Step 5: key authentication and a safe sshd configuration

client$ ssh-keygen -t ed25519
client$ ssh-copy-id -i ~/.ssh/id_ed25519.pub ubuntu-admin@10.89.1.2
client$ ssh ubuntu-admin@10.89.1.2 'echo key login ok; ls -l ~/.ssh/authorized_keys'
key login ok
-rw-------. 1 ubuntu-admin ubuntu-admin 99 Oct 17 02:24 /home/ubuntu-admin/.ssh/authorized_keys

Password logins are fine to enable SSH on Ubuntu the first time, but once the key works, turn them off in a drop-in file instead of editing the main config. sshd -t checks the syntax and sshd -T prints the values sshd will really use:

server$ printf 'PasswordAuthentication no\nPermitRootLogin no\nKbdInteractiveAuthentication no\n' | sudo tee /etc/ssh/sshd_config.d/10-hardening.conf
server$ sudo sshd -t && echo syntax ok; sudo systemctl restart ssh; sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin|kbdinteractiveauthentication|port) '
syntax ok
port 22
permitrootlogin no
passwordauthentication no
kbdinteractiveauthentication no

client$ ssh -o PubkeyAuthentication=no ubuntu-admin@10.89.1.2
ubuntu-admin@10.89.1.2: Permission denied (publickey).
client$ ssh ubuntu-admin@10.89.1.2 'echo key ok'
key ok

Always run sshd -t before restarting. A single typo stops sshd from starting:

server$ echo 'PasswordAuthentcation no' | sudo tee -a /etc/ssh/sshd_config.d/10-hardening.conf
server$ sudo sshd -t
/etc/ssh/sshd_config.d/10-hardening.conf: line 4: Bad configuration option: PasswordAuthentcation
/etc/ssh/sshd_config.d/10-hardening.conf: terminating, 1 bad configuration options
server$ sudo systemctl restart ssh
Job for ssh.service failed because the control process exited with error code.

Fix the file and run sshd -t again before you log out. More hardening ideas are in 5 tips to secure SSH on a Linux server.

Step 6: verify and troubleshoot SSH on Ubuntu

When a step to enable SSH on Ubuntu fails, the client message tells you where to look. The server log shows every login:

server$ journalctl -u ssh --no-pager | grep -E 'Accepted|Failed|Connection closed' | tail -3
... sshd[1504]: Connection closed by authenticating user ubuntu-admin 10.89.1.3 port 33186 [preauth]
... sshd[1506]: Accepted publickey for ubuntu-admin from 10.89.1.3 port 33198 ssh2: ED25519 SHA256:34dVTpvCnZJXJLlANtQjo5gD4yymEMpnt8dbW6CIY0Q
Symptom on the clientLikely causeCheck on the server
Connection refusedSSH not installed, or a different portss -tlnp | grep ssh, systemctl status ssh.socket
Connection timed outFirewall drops the port (ufw, cloud security group)sudo ufw status
Permission denied (publickey)Passwords disabled, key not in authorized_keyssudo sshd -T | grep passwordauthentication, permissions of ~/.ssh
Port change has no effectssh.socket still on the old port (24.04)systemctl daemon-reload && systemctl restart ssh.socket

All server options are documented in sshd_config(5), and Ubuntu describes the socket-based setup in its OpenSSH server guide. Once you can enable SSH on Ubuntu reliably, SSH tunnels are the next useful step.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance