sshd_config settings to change on Ubuntu 24.04: disabling root login, restricting users and the question of changing the port

  1. Goal
  2. Preparation
  3. Steps
  4. Step 1: Use your own file in sshd_config.d
  5. Step 2: Check the syntax before applying
  6. Step 3: Reload the service and test with a new session
  7. Changing the port: is it worth it?
  8. Checking
  9. Common errors
  10. Summary
  11. References

Goal

Once you can log in with a key (the first article of this planet), the next step is to tighten the SSH server configuration.

This article uses OpenSSH 9.6 on Ubuntu 24.04 LTS and changes the most useful settings, explaining why each one matters.

Preparation

Steps

Steps

Step 1: Use your own file in sshd_config.d

The main file /etc/ssh/sshd_config on Ubuntu 24.04 has the line Include /etc/ssh/sshd_config.d/*.conf near the top.

So you do not need to edit the original file: create your own, which is easy to track and is not overwritten when packages are upgraded.

One thing to remember: for most settings sshd uses the first value it reads, and .conf files are read in name order.

Some cloud server images ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. So give your file a lower number, for example 10-hardening.conf, so that your value wins.

sudo nano /etc/ssh/sshd_config.d/10-hardening.conf

Example contents:

# Do not let root log in directly
PermitRootLogin no

# Keys only, passwords off
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

# Only these users may log in (replace with your real name)
AllowUsers deploy

# Limit attempts and waiting time
MaxAuthTries 3
LoginGraceTime 30

What each setting means:

Step 2: Check the syntax before applying

sudo sshd -t

No output means the syntax is valid. To see the values that are actually in effect (after merging all files):

sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|allowusers|maxauthtries'

Step 3: Reload the service and test with a new session

On Ubuntu the service is called ssh:

sudo systemctl reload ssh

Then open a new terminal window and log in. Only when the new session works should you close the old one. Sessions that are already open are not cut when the configuration is reloaded.

Changing the port: is it worth it?

Many people move port 22 to another port (for example 2222). In practice, network scanners scan the whole range of ports, so changing the port does not stop anyone determined to attack.

The benefit is real but small: your logs get less noisy because automated probes only try port 22. A port change does not replace SSH keys, turning passwords off, or a firewall.

If you still want to change it, do it in the right order so you do not lock yourself out:

  1. Open the new port on the firewall first (and keep port 22 until you have finished testing).

  1. Add Port 2222 to the configuration file.

  1. On Ubuntu 24.04, sshd is started through ssh.socket (activated on connection), so after changing the port run sudo systemctl daemon-reload and then sudo systemctl restart ssh.socket.

  1. Check which port is listening with sudo ss -tlnp | grep -E 'sshd|ssh'.

  2. Test with ssh -o Port=2222 -l deploy example.com from a new session, and only then close port 22.

Checking

Common errors

Summary

Put your configuration in its own file in sshd_config.d, disable root and password login, limit users with AllowUsers, check with sshd -t and always test with a new session.

Changing the port is only a minor detail. The last step is to block those who keep guessing, see Blocking SSH password guessing with fail2ban and reading login logs on Ubuntu 24.04.

References

sshd_config settings to change on Ubuntu 24.04: disabling root login, restricting users and the question of changing the port