Before you change anything
- Keep an existing root or sudo session open until a second login with your key works.
- Confirm you have an ed25519 or RSA key loaded:
ssh-add -l. - On a cloud VM, confirm console access exists if SSH fails.
Minimal safe sshd settings
Edit /etc/ssh/sshd_config (or a file under /etc/ssh/sshd_config.d/) and set:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
KbdInteractiveAuthentication no
X11Forwarding no
AllowTcpForwarding no # set yes only if you need tunnels
ClientAliveInterval 300
ClientAliveCountMax 2
Validate, then reload:
sudo sshd -t && sudo systemctl reload ssh
Optional: allow only your user
AllowUsers yourname
Prefer this over inventing a new SSH port. Port changes mainly stop casual scanners; keys and no-root matter more.
Optional: fail2ban for sshd
sudo apt update
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
Default jails usually watch sshd. Confirm with sudo fail2ban-client status sshd.
Prove it
From another machine: ssh -o PreferredAuthentications=publickey yourname@server. Then try password auth and confirm it is refused.
Inaabi Tune’s settings step sets SSH to keys only with no root login, then restarts the server and runs your test; if the test fails, the change is put back.
Related guides: unattended-upgrades setup · firewall with ufw · hardening Ubuntu.
FAQ
Will this lock me out of SSH?
It can, if you disable password login before your key works, or if you change the port and forget the new one. Test a second session before you close the first.
Does Tune do this for me?
Tune applies SSH hardening as part of its settings pass on a staging copy, and re-tests after each change. This page is the DIY checklist.
Which Ubuntu does this target?
Commands below are for Ubuntu 22.04 LTS (x86_64). Inaabi Tune is tested on Ubuntu 22.04 and 24.04.
Next
Run the free Inaabi Scan, then Subscribe to Inaabi Tune at $20 per server per month. Browse all Inaabi guides.
MinakiLabs