How-to

iptables on Linux: How It Works and a Starter Rule Set

iptables is the classic command for the Linux packet filter. You put rules into chains, packets walk the chains in order, and the first matching rule decides their fate.

What is iptables in one paragraph?

The Linux kernel can inspect every packet that enters, leaves or passes through a machine. iptables is the user tool that tells it what to do with them. Rules sit in ordered lists called chains, chains are grouped into tables by purpose, and each rule has match conditions plus a target such as ACCEPT or DROP. When nothing matches, the chain policy applies. Your machine arrives with full root and no firewall preferences imposed beyond our edge DDoS filtering, so the host firewall is yours to design.

Which tables and chains exist?

TableChainsUsed for
filter (default)INPUT, FORWARD, OUTPUTAllow or block traffic
natPREROUTING, OUTPUT, POSTROUTINGPort forwarding and masquerading
mangleall fiveChanging packet marks and header fields
rawPREROUTING, OUTPUTSkipping connection tracking for chosen flows

INPUT sees packets addressed to the machine itself, OUTPUT sees what it sends, and FORWARD sees packets routed through it to VMs or containers. Most single-purpose servers only ever need INPUT rules.

How is a rule built?

A rule is a chain, matches and a target:

iptables -A INPUT -p tcp -s 192.0.2.0/24 --dport 3306 -m conntrack --ctstate NEW -j ACCEPT

Read it left to right: append to INPUT; protocol TCP; from that network; to port 3306; only new connections; accept. -A adds at the end and -I inserts at the top or at a given position, which matters because the first match wins. Common targets are ACCEPT, DROP (silently discard), REJECT (refuse with an error), LOG (write to the kernel log and continue) and a jump to your own chain.

What is a sensible starter rule set?

A default-deny inbound policy with a few openings covers most web servers. Apply it as one file with iptables-restore, which replaces the set atomically, so you never sit half-configured:

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -j ACCEPT
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m limit --limit 6/min --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
COMMIT

Save that as /root/fw.rules, test with iptables-restore --test /root/fw.rules, then load it with iptables-restore < /root/fw.rules. Loopback stays open because many services talk to themselves; established traffic is allowed so replies to your own outbound requests get back in; ICMP stays open so path MTU discovery and ping keep working; SSH new connections are rate limited. If you moved SSH to another port as in Change the SSH port, change 22 here first.

How do I keep the rules after a reboot?

On Ubuntu and Debian, apt install iptables-persistent, then netfilter-persistent save writes /etc/iptables/rules.v4. On Rocky Linux and AlmaLinux, either use firewalld (the default) or install iptables-services, disable firewalld and save to /etc/sysconfig/iptables. Running both at once makes them overwrite each other.

How do I log what gets dropped?

Insert a rate-limited LOG rule just before the end of INPUT, so you see blocked traffic without flooding the journal:

iptables -A INPUT -m limit --limit 3/min -j LOG --log-prefix "fw-drop " --log-level 4
journalctl -k -g 'fw-drop' --since '30 min ago'

Because LOG does not stop processing, the packet continues to the chain policy and is dropped as before.

Is iptables still current, or should I use nftables?

nftables is the newer framework in the kernel, and on every current distribution we install the iptables command already writes into it through the iptables-nft compatibility layer. So iptables syntax remains fully usable and is what most tutorials, Docker and fail2ban still generate. For a new, hand-written rule set, native nft is cleaner: one tool, sets and maps, and IPv4 and other families in one file. Either choice is fine; just do not mix hand-written rules from both tools in the same tables. FreeBSD uses pf or ipfw instead, so none of this applies to the FreeBSD 14 image.

What does the host firewall add to edge filtering?

Our edge DDoS filtering soaks up volumetric floods before they reach your port. It does not know which of your services should be public. The host firewall closes everything you did not mean to expose: a database bound to all addresses, a metrics exporter, a test web server. Pair it with Set up fail2ban for SSH and login brute force, and read Edge DDoS filtering to see what the edge layer covers.

Next steps: Delete an iptables rule for safe removals and iptables port forwarding when you add VMs or containers.

Low-Cost Machines for a First Firewall

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
E2Intel Xeon E3-1270 v34 / 832 GB DDR31x 480 GB SSD2x 1 Gbps$38.32$114.96 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S3Intel Xeon E-2356G6 / 1264 GB DDR42x 1 TB NVMe2x 10 Gbps$80.38$80.38 (1 month)

Keep Reading

Delete an iptables rule

Remove a single iptables rule by number or by its exact spec, clear a chain safely and make sure the change survives a reboot.

Open

Set up fail2ban

Install fail2ban, write a jail.local for SSH and nginx, read the ban list and unban an address you locked out by mistake.

Open

Harden SSH on Linux

A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.

Open

Edge DDoS filtering

How common volumetric attacks are filtered at the network edge before they reach your ports, and what you still handle on the box.

Open

FAQ

Quick Answers

DROP discards the packet without reply, so the sender waits for a timeout. REJECT answers with an error, so the sender fails fast.

Yes. Packets are checked top to bottom and the first matching terminating rule wins. Use -I to place a rule above an existing one.

Yes. Edge filtering absorbs floods, while the host firewall decides which of your own services are reachable at all.

No. FreeBSD uses its own firewalls, pf and ipfw. iptables is Linux only.

The commands and syntax are the same. Rules are stored in nftables underneath, which you can inspect with nft list ruleset.