Guides

How to enable SSH on Ubuntu

sudo apt install openssh-server, then systemctl enable --now ssh and ufw allow OpenSSH if a firewall is active. Ubuntu Server already has it; Ubuntu Desktop does not.

Published ~5 min read

Short answer: sudo apt install openssh-server installs the SSH service; on Ubuntu 22.10 and newer it starts and enables itself on install, so there is usually nothing left to run. If it is already installed but not answering, the fix is sudo systemctl enable --now ssh and, if a firewall is active, sudo ufw allow OpenSSH.

Ubuntu Server already has it; Ubuntu Desktop does not

Ubuntu Server’s installer offers to install OpenSSH server as a checkbox during setup, and almost everyone ticks it, which is why a fresh Ubuntu Server VPS already accepts SSH connections from the first boot with no extra step. Ubuntu Desktop never installs it, on the (reasonable) assumption that a desktop machine does not need to accept remote logins by default, so turning on SSH there is where this actually comes up.

  1. 1Install openssh-server
    Ubuntu (any version)
    sudo apt update
    sudo apt install -y openssh-server

    This installs the ssh service (its systemd unit name) and the sshd daemon itself. Confirm it is there at all:

    Ubuntu
    dpkg -l | grep openssh-server
  2. 2Check it is running
    Ubuntu
    systemctl status ssh

    active (running) means it already works; nothing further to do. If it says inactive (dead) or could not be found, continue to the next step.

  3. 3Enable and start it
    Ubuntu
    sudo systemctl enable --now ssh

    enable makes it start at every boot from now on; --now also starts it immediately, so you do not have to reboot to find out whether it worked.

  4. 4Open the firewall, if one is active
    Ubuntu with ufw
    sudo ufw status
    sudo ufw allow OpenSSH

    OpenSSH here is an application profile name ufw ships with, not a literal package name to type — it resolves to TCP port 22. If ufw status says inactive, there is nothing to open and this step does nothing, which is fine.

  5. 5Connect
    from another machine
    ssh yourusername@SERVER_IP

    Keep your current session (console, or whatever got you onto the machine in the first place) open until this succeeds, in case step 4 missed a rule somewhere else — a cloud provider’s separate network firewall or security group, which ufw knows nothing about.

ssh, sshd or ssh.socket: Ubuntu version matters

Ubuntu versionService nameNotes
22.04 and earliersshsshd listens on the port itself, starts at boot via the service
22.10 to 23.10ssh.service and ssh.socketTransition period; both exist, service usually still does the listening
24.04 and newerssh.socketSocket-activated: systemd listens on the port and starts sshd on the first connection

On 24.04 and newer, a port change or a configuration reload needs sudo systemctl daemon-reload && sudo systemctl restart ssh.socket rather than restarting ssh itself, or the old setting quietly keeps answering — the exact trap covered in changing the SSH port. For simply enabling the service for the first time, systemctl enable --now ssh still works on every version: systemd resolves the alias to whichever unit actually applies.

Confirming sshd is actually listening

Ubuntu
sudo ss -tlnp | grep ssh
sudo sshd -T | grep -i ^port

ss shows what is really bound to a port right now, regardless of what systemctl status claims; sshd -T dumps the effective configuration sshd would use, which also catches a second, forgotten Port line in a drop-in file under /etc/ssh/sshd_config.d/ that reopens port 22 even after you meant to change it.

Password login is on by default — turn it off once key auth works

A fresh openssh-server install accepts password logins, which is fine for the first connection but not something to leave on for long on a server reachable from the whole internet. Set up a key first:

on your own machine
ssh-keygen -t ed25519 -C "you@example"
ssh-copy-id yourusername@SERVER_IP

Then disable password logins on the server, in /etc/ssh/sshd_config or a drop-in file:

/etc/ssh/sshd_config.d/no-password.conf
PasswordAuthentication no
apply it
sudo sshd -t && sudo systemctl restart ssh    # 24.04: restart ssh.socket instead

Test the key login in a new terminal before closing the one you are already connected through — the same rule as changing the port, for the same reason. The full hardening checklist beyond this one setting, including fail2ban and disabling root login, is in securing SSH access.

Installed and running, but connections still fail

SymptomLikely causeSee
Connection refusedFirewall between you and the server, or sshd bound to the wrong addressSSH: Connection refused
Connection timed outNothing answers at all — a network-level firewall or wrong IPSSH connection timed out
Permission denied (publickey)Key auth is on, but the key is not installed or not offeredPermission denied (publickey)
It works, but on another portSomething changed the port previously; check sshd -TSSH port 22

A note on WSL and containers

Everything above is for a real Ubuntu server or VM. Inside WSL or a Docker container, systemctl may not work at all, since neither necessarily runs systemd as PID 1 — sudo service ssh start is the fallback there, and it is rarely worth running an SSH server inside a container in the first place, since the host already offers a way in.

Frequently asked

How do I enable SSH on Ubuntu?

sudo apt install -y openssh-server installs and, on Ubuntu 22.10 and newer, automatically starts and enables it. If it is already installed but not running, sudo systemctl enable --now ssh starts it and sets it to start at every future boot.

Does Ubuntu Server already have SSH enabled?

Usually yes — the Ubuntu Server installer offers to install OpenSSH server as a checkbox during setup, and most people select it, so a fresh server VPS already accepts SSH connections from the first boot. Ubuntu Desktop never installs it by default.

I enabled SSH but still cannot connect — why?

Check, in order: that the service is actually running (systemctl status ssh), that a local firewall allows it (sudo ufw allow OpenSSH), and that a network-level firewall or security group outside the server itself is not blocking port 22 before it ever reaches ufw.

On Ubuntu 24.04, what is ssh.socket and does it change how I enable SSH?

Since Ubuntu 22.10, and by default on 24.04, systemd itself listens on port 22 through ssh.socket and starts sshd only on the first connection. systemctl enable --now ssh still works to turn it on initially, since systemd resolves the alias; it matters afterward, when a configuration change needs systemctl restart ssh.socket rather than restarting ssh itself.

Related articles

All guides