Log inSign up

Harden a fresh Rocky Linux or AlmaLinux server in the first hour

Log in as root with the password from the Overview tab, run dnf upgrade --refresh, create a user in the wheel group and copy your SSH key to it. Then disable password and root logins in an /etc/ssh/sshd_config.d/ drop-in, trim the firewalld public zone to the services you use, keep SELinux in enforcing mode and enable dnf-automatic for security updates. Those steps take about fifteen minutes and close the most common doors.

Topic: Security · Tested on: Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why start with updates?

Our Rocky Linux 9 and AlmaLinux 9 images are built ahead of time, so newer security fixes may already be published by the time your server boots. Apply them before anything else and reboot if the kernel changed:

dnf upgrade --refresh -y
cat /etc/os-release
needs-restarting -r || reboot

needs-restarting comes from dnf-utils (install it with dnf install dnf-utils if missing) and exits with a non zero code when a reboot is required. Both distributions rebuild the same sources, so every command in this guide works on either. The Rocky Linux 9 and AlmaLinux 9 image pages list what the base install includes.

How do I create an administrator user?

Create a normal user and add it to wheel, the group that sudo trusts by default on the RHEL family:

useradd -m -G wheel deploy
passwd deploy
id deploy

Then copy your public key. From your own computer:

ssh-copy-id deploy@198.51.100.24
ssh deploy@198.51.100.24
sudo whoami

The final command prints root. Keep the root session open in another window until you have tested every later step. The general version of this step is in create a sudo user.

How do I lock SSH down to keys?

Write a drop-in file instead of editing the main config. Version 9 reads /etc/ssh/sshd_config.d/*.conf early, and the first value read wins, so a low number at the front of the name takes priority:

sudo tee /etc/ssh/sshd_config.d/10-hardening.conf > /dev/null <<EOF
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 3
LoginGraceTime 30
AllowUsers deploy
EOF
sudo sshd -t
sudo systemctl reload sshd

sshd -t prints nothing when the syntax is valid. Open a new terminal and log in as deploy before closing the old session. Confirm the effective values with sudo sshd -T | grep -Ei "permitroot|passwordauth". More options are covered in disable root login.

What should firewalld allow?

Only what you serve. firewalld runs by default with the public zone on the network interface. Look at it, then remove services you do not use; many builds include cockpit and dhcpv6-client:

sudo firewall-cmd --state
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --remove-service=cockpit
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services

Keep ssh in the list. If the instance gets its IPv6 address through DHCPv6, removing dhcpv6-client can break it later, so leave that one unless you know your IPv6 setup is static. The firewalld guide covers zones, rich rules and source allowlists.

Should SELinux stay enabled?

Yes. SELinux enforcing mode confines services even when one of them is compromised, and it is on by default:

getenforce
sestatus

Both should report Enforcing. Most problems blamed on SELinux are a missing label or port type, which you fix instead of disabling the whole system. If you move SSH to port 2222, for example, tell SELinux and the firewall:

sudo dnf install policycoreutils-python-utils
sudo semanage port -a -t ssh_port_t -p tcp 2222
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload

Read denials with sudo ausearch -m avc -ts recent. The SELinux basics guide explains booleans and file contexts.

How do I turn on automatic security updates?

Install dnf-automatic, tell it to apply security updates only, and start its timer:

sudo dnf install dnf-automatic
sudo sed -i "s/^upgrade_type.*/upgrade_type = security/; s/^apply_updates.*/apply_updates = yes/" /etc/dnf/automatic.conf
grep -E "^(upgrade_type|apply_updates)" /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer

Kernel updates still need a reboot to take effect. Plan a monthly reboot window, or check needs-restarting -r each week. Details and the Ubuntu equivalent are in automatic security updates.

How do I add fail2ban from EPEL?

EPEL is the community repository of extra packages for the RHEL family, and it carries fail2ban:

sudo dnf install epel-release
sudo dnf install fail2ban fail2ban-firewalld
sudo tee /etc/fail2ban/jail.local > /dev/null <<EOF
[sshd]
enabled = true
maxretry = 5
bantime = 1h
EOF
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

The fail2ban-firewalld package makes bans go through firewalld instead of raw iptables. With key only SSH, fail2ban mainly cuts log noise and slows scanners. See the fail2ban guide for more jails.

What else belongs in the first hour?

A few small checks round things off. Confirm time sync, because logs and TLS depend on it: chronyc tracking. Review the system crypto policy with update-crypto-policies --show; DEFAULT is a sound choice and FUTURE drops older algorithms at the cost of compatibility. List listening services with sudo ss -tulpn and disable anything you do not need with sudo systemctl disable --now name. Finally, decide on backups: the optional daily backup add-on costs 20% of the monthly plan price, or follow the daily backups tutorial to run your own with restic.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1a hardened base install fits in 1 GB1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2room for an app on top1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months

Questions about First security steps on Rocky and Alma

Do these steps differ between Rocky Linux and AlmaLinux?

Not in practice. Both rebuild the same sources, use dnf, firewalld and SELinux, and accept the same commands shown here.

Where do I find the first root password?

On the Overview tab of the instance page in the console. After you switch to key only SSH as the deploy user, that password no longer opens SSH.

Can I add my SSH key when I deploy instead?

Yes. Keys saved on the SSH keys page can be selected on the Deploy page and are applied at deploy time, which lets you skip password login entirely.

Is it fine to put SELinux in permissive mode for a while?

Only for short debugging. Permissive mode logs denials without blocking, so switch back with sudo setenforce 1 as soon as you have the label or boolean fix.

I locked myself out over SSH. What now?

There is no browser console, so the way back is a reinstall from the Settings tab, which erases the disk. Always test a second login before closing your working session.

Does CentOS Stream 9 work with this guide?

Mostly yes, since it shares the same tooling. Package versions move faster on Stream, so check updates more often.