To enable SSH on Ubuntu, run sudo apt update and sudo apt install openssh-server, then sudo systemctl enable --now ssh. On Ubuntu 24.04 and later, check ssh.socket instead of ssh.service, allow SSH through UFW with sudo ufw allow OpenSSH, then connect with ssh username@ip-address and switch to key-based login.
Key takeaways
- Install openssh-server, then enable the correct systemd unit: ssh.socket on Ubuntu 24.04 and later, ssh.service on 22.04.
- Open port 22 through UFW before you test a remote connection, or the firewall will block you.
- Switch to SSH keys, disable password login, and disable root login once keys work.
SSH lets you manage an Ubuntu machine from anywhere, but Ubuntu does not turn it on for you. A few commands install the OpenSSH server, start it at boot, and open the firewall — then a few hardening steps keep that open door from becoming a risk.
What is SSH on Ubuntu?
SSH, or Secure Shell, is an encrypted protocol for logging in to a remote computer and running commands on it. On Ubuntu, the OpenSSH server (sshd) listens for those connections, while the OpenSSH client (ssh) makes them.
Ubuntu ships the client on most installs, but not the server. That distinction matters: running ssh -V and seeing a version number only proves the client is installed, not that anyone can connect to your machine. You need the openssh-server package for inbound connections.
SSH also carries file transfers. The same server handles SFTP and SCP, so once it works, you can copy files as well as run commands. Ubuntu's official OpenSSH documentation describes OpenSSH as a replacement for insecure tools such as telnet and rcp, which send passwords in cleartext.
How do I enable SSH on Ubuntu?
Install the server, enable it at boot, open the firewall, and test a login from another machine. On most systems the whole process takes under five minutes.
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
sudo ufw allow OpenSSH
ssh username@ip-address
That short sequence is the whole job for a standard install. The rest of this guide explains what each step does, how the commands differ between Ubuntu releases, and how to secure the server once it is running.
How do I install the OpenSSH server?
Run sudo apt update to refresh the package list, then sudo apt install openssh-server. You need sudo access; without it, ask an administrator to install the package or grant you access.
First, check whether it is already installed. The command dpkg-query -W -f='${Status} ${Package}\n' openssh-server prints install ok installed when the package is present. If the output says the package is not found, install it.
Ubuntu Server cloud images usually include OpenSSH already, and the installer can add it during setup. Ubuntu Desktop usually does not, which is why most desktop users hit this step. Installing the package normally starts the server for you, but you should still enable it explicitly so it survives a reboot.
If you only need to connect out from this machine, install openssh-client instead. The client cannot accept inbound logins, so do not confuse the two packages.
How do I start SSH and make it start at boot?
Use sudo systemctl enable --now ssh on Ubuntu 22.04, and check ssh.socket on Ubuntu 24.04 and later. The --now flag starts the unit immediately and enables it for future boots in one command.
The difference comes from socket activation, introduced in Ubuntu 22.10. On Ubuntu 24.04 and 26.04, the default listener is ssh.socket: it listens on port 22 and starts ssh.service only when a connection arrives. On Ubuntu 22.04, the persistent ssh.service runs directly.
| Ubuntu release | Default listener | Enable command | Check command |
|---|---|---|---|
| 22.04 LTS | ssh.service | sudo systemctl enable --now ssh | systemctl status ssh |
| 24.04 LTS | ssh.socket | sudo systemctl enable --now ssh.socket | systemctl status ssh.socket |
| 26.04 LTS | ssh.socket | sudo systemctl enable --now ssh.socket | systemctl status ssh.socket |
Verify the result with systemctl is-active ssh.socket and systemctl is-enabled ssh.socket on 24.04 and later. Both should report active and enabled. On 22.04, use sudo systemctl status ssh and look for active (running).
Confirm the port is listening with ss -tlnp | grep ':22'. If nothing listens on port 22, the service or socket is not running, or a custom port is configured. Do not enable both the socket and a persistent service as competing listeners; manage the setup your system already uses.
How do I allow SSH through UFW?
Run sudo ufw allow OpenSSH if UFW is active. The OpenSSH application profile maps to port 22, so this one command opens the right port.
UFW is often installed but inactive on Ubuntu Desktop, and commonly enabled on servers. Check first with sudo ufw status. If it says inactive, there is nothing to open yet — but add the rule before you ever enable UFW with a default-deny policy, or you will lock yourself out.
If you changed the SSH port, allow that number instead, for example sudo ufw allow 2222/tcp. After adding a rule, run sudo ufw status verbose to confirm it is listed. If your machine sits behind a cloud firewall or a home router, you may also need to open port 22 there; UFW only controls the Ubuntu host itself.
How do I connect to Ubuntu over SSH?
From another computer, run ssh username@ip-address, replacing username with your Ubuntu username and ip-address with the server's address. On first connect, SSH shows the host key fingerprint and asks you to confirm it.
Find the server's address with ip a on the Ubuntu machine, or check your router. If you are on the same local network, use the private address. From the internet, use the public address — and only after hardening the server, as described below.
On first connection, compare the fingerprint with the one on the server if you can. You can view it with ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub. After you accept it, SSH remembers the host key and warns you if it ever changes, which can signal a different machine or an attack.
DigitalOcean's SSH connection guide covers the same client steps for Ubuntu 22.04 and 24.04. To connect on a custom port, add -p and the port number: ssh -p 2222 username@ip-address. To copy a file, use scp file.txt username@ip-address:/home/username/, or use SFTP for interactive transfers.
How do I set up SSH key login on Ubuntu?
Generate a key on your own computer with ssh-keygen -t ed25519, then copy the public key to the server with ssh-copy-id username@ip-address. Ubuntu's documentation recommends ed25519 for its shorter keys and lower computational cost.
Accept the default key location, and set a passphrase for the private key. The private key stays on your computer; only the public key goes to the server, where ssh-copy-id appends it to ~/.ssh/authorized_keys.
Test key login in a new terminal before you change anything else. Run ssh username@ip-address again — it should ask for your key passphrase, not your account password. If it still asks for a password, check permissions: the .ssh directory and authorized_keys must not be writable by others. Fix them with chmod go-w ~/.ssh/authorized_keys on the server.
Keep a backup of your private key somewhere safe, and never share it. If you lose it and password login is disabled, you will need console access to the machine to get back in. Good key habits are part of a wider baseline, as our small business cybersecurity checklist explains.
How do I secure SSH on Ubuntu?
Once key login works, disable password login and root login, test the configuration, and reload the server. These three changes block the most common SSH attacks: password guessing against the root account.
On Ubuntu 24.04 and later, put custom settings in a drop-in file under /etc/ssh/sshd_config.d/, for example /etc/ssh/sshd_config.d/99-hardening.conf. Ubuntu reads those files first, so they override the main /etc/ssh/sshd_config file and survive package updates.
PasswordAuthentication no
PermitRootLogin no
MaxAuthTries 3
LoginGraceTime 30
Before reloading, test the syntax with sudo sshd -t. A typo here can lock you out, so never skip that check. Then apply the change: on 24.04 and later, restart the socket with sudo systemctl restart ssh.socket; on 22.04, use sudo systemctl reload ssh.
Other useful hardening steps: change the default port only as an extra layer, never as your only defense; use fail2ban or a similar tool to slow brute-force attempts; keep the server updated with sudo apt update && sudo apt upgrade; and consider two-factor authentication with a hardware key where Ubuntu supports it.
Why is SSH not working on Ubuntu?
Work through the chain in order: package, service, firewall, network, then authentication. Most failures sit in the first three links.
- Connection refused: the server is not installed or not running. Check
dpkg-query -W openssh-serverandsystemctl status ssh.socketorsystemctl status ssh. - Connection timed out: a firewall is blocking port 22, or the address is wrong. Check
sudo ufw statusand confirm the IP address on the server. - Permission denied (publickey): the key is not on the server, or permissions are wrong. Re-run
ssh-copy-idand check~/.ssh/authorized_keyspermissions. - Permission denied (password): the password is wrong, or password login is disabled. Use a key, or check the server configuration.
- Host key verification failed: the server key changed. Confirm the server was rebuilt or reinstalled before you accept the new key.
- Config changes had no effect: on 24.04, you may have edited the main file while a drop-in overrides it, or restarted the wrong unit. Use
sudo sshd -tand restartssh.socket.
Live logs help when the cause is unclear: sudo journalctl -fu ssh.service shows connection attempts and errors as they happen. Run it on the server while you try to connect from the client. If logs suggest an intrusion rather than a config error, follow your incident response plan instead of just restarting the service.
How do I disable SSH on Ubuntu?
Run sudo systemctl disable --now ssh.service ssh.socket to stop SSH and prevent it starting at boot. This covers both units on current releases, so a reboot will not bring the listener back.
If you only want to stop it until the next reboot, use sudo systemctl stop ssh.service ssh.socket without disable. To remove the server entirely, run sudo apt remove openssh-server, which leaves your configuration files in place unless you purge them.
The bottom line
Enabling SSH on Ubuntu is three commands — install, enable, allow through the firewall — plus a test login. The release matters: Ubuntu 24.04 and later listen through ssh.socket, while 22.04 runs ssh.service directly.
Do not stop at a working password login. Set up an ed25519 key, confirm it works, then disable password and root login and test the configuration with sudo sshd -t before every reload. You can check the address you will connect to with our free what-is-my-ip tool.
Frequently asked questions
How do I enable SSH on Ubuntu?
Install the server with sudo apt install openssh-server, then run sudo systemctl enable --now ssh. On Ubuntu 24.04 and later, confirm ssh.socket is active and enabled. Then allow SSH through the firewall and test a connection from another machine.
Is SSH enabled by default on Ubuntu?
No. Ubuntu Desktop does not enable SSH by default, and you must install openssh-server yourself. Ubuntu Server images often include it, and the installer can add it during setup.
What port does SSH use on Ubuntu?
SSH uses TCP port 22 by default. You can change it in the sshd configuration, but changing the port alone is not a security control, so use keys and strong settings as well.
Why is my SSH connection refused on Ubuntu?
The usual causes are openssh-server not installed, the ssh service or socket not running, UFW blocking port 22, or connecting to the wrong IP address. Check systemctl status ssh and sudo ufw status, then test locally.
How do I enable SSH on Ubuntu 24.04 specifically?
Ubuntu 24.04 uses socket activation, so the listener is ssh.socket. Install openssh-server, run sudo systemctl enable --now ssh.socket, and check it with systemctl is-active ssh.socket and systemctl is-enabled ssh.socket.
How do I disable SSH on Ubuntu?
Run sudo systemctl disable --now ssh.service ssh.socket to stop SSH and prevent it starting at boot. This covers both the service and the socket on current releases.
Should I use a password or an SSH key?
Use an SSH key. Generate an ed25519 key with ssh-keygen -t ed25519, copy it with ssh-copy-id, test key login, then set PasswordAuthentication no in the server configuration.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against Ubuntu's official OpenSSH server documentation and DigitalOcean's SSH guide for Ubuntu 22.04 and 24.04.
How we write and fact-check →