A fresh server is a target within minutes of going online: automated bots scan the whole IPv4 space and try default passwords on open SSH ports around the clock. A proper Rocky Linux 9 server setup closes those easy doors before you install any application. This guide walks through the essential first steps: system updates, a sudo user, SSH key authentication, sshd hardening, firewalld, Fail2Ban, time synchronisation and a few safe sysctl tweaks. This article covers Initial Server Setup and Hardening on in detail.
Step 1: Update the System in Your Rocky Linux 9 Server Setup – Initial Server Setup and Hardening on
Log in as root for the last time and bring everything up to date:
dnf upgrade -y
dnf install -y vim curl wget tar bash-completion policycoreutils-python-utils
reboot
Rebooting now ensures you run the newest kernel before you invest time in configuration. While you are here, set the hostname and timezone:
hostnamectl set-hostname server1.example.com
timedatectl set-timezone Asia/Ho_Chi_Minh
Our separate guide explains how to change the hostname in Linux on other distributions too.
Step 2: Create a Sudo User and Disable Direct Root Work – Initial Server Setup and Hardening on
useradd -m -s /bin/bash admin
passwd admin
usermod -aG wheel admin
From now on, log in as admin and use sudo for administrative tasks. This gives you an audit trail of privileged commands in /var/log/secure.

Step 3: Set Up SSH Key Authentication
Passwords can be guessed; keys effectively cannot. On your workstation, generate a key pair and copy the public key to the server (see our full tutorial on how to generate SSH keys on Linux):
ssh-keygen -t ed25519
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
Log in once with the key to verify it works. Keep this session open until the next step is tested — that way a configuration mistake cannot lock you out.
Step 4: Harden the SSH Daemon
Edit /etc/ssh/sshd_config (or better, create /etc/ssh/sshd_config.d/99-hardening.conf) and set:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
X11Forwarding no
Optionally change the SSH port to a high number such as 2222 to cut down log noise. On Rocky Linux, SELinux guards the port, so register the new port first, then allow it in the firewall:
semanage port -a -t ssh_port_t -p tcp 2222
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --permanent --remove-service=ssh
firewall-cmd --reload
systemctl restart sshd
Open a new terminal and confirm you can still log in before closing the old session. Our guide on how to change the SSH port on Linux explains the firewall side in more depth.
Step 5: Configure the Firewall (firewalld)
firewalld is the default firewall on Rocky Linux 9. Enable it and allow only the services you actually run:
systemctl enable --now firewalld
firewall-cmd --list-all
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
We cover zones, rich rules and permanent configuration in the dedicated guide on how to configure firewalld on Rocky Linux 9.
Step 6: Install Fail2Ban
Fail2Ban watches your logs and bans IPs that repeatedly fail to log in. It ships in the EPEL repository:
dnf install -y epel-release
dnf install -y fail2ban
systemctl enable --now fail2ban
Create /etc/fail2ban/jail.local with an [sshd] jail, a ban time of one hour and backend = systemd. For background on the tool, read our article about how to install Fail2Ban to protect SSH.
Step 7: Time Sync, Automatic Updates and SELinux
Accurate time is essential for logs, TLS and cron jobs. Rocky uses chrony:
systemctl enable --now chronyd
chronyc tracking
Next, turn on automatic security updates so critical patches land without manual work. Finally, confirm SELinux is enforcing — never “fix” a problem by disabling it:
getenforce
# should print: Enforcing
If it prints Permissive or Disabled, set SELINUX=enforcing in /etc/selinux/config and reboot. Our SELinux on Rocky Linux 9 guide explains how to work with it instead of against it.
Troubleshooting
- Locked out after changing the SSH port: SELinux is the usual culprit — the
semanage portstep was skipped. Use the provider’s web console to add the port label. - sudo says the user is not in the wheel group: check with
groups adminand re-add withusermod -aG wheel admin. - Fail2Ban bans nothing: verify the jail is active with
fail2ban-client status sshdand that the log path matches your SSH configuration.
Step 8: Swap, Listening Ports and a Final Review
Small VPS instances benefit from swap space as a safety net. If your server has none, add a swap file by following our guide on how to create swap in Linux, then finish the setup with an audit of what is actually reachable from the network:
sudo ss -tlnp
Every line in that output should be a service you deliberately enabled. If you find something unexpected, stop it with systemctl disable --now <service> and re-check your firewall rules before moving on.
Conclusion
With updates applied, a sudo user in place, key-only SSH, firewalld and Fail2Ban active, and SELinux enforcing, your Rocky Linux 9 server setup has a solid security baseline. The official hardening advice in the Rocky Linux documentation goes further with auditing and compliance profiles when you need them. From here you can safely install your stack — for example a LEMP stack on Rocky Linux 9.