LLI Wiki — My Digital Knowledge Base

Linux firewall cheat sheet: iptables, nftables and firewalld

Common iptables, nftables and firewalld commands: rules, logging, blocklists, zones and services.

Cheat sheet of common commands to manage a Linux firewall with iptables, nftables and firewalld: rules, logging, blocklists and zones.

iptables

Configuration files on RHEL/CentOS: /etc/sysconfig/iptables, /etc/sysconfig/iptables-config, /etc/sysconfig/ip6tables, /etc/sysconfig/ip6tables-config.

Show, save, restore

iptables -vnL
iptables -L -n --line-numbers
iptables -t nat -L --line-numbers
iptables-save > rules.txt
iptables-restore < rules.txt
watch -d -n 2 sudo iptables -nvL

Add, insert, delete rules

# block an address
iptables -A INPUT -s 192.0.2.13 -j DROP
iptables -A INPUT -s 192.0.2.13 -p tcp --destination-port 25 -j DROP

# insert a rule at a given position (see --line-numbers)
iptables -I INPUT 23 -p tcp --dport 5222 -j ACCEPT

# delete rule number N
iptables -D INPUT N

# flush all rules
iptables -F

Logging

Simple log then drop:

iptables -A INPUT -j LOG --log-prefix "INPUT:DROP:" --log-level 6
iptables -A INPUT -j DROP

Custom chains to log then accept or drop:

iptables -N LOG_ACCEPT
iptables -A LOG_ACCEPT -j LOG --log-prefix "INPUT:ACCEPT:" --log-level 6
iptables -A LOG_ACCEPT -j ACCEPT

iptables -N LOG_DROP
iptables -A LOG_DROP -j LOG --log-prefix "INPUT:DROP: " --log-level 6
iptables -A LOG_DROP -j DROP

iptables -A <chain> <conditions> -j LOG_ACCEPT
iptables -A <chain> <conditions> -j LOG_DROP

Follow the logs:

sudo multitail -f /var/log/kern.log
sudo journalctl -f

Denial-of-service mitigation

Examples based on this article (logdrop is a custom chain you must create, modelled on LOG_DROP):

# invalid packets
iptables -t mangle -A PREROUTING -m conntrack --ctstate INVALID -j DROP

# new TCP packets that are not SYN
iptables -t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j logdrop

# SYN with a suspicious MSS value
iptables -t mangle -A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j logdrop

# bogus TCP flags
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP

# ICMP (often unnecessary)
iptables -t mangle -A PREROUTING -p icmp -j DROP

# fragments
iptables -t mangle -A PREROUTING -f -j DROP

# limit connections per source IP
iptables -A INPUT -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset

# limit RST packets
iptables -A INPUT -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT
iptables -A INPUT -p tcp --tcp-flags RST RST -j DROP

# limit new TCP connections per second
iptables -A INPUT -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT
iptables -A INPUT -p tcp -m conntrack --ctstate NEW -j DROP

nftables

References: nftables wiki, Sets, Arch Linux wiki.

Service and persistence

sudo systemctl start nftables
sudo systemctl stop nftables
sudo systemctl enable nftables

# save and reload
sudo nft -s list ruleset > /etc/nftables.conf
sudo nft -f /etc/nftables.conf

Show

sudo nft list ruleset
sudo nft -s -a -n list ruleset          # -a: handles, -n: numeric
sudo nft -a -n list tables
sudo nft -a -n list table inet my_table
sudo nft list set inet black_table blacklist_addr

Remove

nft flush ruleset                        # remove everything
nft flush table ip filter                # empty a table
nft delete table ip filter               # delete a table
nft flush chain ip filter input          # empty a chain
nft delete chain ip filter input         # delete a chain
nft delete rule ip filter input handle 18   # delete a rule by its handle

Add rules at a position

# before the rule at position 8
sudo nft insert rule ip filter output position 8 ip daddr 192.0.2.22 drop
# after
sudo nft add rule ip filter output position 8 ip daddr 192.0.2.22 drop

sudo nft add rule ip filter input position 11 tcp dport 22 accept
sudo nft add rule ip filter input position 11 tcp dport 5200-5300 accept

Blocklist with a set

sudo nft add table inet black_table
sudo nft add set inet black_table blacklist_addr { type ipv4_addr \; flags interval \; }
sudo nft add chain inet black_table blacklist_chain { type filter hook input priority filter - 1 \; policy accept \; }
sudo nft add rule inet black_table blacklist_chain ip saddr @blacklist_addr log prefix \"nft black list : \" reject with icmp port-unreachable

# add / remove addresses
sudo nft add element inet black_table blacklist_addr { 192.0.2.21, 192.0.2.23 }
sudo nft add element inet black_table blacklist_addr { 192.0.2.26-192.0.2.27, 203.0.113.101 }
sudo nft delete element inet black_table blacklist_addr { 192.0.2.23 }
sudo nft get element inet black_table blacklist_addr { 192.0.2.21 }

# empty the set
sudo nft flush set inet black_table blacklist_addr

Result:

table inet black_table {
        set blacklist_addr {
                type ipv4_addr
                elements = { 192.0.2.21, 192.0.2.23 }
        }

        chain blacklist_chain {
                type filter hook input priority -1; policy accept;
                ip saddr @blacklist_addr log prefix "nft black list : " reject with icmp 3
        }
}

Logging

sudo nft add rule ip filter input log
sudo nft add rule ip filter forward log
sudo nft add rule ip filter output log

sudo journalctl --dmesg -f
cat /proc/net/netfilter/nf_log

Reject what no other rule handled

nft add rule inet my_table my_input meta l4proto udp reject
nft add rule inet my_table my_input meta l4proto tcp reject with tcp reset
nft add rule inet my_table my_input counter reject with icmpx port-unreachable

Firewall example

table inet my_table {
    set LANv4 {
        type ipv4_addr
        flags interval
        elements = { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 }
    }

    chain my_input_lan {
        tcp dport 445 accept comment "Accept SMB"
        udp dport 69 accept comment "Accept TFTP"
    }

    chain my_input {
        type filter hook input priority filter; policy drop;
        iif "lo" accept comment "Accept any localhost traffic"
        ct state invalid drop comment "Drop invalid connections"
        ct state established,related accept comment "Accept traffic originated from us"
        icmp type { destination-unreachable, router-advertisement, router-solicitation, time-exceeded, parameter-problem } accept comment "Accept ICMP"
        ip saddr @LANv4 jump my_input_lan comment "Connections from private IP address ranges"
        tcp dport 22 accept comment "Accept SSH"
        tcp dport { 80, 443 } accept comment "Accept HTTP/HTTPS"
        log prefix "nft drop: "
    }

    chain my_forward {
        type filter hook forward priority filter; policy accept;
    }

    chain my_output {
        type filter hook output priority filter; policy accept;
    }
}
table ip nat {
    chain POSTROUTING {
        type nat hook postrouting priority srcnat; policy accept;
        ip saddr 192.0.2.0/24 oifname "eth0" masquerade
    }
}

Allow traffic from a libvirt bridge: sudo nft insert rule inet my_table my_input iif "virbr0" accept.

To forward a port to another address (DNAT), see this tutorial.

nftables hooks by family and layer

firewalld

Status and reload

sudo firewall-cmd --state
sudo systemctl status firewalld
sudo firewall-cmd --reload

Services and ports

# runtime + permanent
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=http

# or permanent then reload
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --reload

sudo firewall-cmd --zone=public --remove-service=http --permanent
sudo firewall-cmd --get-services

# port, then copy runtime to permanent
sudo firewall-cmd --zone=public --add-port=3306/tcp
sudo firewall-cmd --runtime-to-permanent

Zones

firewall-cmd --list-all-zones
firewall-cmd --get-zones
firewall-cmd --get-active-zones
firewall-cmd --get-default-zone
firewall-cmd --zone=public --list-all
firewall-cmd --list-services
firewall-cmd --list-ports

sudo firewall-cmd --set-default-zone=internal
firewall-cmd --permanent --zone=internal --add-interface=ens34

Predefined zones live in /usr/lib/firewalld/zones, custom zones in /etc/firewalld/zones.

Custom multi-port service

Services are defined in /usr/lib/firewalld/services (or /etc/firewalld/services):

<?xml version="1.0" encoding="utf-8"?>
<service>
  <port port="port1" protocol="proto1"/>
  <port port="port2" protocol="proto2"/>
</service>
firewall-offline-cmd --new-service-from-file=service.xml --name=My_Service
firewall-cmd --reload
firewall-cmd --add-service My_Service

More commands: firewalld cheat sheet.

firewalld command cheat sheet

Notes

Mastodon LLIMastodon SocialFosstodon