SSH is the front door of almost every Linux server, and most servers still run it with the defaults their image shipped with. The defaults are reasonable, but they are written for compatibility, not for a production server that only a few people should ever reach.

This is the baseline we start from. Every line has a reason, and every line can be checked.

Before You Change Anything

Keep one SSH session open while you work, and test from a second one. If a change locks you out, the open session is how you undo it.

Make sure key login works first. On your own machine, create a key if you do not have one:

ssh-keygen -t ed25519 -a 100
ssh-copy-id user@server

Then log in with the key and confirm it is used: ssh -v user@server prints Authenticated ... using "publickey".

The Configuration

Modern Debian, Ubuntu and RHEL-family systems read drop-in files from /etc/ssh/sshd_config.d/. Use one instead of editing the main file, so package updates do not conflict with your changes.

One detail matters here: for most options, sshd uses the first value it reads, and drop-in files are read in name order. Some cloud images ship a file such as 50-cloud-init.conf that turns password login back on. Give your file a lower number so it is read first:

sudo nano /etc/ssh/sshd_config.d/10-hardening.conf
# Keys only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no
AuthenticationMethods publickey

# No direct root login; use a named account and sudo
PermitRootLogin no

# Only members of this group may log in
AllowGroups ssh-users

# Fewer guesses, shorter windows
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2

# Turn off what you do not use
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no

# Log the key fingerprint used for each login
LogLevel VERBOSE

What each group does:

  • Keys only. Password guessing stops being possible at all, instead of merely unlikely. KbdInteractiveAuthentication closes the second password path that PAM can provide.
  • No root login. Every administrative action starts from a named account, so logs show who did what.
  • AllowGroups. New accounts cannot log in over SSH until you add them on purpose. Create the group and add yourself before reloading: sudo groupadd ssh-users && sudo usermod -aG ssh-users "$USER".
  • Limits and timeouts. Fewer attempts per connection and idle sessions that close on their own.
  • Forwarding off. Agent and TCP forwarding are useful, and also a common path for moving from one compromised machine to the next. Enable them per user with a Match block if someone needs them.
  • LogLevel VERBOSE. Records which key was used, which makes access reviews and incident work much easier.

Apply It Safely

Check the syntax first. sshd -t prints nothing when the configuration is valid:

sudo sshd -t

Then reload. The service is called ssh on Debian and Ubuntu and sshd on RHEL-family systems:

sudo systemctl reload ssh

Now open a new terminal and log in again. Only close the old session once that works.

Prove It Is in Effect

A configuration file shows what you wrote. sshd -T shows what the server will actually use after reading every file, which is what matters:

sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|maxauthtries|allowgroups|x11forwarding)'
maxauthtries 3
permitrootlogin no
passwordauthentication no
kbdinteractiveauthentication no
x11forwarding no
allowgroups ssh-users

If a value is not what you set, another file was read before yours. grep -r PasswordAuthentication /etc/ssh/ finds it.

From another machine, confirm that the server refuses passwords:

ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password user@server
# Permission denied (publickey).

Beyond sshd_config

The configuration limits what an attacker can try. Two more steps limit who can try at all:

  • Restrict the network. If only your office or a VPN needs SSH, allow port 22 from those addresses in the firewall and drop everything else. A port nobody can reach cannot be attacked.
  • Keep OpenSSH updated. Hardening does not protect against a vulnerability in sshd itself. Apply security updates on a schedule.

Changing the port number is not on this list. It reduces log noise, but it does not stop anyone who is looking for SSH specifically.