Skip to content

Adrian Giacometti

  • Home
  • Useful links
  • About Me

How to understand Linux IPTables

5th November 2021 by Adrian Giacometti

In my previous 2 posts I played around Linux Networking with the module iproute2.

  • How to configure 1 Linux host with 2 NICs and 2 default gateways
  • How to setup 2 network isolated Docker containers (front-end and back-end)

Now that pure routing is done, the next step is iptables, as a Firewall module.

I’ve to admit that coming from traditional networking it will be weird, but in this new context of Software Defined “Everything” and Clouds, it is important to get over with it.

I will try to make it as simple and short as possible. Then is on your side to go deeper if you want (netfilter, hooks, etc).

Let’s go!

New basic concept: Chains (flow directions) -> Tables (with rules) -> Targets (what to do after a rules was matched)

There are 5 tables and 5 chains to play with. The tables are fixed, but you can add User-defined chains (more on this later, spoiler alert Docker and libvirt).

So there will be a mix of 5 tables x 5 chains. This is the first pain point to grasp.

CHAINS

The traffic flow through the 5 chains: Pre-routing, Input, Forward, Output and Post-routing

Pay attention only to the pre-routing chain. Here it will be decided which path to follow.

TABLES

And the rules will be on the tables.

So, Firewall rules are organized in different tables, each one having an specific meaning:

  • Filter: to filter traffic (src and des IP, TCP, UDP, etc)
  • NAT: for address translation
  • Mangle: to modify IP headers and optionally add a mark to the packet. That mark could be used in the following tables or chains as the packet goes through the stack. (This is what I mention in my previous posts about using marks to force routing instead of using the module iproute2, or in IPTables lingo to JUMP between chains)
  • Raw: since IP tables is a stateful Firewall, at this table the connection tracking will take place. (if there is not a previous mark from the Mangle table)
  • Security: It will add marks per packet or per connection. ONLY useful if you are in an SELinux environment.

Now, this is the tricky part, read slowly.

Each table have a dedicated section to each chain of the previous diagram (Pre-routing, Input, Forward, Output, Post-routing). Notice that not all tables have a portion in all chains.

Tables in chains, and rules in Tables will be evaluated from top to bottom.

Let’s update the diagram.

TARGETS

Apply to tables and they will dictate if the rule matching has to continue going down the table, or it has to exit to the next table or chain. Same effect as a DENY ALL in the middle of an ACL.

There are 2 kinds of targets. 

  • Terminating: accept, drop, reject, etc
  • Non-terminating: log , trace, JUMP to another table, etc

Ok, let’s recap.

  1. A packet arrives to INPUT Chain (or any other chain)
  2. the rule matching start against the Tables
  3. after a matching rule, the Target will dictate what to do next: accept, drop, reject, jump, log, etc

Crazy IPTables

IPTables is huge topic, it wouldn’t fit not even in 5 posts.

So I will give you a glimpse of how crazy this could get. This is why for scale we need orchestration systems.

(At the bottom there are some basic tutorials for you to keep digging and learning)

The following screen capture shows my LAB Linux, I have 1 Docker container and Vagrant with Libvirt.

All traffic is allowed, so if this already permits all, imagine when you start having specific rules 🙁

Try to follow the sequences with the simplest view with the full rules list, top to bottom, which includes chains, tables, and targets.

List rules (raw and mangle tables are not included)

List chains

More examples and tutorials

If you want to see more tutorials I recommend you to check this ones. There a lot of good examples:

How To Set Up a Firewall Using Iptables on Ubuntu 14.04

Iptables Essentials: Common Firewall Rules and Commands
 
How To List and Delete Iptables Firewall Rules
 
How To Set Up an Iptables Firewall to Protect Traffic Between your Servers
 
An In-Depth Guide to iptables, the Linux Firewall 

Maybe someday I will make another post on how to debug iptables.

Thanks and have fun with this monster! 😉

Share on Social Media
linkedin twitter email

Post navigation

Previous Post:

How to set up 2 network isolated Docker containers (front and back-end)

Next Post:

Unleashing the Power of NetDevOps: Simplifying Network Operations with Automation

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Translate to your language

Site search

Tags

ansible automation aws bcp books chatbot chatops cisco cisco aci cloud automation desing devops drp enterprise f5 fastapi free GCP gitlab ci gitops high availability internet iproute2 isp learning linux monitoring nat netdevops netmiko netops network network automation network backup opensource oracle oci python redundancy saltstack slack slackops terraform vpn vrf webinar

Blog Stats

  • 21,120 hits

RSS ipSpace.net blog

  • Network Digital Twins: Between PowerPoint and Reality
  • Dear Vendors, EVPN Route Attributes Matter
  • Public Videos: Whole IPv6 Curriculum
  • netlab 25.06: Fixing Nokia SR-OS Configuration Templates
  • Finding Source Routing Paths

RSS Potaroo blog

  • A QUIC Progress Report
  • A Day in the Life of BGP
  • Resilience in the RPKI
  • Analysis of a Route Leak
  • Jevons Paradox and Internet Centrality

Archives

  • 2024 (1)
    • January (1)
  • 2023 (1)
    • August (1)
    • July (1)
  • 2021 (1)
    • November (1)
    • August (2)
    • July (1)
    • May (2)
    • April (3)
    • February (1)
  • 2020 (1)
    • December (2)
    • October (1)
    • September (1)
    • June (1)
    • April (5)
    • March (1)
    • February (2)
    • January (2)
  • 2019 (1)
    • December (3)

Follow & Contact info

  • LinkedIn
  • GitHub
  • Twitter
  • YouTube
  • Email
  • RSS Feed

Subscribe to stay updated

Loading
© 2025 Adrian Giacometti