Sub topic 10
Harden the SSH Daemon Safely
Assume you already created a non-root user with sudo, installed an SSH public key for that user, and tested key-based login. Keep access to a local console or cloud recovery console while changing SSH. The console is your fallback if the SSH configuration prevents remote login.
The SSH daemon, usually called sshd, accepts incoming SSH connections. Its configuration controls which accounts and authentication methods it accepts. Hardening means reducing unnecessary ways to log in without disabling the method you need.
Disable passwords and direct root login
Create a drop-in configuration file. A drop-in is a small file loaded with the main SSH configuration. It is easier to review and preserve than editing a large vendor-managed file.
# /etc/ssh/sshd_config.d/10-hardening.conf
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yesThese settings have different purposes:
PasswordAuthentication norejects password-based SSH authentication.PermitRootLogin norejects all direct SSH logins asroot, including key-based logins.PubkeyAuthentication yeskeeps public-key authentication enabled.
You can still become root after logging in as the administrative user by running sudo. This creates an audit trail and avoids exposing the root account to remote authentication.
Some systems also allow keyboard-interactive authentication through PAM. Do not disable KbdInteractiveAuthentication automatically if your organization uses it for multifactor authentication. If it only provides another password prompt, disable it after confirming your authentication design.
Validate before reloading
A syntax error can prevent the daemon from reloading. Test the configuration first, then reload the service.
sudo sshd -t
sudo systemctl reload ssh # Debian/Ubuntu
# sudo systemctl reload sshd # RHEL family
# Display the effective settings
sudo sshd -T | grep -E 'passwordauthentication|permitrootlogin|pubkeyauthentication'sshd -t checks syntax and key files. sshd -T expands the configuration and prints the values the daemon will actually use. This matters because distribution defaults, included files, and configuration order can affect the result.
Keep your current SSH session open. From a second terminal, test a new connection:
ssh [email protected]Only close the original session after the second login succeeds. Test that a password login is rejected and that the administrative user can still use sudo.
Exercise
Why should you test a second SSH session before closing the first one?
Show answer
The first session remains a recovery path if the new configuration has a syntax error, rejects the key, or prevents the account from logging in.