How to Install iptables on Rocky, AlmaLinux & RHEL 9/10 (and CentOS 8/7)

Replace firewalld with the classic iptables service on Rocky Linux, AlmaLinux and RHEL 9/10, and CentOS 8/7, without locking yourself out of SSH. Also: why iptables is now nftables underneath, and when to use nft directly instead.

Share
Installing the iptables service instead of firewalld on Rocky Linux and RHEL

Rocky Linux, AlmaLinux and RHEL ship with firewalld. You can still replace it with the classic iptables service, the way CentOS 6 worked: rules in /etc/sysconfig/iptables, loaded at boot. The short version for Rocky, Alma and RHEL 9 (CentOS 8 and 7 are the same with yum):

sudo systemctl disable --now firewalld
sudo systemctl mask firewalld
sudo dnf install -y iptables-services
sudo systemctl enable --now iptables

Before you run that on a remote server, read the SSH section below. The default ruleset allows port 22 and blocks everything else, so if SSH runs on another port, you'll lock yourself out.

Since RHEL 9, iptables is deprecated, and on RHEL 9 and 10 it's really nftables underneath. It still works, but for a new server I'd look at the nftables option at the end first.

What "iptables" means on RHEL 9 and 10

On RHEL 8 and later, the iptables command comes from the iptables-nft package. It accepts the old syntax but writes the rules into the kernel's nftables framework:

$ iptables -V
iptables v1.8.10 (nf_tables)

Load a few rules with iptables and nft list ruleset shows them as nftables rules, with a note on top:

# Warning: table ip filter is managed by iptables-nft, do not touch!

Red Hat deprecated the iptables-nft and ipset packages in RHEL 9 and recommends the nft tool for new setups. In RHEL 10, iptables-nft-services still exists but is on the deprecated packages list. So it works today, it gets no new features, and it will likely be gone in the next major release.

There's a naming detail too. On Rocky 9, dnf install iptables-services actually installs a package called iptables-nft-services, as Rocky forum users found. Same files, same service name. Don't let that confuse you.

Step 1: disable firewalld

Two firewalls managing the same tables will fight. Stop firewalld, keep it from starting at boot, and mask it so nothing pulls it back in:

sudo systemctl disable --now firewalld
sudo systemctl mask firewalld

On CentOS 7 the commands are the same.

Step 2: install the iptables service

Rocky, AlmaLinux, RHEL 9/10, CentOS 8:

sudo dnf install -y iptables-services

CentOS 7:

sudo yum install -y iptables-services

This gives you the iptables and ip6tables systemd services and two rule files:

  • /etc/sysconfig/iptables for IPv4
  • /etc/sysconfig/ip6tables for IPv6

Step 3: check the ruleset before you start it (SSH!)

The service loads /etc/sysconfig/iptables on start. The packaged default looks like this:

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

It allows established connections, ping, loopback and SSH on port 22, and rejects everything else. That's a sane start, with two traps:

  • SSH on a custom port. Change --dport 22 to your port before starting the service.
  • Web or other services. Add their ports above the REJECT line, for example:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

If you want a fuller example with comments, I have an iptables configuration example script you can adapt.

Do the same review in /etc/sysconfig/ip6tables if the server has IPv6.

Step 4: start iptables and enable it at boot

sudo systemctl enable --now iptables
sudo systemctl enable --now ip6tables

Check that it's running and the rules loaded:

sudo systemctl status iptables
sudo iptables -L -n -v --line-numbers

Keep your current SSH session open and test a new login from a second terminal before you log out.

Saving rules you add on the command line

Rules added with iptables -A live in memory only and are gone after a reboot or service restart. To write the running ruleset back to /etc/sysconfig/iptables:

sudo service iptables save

That's the same as:

sudo iptables-save | sudo tee /etc/sysconfig/iptables

My rule: edit /etc/sysconfig/iptables and run systemctl restart iptables, rather than adding rules live and saving later. The file stays the single source of truth, and you can keep it in git.

Docker and the iptables service

If the host runs Docker, be careful with systemctl restart iptables. Docker adds its own NAT and forwarding rules at runtime, and a restart reloads only what's in the file, so container networking breaks until Docker recreates its rules:

sudo systemctl restart iptables
sudo systemctl restart docker

The same applies to service iptables save: it saves Docker's dynamic rules into your file too, and that gets messy. Edit the file by hand instead.

Or skip iptables and use nftables

On Rocky, Alma and RHEL 9/10, the native option is the nftables service, which works the same way: one file, loaded at boot.

sudo systemctl disable --now firewalld
sudo systemctl mask firewalld
sudo systemctl enable --now nftables

Rules go in /etc/sysconfig/nftables.conf. If you already have an iptables ruleset, you don't need to rewrite it by hand. iptables-restore-translate converts the whole file:

iptables-restore-translate -f /etc/sysconfig/iptables

Run on the default ruleset above, it prints:

add table ip filter
add chain ip filter INPUT { type filter hook input priority 0; policy accept; }
add rule ip filter INPUT ct state related,established counter accept
add rule ip filter INPUT ip protocol icmp counter accept
add rule ip filter INPUT iifname "lo" counter accept
add rule ip filter INPUT ct state new tcp dport 22 counter accept
add rule ip filter INPUT counter reject with icmp type host-prohibited
...

Single rules work too:

$ iptables-translate -A INPUT -p tcp --dport 443 -j ACCEPT
nft 'add rule ip filter INPUT tcp dport 443 counter accept'

If you just want something simple that survives the next RHEL release, firewalld itself is fine as well. firewall-cmd --permanent --add-service=https and a reload covers most servers.

CentOS 8 and 7 are end of life

CentOS 8 reached end of life at the end of 2021 and CentOS 7 in June 2024. The commands above still work on them, but the systems no longer get security updates, and on CentOS 8 even dnf fails until you point it at the vault. See the fix for Failed to download metadata for repo AppStream on CentOS 8. If you can, move to Rocky Linux or AlmaLinux.

On Debian and Ubuntu the setup is different: see installing iptables on Ubuntu 24.04 or on Debian 11.

FAQ

Is iptables installed by default on Rocky Linux 9?

The iptables command usually is, because firewalld depends on iptables-nft. The iptables service that loads /etc/sysconfig/iptables at boot isn't. That's the iptables-services package.

rpm -q iptables-services says it's not installed

On EL9 the package is called iptables-nft-services. dnf install iptables-services resolves to it, but rpm -q looks for the exact name. Check with rpm -q iptables-nft-services instead.

Can I run firewalld and iptables at the same time?

Don't. Both write to the same netfilter tables and will overwrite or conflict with each other's rules. Pick one and mask the other.

I locked myself out of SSH. What now?

Use the console of your VM or hosting provider (Proxmox console, VNC, IPMI), fix the port in /etc/sysconfig/iptables and run systemctl restart iptables. Or stop the service with systemctl stop iptables, which leaves no rules loaded.

Should I use iptables or nftables on a new server?

nftables or firewalld. iptables on RHEL 9 and 10 is a compatibility layer over nftables that Red Hat has deprecated. Use it when you have existing iptables rulesets or scripts to carry over, not for a fresh design.