0Pricing
Linux Networking & TCP/IP for Developers · Lesson

Linux Firewall (Netfilter/iptables)

Understand the basics of Linux firewalls using `iptables` to filter traffic and secure your systems.

Linux Firewall (Netfilter/iptables) is a free Linux Networking & TCP/IP for Developers lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Networking & TCP/IP for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Firewalls: Your Network Guardian

What is a firewall? It's like a security guard for your network, controlling what traffic goes in and out. In Linux, the core firewall framework is called Netfilter. We use a command-line tool called iptables to manage its rules.

Netfilter: The Kernel's Core

Netfilter is a powerful framework built right into the Linux kernel. It allows different kernel modules to inspect, modify, and drop network packets.

Think of it as the engine behind the firewall. It provides "hooks" where packet processing can be intercepted.

`iptables`: Managing Firewall Rules

While Netfilter is in the kernel, iptables is the command-line utility you use to interact with it. It lets you define rules that tell Netfilter what to do with specific packets.

These rules are organized into tables and chains, which we'll explore next.

`iptables` Chains: Traffic Paths

iptables organizes rules into chains. These are ordered lists of rules that packets are checked against. The three most common built-in chains are:

  • INPUT: For packets destined for the local system.
  • OUTPUT: For packets originating from the local system.
  • FORWARD: For packets passing through the system (e.g., a router).

Default Actions: Chain Policies

Each chain has a default policy, which is the action taken if no rule in the chain matches a packet. Common policies are:

  • ACCEPT: Let the packet through.
  • DROP: Silently discard the packet (sender gets no response).
  • REJECT: Discard the packet and send an error message back to the sender.

It's common to set default policies to DROP for security.

Viewing Current `iptables` Rules

Before adding rules, it's good to see what's already there. You can list all current iptables rules with the -L option. Adding -n shows IP addresses numerically, and -v adds verbosity.

sudo iptables -L -n -v

Allowing Inbound SSH Traffic

Let's add a rule to allow incoming SSH connections (port 22). We'll append (-A) this rule to the INPUT chain, specifying TCP protocol (-p tcp) and destination port (--dport 22). The action (-j) will be ACCEPT.

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Blocking Outbound Ping Requests

Now, let's block all outbound ping requests (ICMP protocol). We'll append this rule to the OUTPUT chain, specifying the ICMP protocol. The action will be DROP.

This means your system won't send ping requests, but might still receive them if not blocked on INPUT.

sudo iptables -A OUTPUT -p icmp -j DROP

Making Rules Permanent

iptables rules are volatile; they disappear on reboot! To make them permanent, you need to save them. On many systems, you'd use iptables-save to export rules and iptables-restore to load them.

Some Linux distributions use specific services (like netfilter-persistent) or files (e.g., /etc/sysconfig/iptables) to manage persistence.

Firewall Chains Check

Based on what you've learned, which iptables chain would typically handle network packets that are trying to reach a service running on your local machine?

Recap: `iptables` Firewall Basics

You've learned about Netfilter, the kernel's firewall framework, and iptables, the user-space tool to manage its rules. We covered the main chains (INPUT, OUTPUT, FORWARD) and policies (ACCEPT, DROP, REJECT).

You also saw how to list, add basic rules, and the importance of saving them for persistence. This is a crucial step in securing any Linux system!

Frequently asked questions

Is the “Linux Firewall (Netfilter/iptables)” lesson free?

Yes — the full text of “Linux Firewall (Netfilter/iptables)” is free to read here on the web, and the Linux Networking & TCP/IP for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Networking & TCP/IP for Developers course, upgrade to CoddyKit PRO.

What will I learn in “Linux Firewall (Netfilter/iptables)”?

Understand the basics of Linux firewalls using `iptables` to filter traffic and secure your systems. You practise Linux Networking & TCP/IP for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Linux Networking & TCP/IP for Developers?

No prior experience is required. Linux Networking & TCP/IP for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Linux Firewall (Netfilter/iptables)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Linux Networking & TCP/IP for Developers lesson?

Yes. Every Linux Networking & TCP/IP for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Packet Capture with Wireshark/tcpdump
  2. Network Performance Tools
  3. Linux Firewall (Netfilter/iptables)
  4. DNS Diagnostics with dig and nslookup
← Back to Linux Networking & TCP/IP for Developers