🧱 iptables Demystified: Build a Firewall Like a Hacker (But For Good)
Rajpreet Gill

Rajpreet Gill @rajpreet_gill

Joined:
Dec 10, 2024

🧱 iptables Demystified: Build a Firewall Like a Hacker (But For Good)

Publish Date: May 7
4 2

"Ever accidentally locked yourself out of your own server? Yeah, me too. Let's fix that.”

👀 What’s This All About?

Imagine your Linux server is a castle. You’re the ruler (root user), but you’ve left the front gates wide open. Anyone — bots, hackers, curious squirrels — can walk in.

Enter iptables, the grumpy gatekeeper with a clipboard. You give him rules, and he checks every visitor (aka network packet). Only the worthy get through.

Whether you're:

  • Spinning up cloud VMs
  • Hosting your app backend
  • Dealing with pesky port scanners

..you need to understand iptables.

🧭 Table of Contents

⚔️ Why Should Developers or DevOps Folks Care?

Because security isn’t optional anymore.

If you're running anything public-facing — from APIs to static sites — and not filtering traffic, you're just hoping the internet will be nice to you. Spoiler: It won’t.

🎯 So, What Exactly Is iptables?

  • 🔧 A command-line firewall tool for Linux.
  • 🧠 Works with netfilter, part of the kernel.
  • 🔐 Lets you filter, allow, block, or reroute traffic based on rules you define.

📦 The Core Concept: Tables and Chains

Think of it like a mailroom with three conveyor belts:

Image description

Each chain is part of a table (usually filter, nat, or mangle), and rules are just... filters.

🚀 Quickstart: Playing with Fire (Safely)

🧼 Step 1: List Your Current Rules:

Image description

You’ll see existing rules, packet counts, and a lot of “ACCEPT” if it’s the default.

✅ Step 2: Allow Traffic to Port 80 (HTTP):

Image description

🗣️ Translation: “Allow TCP traffic coming to port 80.”

🔒 Step 3: Drop All Other Incoming Traffic (except SSH)

Image description

👻 Don’t lock yourself out! Always allow SSH before setting a DROP policy.

❌ Step 4: Block a Specific IP

Image description

🪓 Bye-bye, botnet!

✅ Test: Check Listening Ports on the Server

Run this on your Linux server to make sure services are actually listening on those ports:

Image description

📘 Pro Tip: Save Your Rules

Otherwise, they disappear on reboot.

Image description

Or for systemd-based distros:

Image description

🐳 Wait, What About Docker?

Docker loves iptables. It auto-writes rules to make containers talk to each other.

But sometimes it messes with your custom rules. If you’re managing rules manually, consider:

Image description
(Use with caution. You're taking control.)

🔥 Real-World Example: Lock Down Your Web Server

Image description
Boom 💥 — you've just built a basic fortress.

🧠 Bonus: Things Most People Forget

  • Always test in a VM first. Recovery from DROP-everything-without-SSH is painful.
  • Use conntrack to maintain state-aware firewalls.
  • Pair with fail2ban to auto-ban brute-force attackers.
  • iptables is being replaced by nftables in some distros — but iptables is still everywhere.

👋 Final Thoughts

You don’t have to be a networking wizard to use iptables. Think of it like a programmable bouncer for your apps. Learn the basics, write a few rules, save yourself from disaster.

Because firewalls aren't just for enterprise security teams. They're for you, too.

#30DaysLinuxChallenge #CloudWhisler
DevOps #Linux #RHCSA #Opensource #AWS #CloudComputing

Catch out by My LinkedIn profile
https://www.linkedin.com/in/rajpreet-gill-4569b4161/

Comments 2 total

  • Nevo David
    Nevo DavidMay 8, 2025

    been there, locked myself out messing with firewall stuff lol - always makes me rethink every rule i set. you ever figure out if taking total control beats letting docker handle it?

    • Rajpreet Gill
      Rajpreet GillMay 8, 2025

      Haha yep, locked myself out once and learned the hard way! 😅 I usually let Docker handle iptables unless I need tight control — but when I do, I make sure to allow Docker bridge traffic.

Add comment