SSH hardening means reducing the ways attackers can abuse remote access: key-based logins, no direct root login, limited users and close monitoring. This checklist from Mintopsblog.com lists the settings that matter most for a Linux server and how to apply them safely.
Key Takeaways
- Switch to SSH keys and turn off password logins once keys work.
- Disable direct root login and allow only the accounts that need access.
- Test every sshd_config change from a second session before closing the first.
- Add rate limiting, firewall rules and log monitoring for failed logins.
Why SSH Needs Attention
Any server with port 22 open to the internet receives automated login attempts within minutes of going online. Most of them guess common usernames and weak passwords. OpenSSH is secure software, but its defaults on many distributions still allow password logins, and old keys or forgotten accounts tend to pile up over time.
NIST’s report NISTIR 7966 focuses on the management of SSH keys in organizations and is a good read once you run more than a handful of servers.
The Checklist
All settings below go in /etc/ssh/sshd_config or a file in /etc/ssh/sshd_config.d/. The sshd_config manual documents each keyword.
| Setting | Recommended value | Why |
|---|---|---|
PasswordAuthentication | no | Stops password guessing entirely once keys are in place |
KbdInteractiveAuthentication | no (unless you use MFA through PAM) | Closes another password prompt path |
PermitRootLogin | no (or prohibit-password) | Forces admins to log in as themselves and use sudo |
PubkeyAuthentication | yes | Enables key-based logins |
AllowUsers / AllowGroups | Only named admins or an ssh group | Blocks every other account, including forgotten ones |
MaxAuthTries | 3 to 4 | Ends sessions that try many keys or passwords |
X11Forwarding | no | Removes a feature servers rarely need |
LoginGraceTime | 30s to 60s | Drops idle unauthenticated connections sooner |
Set Up Keys Before Disabling Passwords
- On your workstation, create a key with
ssh-keygen -t ed25519and protect it with a passphrase. - Copy the public key to the server with
ssh-copy-id user@server, or append it to~/.ssh/authorized_keys. - Check permissions:
~/.sshat 700 andauthorized_keysat 600, owned by the user. The Linux permissions guide explains why sshd is strict here. - Open a new terminal and confirm you can log in with the key.
- Only then set
PasswordAuthentication no.
Apply Changes Without Locking Yourself Out
Run sudo sshd -t to check the configuration for syntax errors, then reload the service with sudo systemctl reload ssh (the unit is named sshd on RHEL-based systems). Keep your current session open and test from a second one. The systemd guide covers reload versus restart in more detail. If you work on a cloud server, make sure you have console access through your provider in case something goes wrong.
Network-Level Protection
- Allow port 22 only from the addresses that need it, using the host firewall (ufw, firewalld or nftables) or a cloud security group.
- Use a VPN or a bastion host for administration instead of exposing SSH on every server.
- Add rate limiting with a tool such as fail2ban, which bans addresses after repeated failures.
- Changing the port reduces log noise but is not a security control on its own; treat it as optional.
Keys, Accounts and Monitoring
Hardening is not a one-time task. Review authorized_keys files when people change roles or leave, and remove keys nobody can identify. Prefer one key per person and device so a lost laptop means removing one key, not rotating everything. For larger fleets, SSH certificates signed by an internal CA give keys an expiry date and make revocation easier.
Watch authentication logs for repeated failures and for successful logins at unusual times. Sending those events to a central system, as shown in the centralized logging guide, lets you alert on them across all servers. Practice these changes first on a test machine; our home lab guide explains how to build one.
More security topics are in the Security and SSL category, and command-line basics are in the Linux section. For the complete Mintopsblog.com reading list, start from the Mintopsblog.com main page.
Quick Audit Commands
Before and after hardening, a few commands show how a server is really configured. Run sudo sshd -T to print the effective settings after all include files are applied, which is more reliable than reading sshd_config by eye. Use ss -tlnp | grep ssh to confirm the listening address and port, and sudo journalctl -u ssh --since today (or -u sshd) to review recent logins and failures. On each user account, list ~/.ssh/authorized_keys and confirm every key has a known owner and a comment that identifies the device.
Repeat this audit on a schedule, for example every quarter, and after anyone with admin access leaves. Small, regular reviews catch forgotten keys and accounts long before an attacker does.
Optional Extra Layers
- Multi-factor authentication: add a time-based one-time code through PAM, or use hardware security keys with the FIDO2 key types (ed25519-sk) supported by recent OpenSSH.
- Session recording or auditing: for shared admin accounts on sensitive systems, record who ran which commands.
- Idle timeouts:
ClientAliveIntervalwithClientAliveCountMaxcloses sessions left open on unattended laptops. - Restricted accounts: users who only transfer files can be limited with
ForceCommand internal-sftpand aChrootDirectory.
Add these after the core checklist is stable, one at a time, and test each change from a second session.
Frequently Asked Questions
Should I Disable Password Login for SSH?
Yes, for most servers. Key-based authentication is far harder to brute-force than passwords. Set up and test your keys first, confirm you can log in from a new session, and only then set PasswordAuthentication to no. If you need multi-factor authentication, configure it through PAM alongside keys.
Is Changing the SSH Port Worth It?
Moving SSH from port 22 cuts the number of automated attempts in your logs, but a port scan finds the new port quickly. It is a convenience, not real protection. Firewall rules, key-only logins, disabled root login and rate limiting provide the actual security.
Which SSH Key Type Should I Use?
Ed25519 keys are the common recommendation for new keys: they are short, fast and well supported by current OpenSSH versions. RSA keys of 3072 bits or more remain acceptable where older systems require them. Always protect private keys with a passphrase and store them only on trusted devices.
How Do I Avoid Locking Myself Out?
Test the configuration with sshd -t, reload rather than restart, and keep your existing session open while you log in from a second terminal. Confirm you have out-of-band access, such as a cloud provider console. Only close the first session after the new settings work.





