Short answer: Keep a second SSH session open, install an ed25519 key (RSA on a FIPS-mode server) and log in with it, then add a drop-in at /etc/ssh/sshd_config.d/10-hardening.conf with PermitRootLogin prohibit-password and PasswordAuthentication no. Optionally move to a high port (label it for SELinux with semanage port), rate-limit with CSF, cPHulk or fail2ban, and restrict logins with AllowUsers. Test every change with sshd -t before reloading. (Reviewed November 2026.)
Before you touch anything
Open a second SSH session and leave it connected. Every change below is applied with sshd -t (config test) before a reload, and a reload never drops existing sessions, so if you lock yourself out you still have the first window to undo it.
Table of Contents
Check that the server is actually using sshd_config and not a drop-in you did not know about:
sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|^port|pubkeyauthentication'
ls /etc/ssh/sshd_config.d/
On AlmaLinux 9 the file /etc/ssh/sshd_config.d/50-redhat.conf exists and is read before the main file. Anything you set in the main file that conflicts with it loses, because sshd keeps the first value it sees. Put your hardening in its own drop-in with a lower number so it wins.
The same rule cuts the other way. If ls shows a file that sorts before 10-hardening.conf, its values beat yours. A common example is 01-permitrootlogin.conf containing PermitRootLogin yes, which some kickstart installs write to allow root password logins. Remove it or fix it, then check with grep -ri permitrootlogin /etc/ssh/sshd_config /etc/ssh/sshd_config.d/. sshd -T always shows the value that actually won.
1. Install your key, then disable passwords
From your workstation:
ssh-keygen -t ed25519 -C "you@workstation"
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@server
Before you run those, check whether the server is in FIPS mode, which is common on servers built to a compliance baseline. In FIPS mode the system crypto policy does not allow Ed25519, so the key copies across but every login with it fails. Use an RSA key of 3072 bits or more there instead:
ssh root@server 'fips-mode-setup --check' # "FIPS mode is enabled" means no ed25519
ssh-keygen -t rsa -b 4096 -C "you@workstation"
ssh-copy-id -i ~/.ssh/id_rsa.pub root@server
Log in once with the key to prove it works. Then create /etc/ssh/sshd_config.d/10-hardening.conf:
PermitRootLogin prohibit-password
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
prohibit-password lets root in with a key only, which is what most cPanel and DirectAdmin servers need. If you have a sudo user and never need root over SSH, use PermitRootLogin no instead. Test and reload:
sshd -t && systemctl reload sshd
Try a password login from another terminal; it should be refused immediately.
2. Change the port without fighting SELinux
Moving off port 22 does not make the server secure, but it removes 95% of the automated noise from your logs and from your brute-force blocker. Pick a port above 1024 that nothing else uses, for example 2222 is too common; 48222 is fine.
SELinux only allows sshd to bind to ports labelled ssh_port_t. Add the label first or sshd will fail to start:
dnf -y install policycoreutils-python-utils
semanage port -a -t ssh_port_t -p tcp 48222
firewall-cmd --permanent --add-port=48222/tcp && firewall-cmd --reload
echo "Port 48222" >> /etc/ssh/sshd_config.d/10-hardening.conf
sshd -t && systemctl restart sshd
Connect on the new port in a new terminal before you close the old one. Only then close port 22, from the session on the new port. firewalld keeps a runtime configuration (what is active now) and a permanent one (what loads after a reload or reboot), so remove the service from both in the zone your interface uses, then check both:
firewall-cmd --get-active-zones # usually public
firewall-cmd --zone=public --remove-service=ssh # runtime: port 22 closes now
firewall-cmd --zone=public --permanent --remove-service=ssh # permanent: stays closed after reboot
firewall-cmd --zone=public --list-all # ssh gone, 48222/tcp still listed
firewall-cmd --zone=public --permanent --list-all # same result in the permanent config
Removing it with --permanent alone leaves port 22 open until the next reload or reboot. If you use CSF instead of firewalld, add the port to TCP_IN in /etc/csf/csf.conf and run csf -r.
3. Rate-limit and ban
With passwords off, brute force cannot succeed, but it still costs CPU and fills /var/log/secure. Use whichever blocker the server already has: CSF+LFD (LF_SSHD = "5" in csf.conf), cPHulk on cPanel, or fail2ban on a plain box:
dnf -y install epel-release
dnf -y install fail2ban
cat > /etc/fail2ban/jail.d/sshd.local <<'EOF'
[sshd]
enabled = true
port = 48222
maxretry = 4
findtime = 10m
bantime = 24h
EOF
systemctl enable --now fail2ban
fail2ban is not in the AlmaLinux 9 base repositories; it comes from EPEL, which is why the block installs epel-release first. Many EPEL packages also need the CRB repository (dnf config-manager --set-enabled crb). Confirm the jail is live and see who it has banned:
fail2ban-client status sshd
Do not run two blockers at once; they fight over iptables chains.
4. Restrict who can log in
If only two people should ever SSH in, say so:
AllowUsers root deploy
Add it to the same drop-in. Anyone else gets “Permission denied” before authentication even starts. For root, also trim /root/.ssh/authorized_keys — old keys from previous admins and providers’ automation are the most common leftover.
5. Limit root to your management network
AllowUsers also accepts USER@HOST patterns, and the host part can be a CIDR range. That lets you keep key-only root access but only from your office or VPN range, while the sudo user can still connect from anywhere:
AllowUsers root@198.51.100.0/24 deploy
Change the existing AllowUsers line in 10-hardening.conf rather than adding a second one, then run sshd -t && systemctl reload sshd. Root logins from any other address are refused before authentication. sshd processes DenyUsers before AllowUsers, so a deny entry always wins.
Do this only if your management address is fixed. If your office IP changes, root is locked out over SSH; log in as the sudo user and fix the range. Test a root login from the allowed range in a new terminal before you close your working session.
6. Leave ciphers to the crypto policy
Many older hardening guides tell you to paste Ciphers, MACs and KexAlgorithms lines into sshd_config. On AlmaLinux 9 that is usually unnecessary: OpenSSH follows the system-wide cryptographic policy, which 50-redhat.conf pulls in, and Red Hat describes the DEFAULT policy as secure for current threat models. Check what you have:
update-crypto-policies --show
sshd -T | grep -Ei '^(ciphers|macs|kexalgorithms)'
If you need stricter settings, change the policy for the whole system with update-crypto-policies --set FUTURE and reboot, as Red Hat recommends after a policy change. FUTURE can break old clients, backup agents and monitoring tools that only speak older algorithms, so test them first. If you really must override algorithms for sshd alone, Red Hat’s documented way is a drop-in whose number is below 50 so it precedes 50-redhat.conf; hand-written algorithm lists then stop following future policy updates.
Verify
sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|^port|maxauthtries|allowusers'
ss -ltnp | grep sshd
grep -c 'Failed password' /var/log/secure
The last number should stop growing within an hour. The server-security-audit script checks all of the above and flags anything that drifts later.
Prove passwords are refused from the client
sshd -T shows what the server intends. Confirm what a client actually gets by forcing a password attempt from your workstation:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password -p 48222 deploy@server.example.com
ssh -v -p 48222 deploy@server.example.com 2>&1 | grep -i "can continue"
The first command should fail at once with Permission denied (publickey) and never show a password prompt. In the verbose output of the second, the server should offer only publickey. If password or keyboard-interactive still appears, an earlier drop-in or a Match block is overriding your settings.
Troubleshooting lockouts and start failures
Most problems after hardening come from one of six causes. Work from the session you kept open, and read journalctl -u sshd -n 50 first.
| Symptom | Likely cause | Fix |
|---|---|---|
| sshd fails to start after the port change; the journal shows a bind error with “Permission denied” | The port has no ssh_port_t SELinux label | Run semanage port -l | grep ssh, add the label, restart. ausearch -m AVC -ts recent shows the denial. |
semanage port -a refuses the port | The port already has a label for another service | Pick a different port, or relabel it with semanage port -m -t ssh_port_t -p tcp 48222 if nothing else uses it |
| New port times out, sshd is listening | Firewall not opened, or the provider’s firewall blocks it | Check firewall-cmd --list-ports (or TCP_IN in CSF) and any cloud security group |
| A setting in your drop-in seems ignored | An earlier file or the main file set it first | ls /etc/ssh/sshd_config.d/, then sshd -T to see the winning value |
| Key is offered but refused | Wrong permissions or SELinux context on ~/.ssh | chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys, restorecon -Rv ~/.ssh |
| Your own IP suddenly cannot connect | fail2ban, LFD or cPHulk banned it | fail2ban-client set sshd unbanip 203.0.113.7, or the matching CSF or cPHulk tool |
Roll back safely
Undo changes in the reverse order you made them, from the session that is still connected:
# 1. Re-open port 22 first if you removed it
firewall-cmd --permanent --add-service=ssh && firewall-cmd --reload
# 2. Remove (or edit) the drop-in, then reload
rm /etc/ssh/sshd_config.d/10-hardening.conf
sshd -t && systemctl reload sshd
# 3. Only after sshd no longer uses the custom port
semanage port -d -t ssh_port_t -p tcp 48222
Re-opening port 22 before removing the Port line means there is never a moment when sshd listens on a closed port. If you moved fail2ban to the custom port, set port in sshd.local back and restart fail2ban. Removing the whole drop-in is the fastest way back to the vendor defaults; you can then re-apply the settings one at a time. If every SSH session is gone, use the provider’s out-of-band console (KVM, VNC, IPMI or the hypervisor console). PasswordAuthentication no only affects sshd, so a local console login with the root or sudo password still works. Keep that password in your password manager even after you switch SSH to keys.
Harden SSH AlmaLinux 9 at a glance

Official documentation: AlmaLinux wiki, Linux man pages, firewall-cmd manual, Red Hat: securing networks (SSH).
Related guides: KernelCare on cPanel and DirectAdmin servers: setup, verification and rollback · Replacing cxs: malware scanning with LMD (maldet), ClamAV and ImunifyAV on hosting servers · Incident response after a cPanel root-escalation CVE: rotating keys, hunting .sorry, auditing sessions · Change the SSH port safely.
Frequently asked questions
Should I edit /etc/ssh/sshd_config or use a drop-in on AlmaLinux 9?
Use a drop-in. The main file includes /etc/ssh/sshd_config.d/*.conf at the top and sshd keeps the first value it reads, so a file such as 10-hardening.conf wins over both 50-redhat.conf and anything further down sshd_config. It also keeps all your hardening in one file that is easy to review or remove.
Is PermitRootLogin prohibit-password already the default on AlmaLinux 9?
It is the upstream OpenSSH default, but a drop-in can override it, for example one written by a kickstart install. Check the effective value with sshd -T | grep -i permitrootlogin rather than trusting the default, and set it explicitly in your own drop-in.
Do I need to reboot after changing the SSH port?
No. Label the port for SELinux, open it in the firewall, then run sshd -t and restart sshd. Existing sessions stay connected during the restart, which is why you keep one open until a login on the new port works.
