Operating System CentOS

Set Up a Basic Firewall on AlmaLinux 9 / Rocky Linux 9: firewalld and iptables

CentOS 6 and its iptables service are gone. Set up the same default-deny firewall on AlmaLinux 9 or Rocky Linux 9 with firewalld or nftables.

This guide sets up a basic host firewall that blocks everything except SSH, web traffic and ping, which is what the classic CentOS 6 iptables recipe did. CentOS 6 reached end of life in November 2020 and should not be on the internet any more, so the steps here target AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9. On these systems the default firewall is firewalld, which programs nftables in the kernel. You will build the rule set with firewalld, and the later sections show what each old iptables command became and what to do if you still have iptables rules to carry over.

What changed since CentOS 6

  • CentOS 6: the iptables service loaded rules from /etc/sysconfig/iptables, and you saved them with service iptables save.
  • RHEL 9 family: firewalld is installed and enabled by default and uses nftables as its backend. The iptables command still exists, but it is supplied by the iptables-nft package, which converts each rule to nftables. Red Hat has marked that package as deprecated and recommends firewalld or native nftables for new setups.

Use one firewall manager per server. Do not add rules with iptables or nft while firewalld is running, because firewalld knows nothing about them and they are not saved.

Requirements

  • AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 and a user with sudo rights.
  • A list of the services the server offers. This example is a web server: SSH (TCP 22), HTTP (TCP 80), HTTPS (TCP 443) and ping.
  • Access to your provider's web console as a safety net.

You do not need to open ports for outgoing DNS or NTP. The firewall is stateful, so replies to requests your server sends are accepted automatically. Open UDP 53 or UDP 123 only if this machine is itself a DNS or NTP server.

Step 1: Check that firewalld is running

sudo firewall-cmd --state

If the command is missing or reports not running, install and start the service:

sudo dnf install -y firewalld
sudo systemctl enable --now firewalld

Look at the current configuration:

sudo firewall-cmd --get-default-zone
sudo firewall-cmd --list-all

A fresh install uses the public zone, which rejects incoming connections except for the services listed in the output, normally including ssh. That already matches the old "default policy DROP" approach: anything you do not add stays closed.

Step 2: Open the web ports

firewalld keeps two configurations: the runtime one that is active now, and the permanent one that is loaded at boot and on reload. Add the rules to the runtime configuration first:

sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https

A service is a named port definition. For a port without a service name, give the number and protocol instead:

sudo firewall-cmd --add-port=8080/tcp

Ping needs no rule. firewalld accepts ICMP echo requests unless you add an ICMP block.

Step 3: Test, then save

Test the site from another machine and open a second SSH session to confirm you can still log in. Runtime changes are the built-in lockout protection: if something is wrong, sudo firewall-cmd --reload or a reboot from your provider's web console returns the firewall to the last saved state.

When everything works, copy the runtime configuration to the permanent one:

sudo firewall-cmd --runtime-to-permanent

This replaces service iptables save. The alternative is to add --permanent to each command and then run sudo firewall-cmd --reload; permanent rules are not active until that reload.

Step 4 (optional): Limit SSH to your own address

If you always connect from a fixed address, allow SSH only from there. Add the rich rule first, then remove the general SSH service, and keep your current session open while you test with a new one:

sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.10" service name="ssh" accept'
sudo firewall-cmd --remove-service=ssh

Use a range such as 203.0.113.0/24 for a whole network. Save with sudo firewall-cmd --runtime-to-permanent once a new login succeeds. Skip this step if your address changes.

Verify

sudo firewall-cmd --list-all

The output shows the zone, its interfaces and the allowed services, ports and rich rules. To see the nftables rules firewalld generated from them:

sudo nft list ruleset

iptables commands and their firewalld equivalents

CentOS 6 (iptables)RHEL 9 family (firewalld)
iptables -L -nfirewall-cmd --list-all
iptables -A INPUT -p tcp --dport 80 -j ACCEPTfirewall-cmd --add-service=http
iptables -A INPUT -p tcp --dport 8080 -j ACCEPTfirewall-cmd --add-port=8080/tcp
iptables -A INPUT -s 203.0.113.10 -p tcp --dport 22 -j ACCEPTthe rich rule in step 4
iptables -A INPUT -i lo -j ACCEPT and the ESTABLISHED,RELATED rulebuilt in, nothing to add
iptables -P INPUT DROPthe default behaviour of the public zone
service iptables savefirewall-cmd --runtime-to-permanent
iptables -F (emergency)firewall-cmd --reload to drop unsaved changes

If you still have iptables rules

You can convert existing rules to nftables syntax instead of rewriting them by hand. The iptables-translate tool, part of the iptables packages, prints the nftables equivalent of a single rule:

iptables-translate -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT

It answers with an nft add rule ip filter INPUT ... line. To convert a whole saved rule file, such as a copy of /etc/sysconfig/iptables from the old server:

iptables-restore-translate -f iptables.rules > ruleset.nft

Review the result before loading it; translated rule sets usually contain more than a small server needs.

Alternative: a native nftables rule set

If you prefer one readable rules file to firewalld's zones, you can run the nftables service instead. Run only one of the two services. Stop firewalld first, from the web console or a session you are prepared to lose:

sudo systemctl disable --now firewalld

Create /etc/nftables/basic.nft with the same policy as above:

flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        iif "lo" accept
        ct state established,related accept
        ct state invalid drop
        meta l4proto { icmp, ipv6-icmp } accept
        tcp dport { 22, 80, 443 } accept
    }
    chain forward {
        type filter hook forward priority 0; policy drop;
    }
    chain output {
        type filter hook output priority 0; policy accept;
    }
}

Check the syntax without applying it, then load it:

sudo nft -c -f /etc/nftables/basic.nft
sudo nft -f /etc/nftables/basic.nft

On the RHEL 9 family the nftables service loads /etc/sysconfig/nftables.conf at boot. Add an include line for your file there and enable the service:

echo 'include "/etc/nftables/basic.nft"' | sudo tee -a /etc/sysconfig/nftables.conf
sudo systemctl enable --now nftables

The inet table covers IPv4 and IPv6 together, so there is no separate ip6tables configuration to maintain.

If you lock yourself out

  • Unsaved firewalld change: reboot the server from your hosting control panel. The runtime rules are discarded and the saved configuration loads.
  • Saved firewalld change: log in through your provider's web console and run sudo firewall-cmd --permanent --add-service=ssh followed by sudo firewall-cmd --reload.
  • nftables: from the web console, run sudo nft flush ruleset to remove all rules, fix the file and load it again.

Frequently asked questions

Can I still use the iptables command on AlmaLinux 9 or Rocky Linux 9?

The command is available through the iptables-nft package and the rules end up in nftables, but the package is deprecated in RHEL 9 and the rules are not saved by firewalld. For a new server, learn firewalld or nftables rather than building on the compatibility layer.

Do I need separate rules for IPv6?

Not with firewalld services and ports, which apply to IPv4 and IPv6 alike, and not with an nftables inet table. Only address-specific rules, such as the rich rule in step 4, need an IPv6 counterpart with family="ipv6".

What about Ubuntu and Debian?

They also use nftables in the kernel. Ubuntu ships ufw as its simple front end and Debian leaves the choice to you; firewalld and plain nftables are available on both. The firewalld commands in this guide work the same way there once the package is installed.

Should I also block "bad" TCP flag combinations as old guides did?

There is little need. Connection tracking already treats packets that do not belong to a valid connection as invalid, and a default-deny inbound policy drops what you have not allowed.

Share story:

Continue reading other guides on How VPS.