Skip to main content

Security and hardening · Intermediate · 12 min

How to Harden SSH on Ubuntu 26.04: Keys Only, No Root, Custom Limits

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · OpenSSH

You will switch an Ubuntu 26.04 server's SSH daemon to key-only logins, turn off root login, limit who can connect and how many guesses they get, and test the change without locking yourself out.

On this page

Any server with a public address gets SSH login attempts within minutes of going online. Most are bots trying common passwords against root. Three settings stop nearly all of it: log in with keys only, never as root, and allow only the accounts you name.

In this tutorial you will write a hardening file for the OpenSSH server, check it for mistakes before it goes live, apply it, and prove from a second terminal that the old ways in are closed and the new way still works. The example account is called deploy. Use your own user name wherever you see it.

Read the warning in the next section before you change anything.

Step 1 · Protect yourself from a lockout

A wrong SSH setting can lock you out of your own server. These three habits make that recoverable:

  1. Keep your current SSH session open until the very end. An open session survives a daemon reload, so you can still fix a mistake.
  2. Open a second terminal and log in with your key before you change anything. If that fails, stop and fix it first, because turning off passwords then would leave you with no way in. The steps are in /tutorials/log-in-to-a-server-with-ssh-keys.
  3. Set up a recovery path before you need it. Your provider's web console gives you a login prompt that does not use SSH, but it asks for your account's password, and a key-only account does not have one. Set one now, with a long random password stored in your password manager:
Bash
sudo passwd deploy

The console login is used only in an emergency, and the server stays closed to password logins over the network. If your provider has a rescue mode that mounts the server's disk, that is a second way in without a password.

Check it worked

In the second terminal, log in with your key. Replace the user and address with yours:

Bash
ssh deploy@203.0.113.10

You get a shell without being asked for a password (or only for the passphrase of your key). Keep this session open too. If it asks for the account password instead, your key is not installed correctly, so do not continue.

Step 2 · See what the server allows today

Print the settings the daemon actually uses. The -T option makes sshd print its effective configuration after reading every file:

Bash
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|maxauthtries|logingracetime|x11forwarding|allowusers) '

On a stock Ubuntu 26.04 install the output is permitrootlogin prohibit-password, passwordauthentication yes, maxauthtries 6, logingracetime 120 and x11forwarding yes. Some providers change these with their own file, which is why you check instead of assuming. Which files are read?

Bash
grep -n '^Include' /etc/ssh/sshd_config
ls -l /etc/ssh/sshd_config.d/

The first command prints the Include line, and the second lists the folder, which may be empty or hold a provider's file. The Include line makes sshd_config read every *.conf file in /etc/ssh/sshd_config.d/, in alphabetical order. OpenSSH uses the first value it finds for each setting and ignores later ones. Cloud images often ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. A file named 00-hardening.conf is read before it, so it wins. Name your file with a low number for that reason. If you added the same setting to the main sshd_config below the include line, the include would still win, so keep your changes in one drop-in file.

Step 3 · Write the hardening file

Create /etc/ssh/sshd_config.d/00-hardening.conf. Replace deploy in AllowUsers with your own login name, or you will lock that account out too.

Bash
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf > /dev/null <<'CONF'
# Log in with keys only.
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey

# Nobody logs in as root; use a normal user and sudo.
PermitRootLogin no

# Only these accounts may connect.
AllowUsers deploy

# Limit guessing and idle connections.
MaxAuthTries 3
LoginGraceTime 30
MaxSessions 4
ClientAliveInterval 300
ClientAliveCountMax 2

# Features a server rarely needs.
X11Forwarding no
CONF

What each setting does:

  • PasswordAuthentication no and KbdInteractiveAuthentication no close both password prompts. AuthenticationMethods publickey states that a key is the only method the server accepts, so every other method is refused even if another setting would allow it, and a key alone is enough.
  • PermitRootLogin no refuses the root account over SSH even with a valid key. An attacker then has to guess a user name as well as break in.
  • AllowUsers deploy is an allow-list. Any other account, including system accounts, is refused. Separate several names with spaces.
  • MaxAuthTries 3 closes the connection after three failed attempts, and LoginGraceTime 30 gives an unauthenticated connection 30 seconds instead of two minutes. MaxSessions 4 limits how many sessions share one connection.
  • ClientAliveInterval 300 with ClientAliveCountMax 2 makes the server probe the client every five minutes, and drops the connection after two probes go unanswered, about ten minutes. It clears connections whose client has vanished, such as a laptop that lost its network. It does not close a session that is merely idle, because an idle client still answers the probe.
  • X11Forwarding no turns off graphical forwarding that most servers never use.

Step 4 · Test the file before applying it

sshd -t reads the configuration and checks it for errors without starting anything. Run it before every reload:

Bash
sudo sshd -t

No output means the file is valid. An error names the file and line, for example /etc/ssh/sshd_config.d/00-hardening.conf line 6: Bad configuration option. Fix it and run the test again. Never reload with an untested file.

Check what the daemon will use:

Bash
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|authenticationmethods|maxauthtries|logingracetime|x11forwarding|allowusers) '

The output shows permitrootlogin no, passwordauthentication no, kbdinteractiveauthentication no, authenticationmethods publickey, maxauthtries 3, logingracetime 30, x11forwarding no and allowusers deploy. If any line still shows the old value, an earlier file is overriding yours. Look in /etc/ssh/sshd_config.d/ for a file with a lower number.

Step 5 · Apply the change and test from a second terminal

Reload the daemon. A reload reads the new file without dropping the sessions that are already open:

Bash
sudo systemctl reload ssh

On Ubuntu the service is called ssh, not sshd.

Do not close your first session yet. In the second terminal, check that your key still works:

Bash
ssh deploy@203.0.113.10

You get a shell as before. Now check that a password login is refused. This forces the client to offer a password instead of a key:

Bash
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@203.0.113.10

The reply is deploy@203.0.113.10: Permission denied (publickey). The server no longer offers a password prompt at all. Last, check that root is refused:

Bash
ssh root@203.0.113.10

It also ends in Permission denied (publickey)., even if the root account has a key. Once all three behave as described, you can close the first session.

Troubleshooting

  • Permission denied (publickey) for your own account: the key is wrong, the user is not in AllowUsers, or ~/.ssh/authorized_keys has the wrong permissions. Use your first, still-open session to fix it. The .ssh folder must be mode 700 and the file mode 600.
  • You closed every session and cannot log in: use your provider's web console and log in with the account password you set earlier, or boot the provider's rescue mode. Then delete /etc/ssh/sshd_config.d/00-hardening.conf, and run sudo systemctl reload ssh.
  • sshd -T still shows passwordauthentication yes: another file in /etc/ssh/sshd_config.d/ is read first. Rename your file to a lower number, or edit the file that sets it.
  • To see why a login failed, run sudo journalctl -u ssh -n 50, or add -vvv to the ssh command on your computer.

Step 6 · Find out what the daemon is doing

Confirm that SSH is still listening, and see who is connected:

Bash
sudo ss -ltn | grep ':22 '
who

A line shows the daemon listening on port 22, and who lists the accounts that are logged in, with the address each came from. On a server with a firewall, SSH must be allowed there too, which /tutorials/set-up-ufw-firewall-ubuntu-26-04 covers.

Changing the port from 22 to another number reduces log noise from bots but it is not security. Keys-only login is what actually protects you, so this tutorial leaves the port alone.

Next steps

  • Close every port you do not use: /tutorials/set-up-ufw-firewall-ubuntu-26-04.
  • Ban addresses that keep guessing: /tutorials/block-brute-force-logins-with-fail2ban.
  • Install security fixes without logging in: /tutorials/automatic-security-updates-ubuntu.
  • The reference for every option is man sshd_config.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.