Browse guides
Secure SSH access
Require key authentication and block direct root login without locking yourself out.
Intermediate10 min readUpdated July 18, 2026
1
Back up the configuration
Before changing SSH, make a copy of its configuration so that you can restore it if needed.
Terminal
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup2
Apply safe rules
Create a dedicated override file at /etc/ssh/sshd_config.d/99-hardening.conf.
99-hardening.conf
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30SSH key required
Only disable passwords after validating key access in a second session.
3
Validate and reload SSH
Test the syntax before reloading the service. No output means that the configuration is valid.
Terminal
sudo sshd -t
sudo systemctl reload sshTest without closing your session
Open a new terminal and reconnect with your sudo user before leaving the current session.