List filter rules numerically with packet counters and line numbers for deletion
iptables -L -n -v --line-numbersiptables essentials: listing and saving rules, default policies, allowing ports and subnets, connection tracking, logging and rate limits, NAT and port forwarding, rule persistence, deleting rules by line number, with nftables notes.
List filter rules numerically with packet counters and line numbers for deletion
iptables -L -n -v --line-numbersInspect the nat table for port forwarding and address translation
iptables -t nat -L -n -vPrint rules as reusable commands, useful before backups
iptables -SSearch a chain for a specific port rule
iptables -S INPUT | grep 8080Export all rules before changes so a rollback is possible
iptables-save > /root/iptables.bakShow the version; RHEL 8+ uses nftables with an iptables compatibility layer
iptables -VNewer systems accept iptables commands backed by nftables.
Set default policies; allow SSH before dropping INPUT or you lose remote access
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPTFlush filter and nat rules; on production this disrupts all traffic
iptables -F
iptables -t nat -FCreate a custom chain and jump to it to group rules by service
iptables -N APP_CHAIN
iptables -A INPUT -j APP_CHAINDelete a custom chain after flushing it and removing jumps
iptables -X APP_CHAINAllow loopback; many services rely on 127.0.0.1 traffic
iptables -A INPUT -i lo -j ACCEPTAllow SSH; restrict the source subnet when working remotely
iptables -A INPUT -p tcp --dport 22 -j ACCEPTAllow established and related connections, the basis of stateful filtering
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTAllow only a subnet to reach the database port
iptables -A INPUT -p tcp -s 10.0.0.0/24 --dport 3306 -j ACCEPTAllow several ports in one rule
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPTInsert at the top to bypass earlier DROP rules in an emergency
iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPTSilently drop traffic from an IP with no rejection response
iptables -A INPUT -s 203.0.113.10 -j DROPReject explicitly so clients fail fast instead of timing out
iptables -A INPUT -p tcp --dport 8080 -j REJECT --reject-with icmp-port-unreachableLog attempts with a rate limit so the kernel log is not flooded
iptables -A INPUT -p tcp --dport 22 -j LOG --log-prefix "SSH-ATTEMPT: " -m limit --limit 5/minRate-limit new connections to ease SYN floods; tune thresholds by load testing
iptables -A INPUT -p tcp --syn -m limit --limit 20/s --limit-burst 40 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROPEnable forwarding for NAT; persist it in sysctl.conf
sysctl -w net.ipv4.ip_forward=1Source NAT for outbound traffic when the public IP is dynamic
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADEForward local 8080 to an internal host; also allow it in FORWARD
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.10.21:80Allow forwarded traffic; the usual fix when DNAT works but traffic does not
iptables -A FORWARD -p tcp -d 192.168.10.21 --dport 80 -j ACCEPTStatic source NAT performs better than MASQUERADE with a fixed IP
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 203.0.113.20Save to the standard RHEL/CentOS path restored at boot
iptables-save > /etc/sysconfig/iptablesBack up the file first; a bad file can break networking after reboot.
Restore a full rule set from a file
iptables-restore < /etc/sysconfig/iptablesEnable boot restore with iptables-services on CentOS 7; prefer firewalld or nftables on RHEL 8+
systemctl enable --now iptablesPersist rules on Debian and Ubuntu with iptables-persistent
apt install iptables-persistent
netfilter-persistent saveDelete by line number from --line-numbers; numbers shift afterwards
iptables -D INPUT 3Delete by exact rule specification matching the original
iptables -D INPUT -p tcp --dport 8080 -j ACCEPTCheck whether a rule exists; exit code 0 means present, useful for idempotent scripts
iptables -C INPUT -p tcp --dport 22 -j ACCEPT; echo $?Replace a rule in place without changing order
iptables -R INPUT 2 -p tcp -s 10.0.0.0/24 --dport 22 -j ACCEPTReset packet counters before measuring hits
iptables -ZThis website uses cookies to ensure you get the best experience on our website. Privacy Policy.