Browse guides

Secure SSH access

Require key authentication and block direct root login without locking yourself out.

Intermediate10 min readUpdated
On this page

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.backup

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 30

SSH key required

Only disable passwords after validating key access in a second session.

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 ssh

Test without closing your session

Open a new terminal and reconnect with your sudo user before leaving the current session.