How to Harden SSH and Security on AlmaLinux/Rocky Linux

This guide adapts the core SSH and security hardening practices to AlmaLinux/Rocky Linux's specific tooling — firewalld, SELinux, and dnf — rather than duplicating the general concepts covered for Ubuntu/Debian.

Prerequisites

  • AlmaLinux 9 or Rocky Linux 9 VPS
  • A sudo-enabled user already created
  • An active SSH session (keep it open throughout)

Step 1 — Set Up SSH Key Authentication

ssh-copy-id deploy@YOUR_SERVER_IP

Step 2 — Edit sshd_config

sudo nano /etc/ssh/sshd_config
Port 2222
PermitRootLogin no
PasswordAuthentication no

Step 3 — Update firewalld for the New Port

sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload

Step 4 — Update SELinux to Allow SSH on the Custom Port

Unlike Ubuntu/Debian, SELinux on AlmaLinux/Rocky Linux will block SSH from binding to a non-standard port unless explicitly told the port is valid for SSH:

sudo dnf install policycoreutils-python-utils -y
sudo semanage port -a -t ssh_port_t -p tcp 2222

This step is easy to miss and is a very common cause of SSH failing to start after a port change on RHEL-family systems.

Step 5 — Validate and Restart SSH

sudo sshd -t
sudo systemctl restart sshd

Note the service name is sshd, not ssh.

Step 6 — Test in a New Session Before Closing the Current One

ssh -p 2222 deploy@YOUR_SERVER_IP

Step 7 — Install and Configure Fail2Ban

sudo dnf install epel-release -y
sudo dnf install fail2ban -y
sudo systemctl enable --now fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
sudo systemctl restart fail2ban

Step 8 — Verify SELinux Status

getenforce

Keep it Enforcing — see Understanding SELinux Basics on AlmaLinux/Rocky Linux for working with it rather than disabling it.

Step 9 — Enable Automatic Security Updates

sudo dnf install dnf-automatic -y
sudo nano /etc/dnf/automatic.conf
upgrade_type = security
apply_updates = yes
sudo systemctl enable --now dnf-automatic.timer

Step 10 — Review firewalld Rules

sudo firewall-cmd --list-all

Confirm only genuinely necessary services/ports are allowed.

Quick Reference: Key RHEL-Family Differences from Generic Hardening Guides

TaskRHEL-Family Specific Step
Change SSH portMust also run semanage port -a -t ssh_port_t
Restart SSHService name is sshd, not ssh
Firewallfirewalld with --permanent + --reload, not UFW
Fail2Ban installRequires EPEL repository first

Common Errors

SSH fails to start after changing the port — almost always the missing semanage port step; SELinux blocks sshd from binding to a port not registered for the ssh_port_t type.

sudo ausearch -m avc -ts recent | grep sshd

Continue Reading

Browse more articles in AlmaLinux & Rocky Linux.

  • almalinux ssh hardening, rocky linux security, selinux ssh port, rhel hardening
  • 0 أعضاء وجدوا هذه المقالة مفيدة
هل كانت المقالة مفيدة ؟

مقالات مشابهة

AlmaLinux vs Rocky Linux vs CentOS: What Happened to CentOS?

If you're choosing a RHEL-compatible Linux distribution for your VPS, understanding the CentOS...

How to Get Started with AlmaLinux/Rocky Linux on a VPS

This guide covers the essential first steps after deploying a new AlmaLinux or Rocky Linux VPS...

How to Use dnf: The Package Manager for AlmaLinux & Rocky Linux

dnf (Dandified YUM) is the package manager for AlmaLinux, Rocky Linux, and other RHEL-family...

How to Configure firewalld on AlmaLinux/Rocky Linux

firewalld is the default firewall management tool on AlmaLinux and Rocky Linux, using a...

How to Install Nginx on AlmaLinux/Rocky Linux

This guide covers installing and configuring Nginx on AlmaLinux or Rocky Linux, including the...