Home / Guides

Server guide

SSH hardening on Ubuntu and AlmaLinux

SSH is the front door of a Linux server, and the internet knocks on it all day. Locking it down takes ten minutes. Doing it carelessly can lock you out, so these steps keep a second way in open until you have tested.

2 min readUpdated November 2026 by the RootManage teamWorks on Ubuntu, Debian, AlmaLinux and Rocky unless noted

The short answer

Add your public key, test that you can log in with it from a second window, then set password login off, root login off or key-only, a low number of tries, and check the result with `sshd -T`. Keep the first session open until the new one works.

Put your key on the server first

terminal
# on your own computer
ssh-keygen -t ed25519 -C "you@example.com"
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

Use a normal user with sudo, not root. Open a new terminal and log in with the key to prove it works. Do not continue until it does.

Use a drop-in file, and watch the order

terminal
sudo mkdir -p /etc/ssh/sshd_config.d
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf

Current Ubuntu and AlmaLinux releases read every *.conf file in /etc/ssh/sshd_config.d/ before the rest of the main file. For most settings the first value OpenSSH finds wins, so a file named 00-... beats a later one. Cloud images often ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes; yours must come before it, or you must change it.

The settings

terminal
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin prohibit-password
MaxAuthTries 3
LoginGraceTime 30
PermitEmptyPasswords no
X11Forwarding no
# optional: only these users may log in
# AllowUsers alice bob

PermitRootLogin no is the strictest choice. prohibit-password lets root in only with a key, which is fine if you need it for tools. AllowUsers is a strong extra step, as long as you list yourself.

Check the file, then reload

terminal
sudo sshd -t
# Ubuntu / Debian
sudo systemctl reload ssh
# AlmaLinux / Rocky / RHEL
sudo systemctl reload sshd

sshd -t prints nothing when the syntax is fine. Reloading keeps your current session alive. Now open a new window and log in with the key. Only then close the old session.

Confirm what is really in use

terminal
sudo sshd -T | grep -Ei 'passwordauthentication|permitrootlogin|maxauthtries|pubkeyauthentication'

This prints the final values after every file is read. If it still says passwordauthentication yes, another file is winning: search with grep -ri passwordauthentication /etc/ssh/.

Slow down the guessing

  • Install fail2ban (on AlmaLinux from the EPEL repository) and enable the sshd jail. It blocks addresses that fail too often.
  • Allow SSH only from known addresses in the firewall if your own address is fixed.
  • Changing the port from 22 hides you from lazy scans but adds little real protection. If you do it, allow the new port in the firewall first, tell SELinux on AlmaLinux with semanage port, and on newer Ubuntu releases check whether SSH starts through a systemd socket. Test before you close the old port.

If you are locked out

Use the console of your hosting provider (VNC or serial console in the control panel) to log in and fix the file. This is why you keep a second session open and why you test before you close anything.

Check it every week

A setting can drift after a package update or a new cloud-init file. The RootManage security audit checks these SSH settings every week and tells you in your dashboard when one changes. You can also run it once yourself for free with the free Linux security audit.

Questions people ask

Is it safe to turn off password login?

Yes, once key login works and you have tested it from a second window. Keep a copy of your private key safe.

Should I disable root login completely?

It is the safest choice if you can use a normal user with sudo. If a tool needs root over SSH, use key-only root login (`prohibit-password`).

Why does my change not work?

Another file is probably setting the same option first. Check `sudo sshd -T` and look in `/etc/ssh/sshd_config.d/`.

Do I need two-factor for SSH?

It adds protection if keys could be stolen, but it is more to maintain. Keys with a passphrase plus fail2ban are a solid base for most servers.

Want a person to look at it?

Tell us what you see. We reply with what we think is wrong and what it would take to fix it.