Home CentOS/RHELSSH Hardening on RHEL 9: Secure the sshd Daemon Step by Step

SSH Hardening on RHEL 9: Secure the sshd Daemon Step by Step

by Knowledge Base Linux
SSH hardening illustration with a key and padlock for RHEL 9

SSH is the front door of every Linux server, and on the public internet that door is rattled thousands of times a day by bots trying password lists. Sensible SSH hardening turns a RHEL 9 server from a soft target into a boring one: key-only authentication, no direct root login, a non-standard port registered with SELinux, and the system-wide crypto policies that Red Hat maintains for you. This walkthrough applies SSH hardening on RHEL 9 step by step, in an order that keeps you from locking yourself out.

If you are completely new to remote access, first read our beginner guide on how to SSH into a Linux server, then come back here to secure it.

Prerequisites – SSH Hardening on RHEL 9

  • A RHEL 9, CentOS Stream 9, AlmaLinux 9 or Rocky Linux 9 server
  • A user account with sudo privileges, plus console or provider VNC access as an emergency fallback
  • Your SSH public key ready to install (see how to generate SSH keys on Linux if you do not have one yet)

Step 1: Set Up Key Authentication First – SSH Hardening on RHEL 9

Never disable password login before your keys work. Copy your public key to the server and test it in a second terminal while your current session stays open:

ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
ssh -i ~/.ssh/id_ed25519 [email protected]

On the server, verify the permissions are strict — sshd ignores keys it considers exposed:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Step 2: Apply the Core SSH Hardening Settings

On RHEL 9, do not edit /etc/ssh/sshd_config directly. Drop your settings into /etc/ssh/sshd_config.d/01-hardening.conf so they survive package updates:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
AllowUsers admin deploy

Optionally restrict access to a group instead with AllowGroups sshusers. Validate the configuration before reloading — a syntax error here is the classic self-lockout:

sshd -t
systemctl reload sshd

Reload (not restart) applies the changes to new connections while keeping your current session alive.

Digital key unlocking a server door as part of SSH hardening

Step 3: Change the SSH Port — the RHEL Way

A custom port does not stop a determined attacker, but it removes your server from the noise of internet-wide scans. Add the port to your drop-in file:

Port 2222

On Debian-style systems that is nearly enough. On RHEL 9, two more steps are mandatory. First, register the port with SELinux, or sshd will be refused permission to bind:

semanage port -a -t ssh_port_t -p tcp 2222

Second, allow the new port through firewalld, then reload sshd:

firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload
systemctl restart sshd
ss -tlnp | grep 2222

Test a fresh login on the new port before removing port 22 from the firewall. The general background on port changes is covered in our tutorial on changing the SSH port in Linux.

Step 4: Use System-Wide Crypto Policies

RHEL 9 centralizes TLS and SSH cryptography in system-wide crypto policies, so you should not hand-tune cipher lists in sshd_config — they will be ignored or will fight the policy. Inspect and strengthen the policy instead:

update-crypto-policies --show
update-crypto-policies --set FUTURE
systemctl restart sshd

The FUTURE policy disables weak algorithms and requires longer keys, at the cost of compatibility with very old clients. For most fleets, the default DEFAULT policy is a sane baseline; move to FUTURE on internet-facing machines after testing your admin workstations can still connect.

Step 5: Add Brute-Force Protection and Logging

With key-only login, brute force is mostly noise — but silence the noise anyway. Fail2Ban works on RHEL 9 from the EPEL repository:

dnf install -y epel-release fail2ban
systemctl enable --now fail2ban
fail2ban-client status sshd

Also make a habit of reviewing authentication logs after any incident:

journalctl -u sshd --since today
grep "Failed" /var/log/secure | tail

Step 6: Set Idle Timeouts and Session Limits

Finish with two small settings that close off forgotten sessions — a surprisingly common way into servers. Disconnect idle shells after ten minutes and cap concurrent unauthenticated connection attempts:

ClientAliveInterval 300
ClientAliveCountMax 2
MaxStartups 10:30:60

Reload sshd once more after adding them. Combined with key-only authentication, these limits mean an unattended laptop or a flooded login queue can no longer become your next security incident.

Troubleshooting

“Permission denied (publickey)”: wrong key file, permissions too open on .ssh, or AllowUsers missing your account. Run the client with ssh -v to see which key was offered.

Locked out after changing the port: you skipped the SELinux port registration or the firewall rule. Use the console, add the missing piece, and always test the new port in a parallel session before closing the old one.

Legacy client can no longer connect after setting FUTURE policy: the client only offers deprecated algorithms. Either upgrade the client, or scope an exception with a custom crypto policy module instead of weakening the whole server.

Conclusion

SSH hardening on RHEL 9 is a sequence, not a single tweak: keys first, then a drop-in config that disables root and password logins, a port change done the SELinux-aware way, and crypto left to the system policy so it stays current. Follow that order and your servers will quietly shrug off the internet’s background noise. The OpenSSH manual documents every sshd option when you need to go further.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.