All guides
Home Glossary iptables
Term

iptables

A command-line utility for configuring IP packet filter rules and network address translation in the Linux kernel.

iptables is a command-line utility for configuring IP packet filter rules and network address translation (NAT) in the Linux kernel via the Netfilter framework. It enables system administrators to control how incoming, outgoing, and forwarded network packets are handled at the operating system level. While newer tools exist, it remains a foundational component of Linux server security and container networking.

How it works

The utility organizes firewall logic into tables, which contain chains of rules evaluated sequentially as packets traverse the Linux network stack. When a packet matches a rule's criteria—such as source IP, destination port, or protocol—it triggers a target action like ACCEPT, DROP (silently discard), or REJECT (discard and send an error response).

  • Filter table: The default table responsible for deciding whether a packet is allowed to continue to its destination, primarily using the INPUT, OUTPUT, and FORWARD chains.
  • NAT table: Handles Network Address Translation using PREROUTING, POSTROUTING, and OUTPUT chains to rewrite source or destination addresses, enabling port forwarding and masquerading.
  • Mangle and raw tables: Used for specialized packet header alterations (such as modifying Time-to-Live values) and bypassing connection tracking before stateful inspection occurs.
  • Stateful inspection: Through the conntrack kernel module, the firewall tracks connection states (NEW, ESTABLISHED, RELATED, INVALID), allowing return traffic for approved outbound connections without opening broad inbound ports.

Example

In a typical web server setup, an administrator permits established connections, SSH access, and web traffic while dropping all other unsolicited incoming packets. The following commands configure this baseline policy and block a specific abusive IP address:

# Allow traffic from existing connections and loopback interface
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT

# Allow SSH (port 22) and HTTPS (port 443)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# Block a specific malicious IP address
iptables -A INPUT -s 192.168.1.10 -j DROP

# Set default policy to drop all other incoming traffic
iptables -P INPUT DROP

What matters when choosing a server or hosting

When selecting a hosting environment, the level of access you have to kernel networking determines how you manage packet filtering:

  • Virtualization type: Full virtualization platforms (such as KVM) provide a dedicated kernel, giving you unrestricted access to load Netfilter modules and customize rules. Older container-based VPS platforms may restrict certain kernel modules like conntrack or NAT tables.
  • Cloud provider firewalls vs. host firewalls: Cloud platforms often provide external security groups alongside OS-level firewalls. Relying on host-level rules gives granular, portable control across providers, while external firewalls block volumetric junk traffic before it reaches your server's network interface.
  • Container workloads: If you run Docker or Kubernetes, the container runtime automatically injects its own NAT and forwarding rules to route traffic to containers, requiring careful coordination with manual host rules.

Common mistakes

  • Locking yourself out over SSH: Setting the default INPUT policy to DROP before adding an explicit rule to allow port 22 (or your custom SSH port) immediately terminates your remote session.
  • Forgetting rule persistence: Rules added via the command line reside in volatile memory and vanish upon reboot unless saved using a package like iptables-persistent or a systemd service.
  • Misunderstanding rule order: Because chains are evaluated top-to-bottom until the first match, placing a broad DROP rule above specific ACCEPT rules prevents traffic from ever reaching the allow rules.
  • Ignoring IPv6 traffic: Standard commands only govern IPv4 packets; leaving ip6tables unconfigured can leave an IPv6-enabled server completely exposed.

FAQ

How does iptables differ from nftables?

nftables is the modern replacement for iptables in the Linux kernel. It unifies IPv4 and IPv6 rule management into a single syntax, avoids duplicating rules across separate utilities, and offers faster rule updates and better performance for large rulesets.

Do I need iptables if I use UFW or firewalld?

No, you generally should not run them manually alongside higher-level tools. Both UFW (Uncomplicated Firewall) and firewalld act as user-friendly frontends that translate simple commands into underlying iptables or nftables rules automatically.

Why does Docker bypass my INPUT chain rules?

Docker routes container traffic through the FORWARD chain and inserts its own DOCKER chains in the NAT and filter tables before standard host rules are evaluated. To restrict container access, you must place rules in the DOCKER-USER chain rather than INPUT.

All glossary terms

Glossary

Related terms

Full glossary

New guides every week.

A digest of the best pieces on servers, VPN and self-hosted software. No ads, no spam.

1 email a weekOne-click unsubscribeRU · EN
Only slipjar.app articles, nothing else.