Hello! Welcome to the next lesson on your path to becoming a security professional.
In our last session, we significantly hardened access to your virtual server by replacing passwords with SSH key-based authentication. This secures the "front door" to your system. Now, we're going to take a step back and look at the first line of defense that protects the entire server at the network level: the firewall. Understanding how firewalls work is essential for both defending systems and recognizing how to probe for weaknesses in a target's defenses.
Today, we'll focus on this learning outcome: Explain the purpose of a firewall and the difference between stateless and stateful inspection. We'll cover the fundamental role of a firewall and then dive into the two main operational models that determine their intelligence and effectiveness.
1. What is a Firewall? The Digital Bouncer
At its core, a firewall is a network security system that monitors and controls incoming and outgoing network traffic based on a set of predefined security rules. Think of it as a bouncer or a security guard standing at the boundary between two networks—typically, your trusted internal network and the untrusted external network, like the public internet. Its job is to examine every packet of data trying to cross this boundary and decide whether to allow it or block it.

Let's watch a brief video that introduces this concept.
This short clip from IBM Technology gives a great overview of a firewall's purpose and its typical placement in a network.
Watch from 05:23 to 05:55 to get a clear, concise definition of a firewall and its role as a network security device.
Firewalls can be hardware devices, software running on a general-purpose computer (like your Arch Linux machine), or a cloud-based service. Regardless of their form, their primary goal is the same: to create a barrier and enforce access control policies.
How a firewall makes its decisions is what separates the two main types: stateless and stateful.
2. Connection Fundamentals: A Quick Refresher
To understand the difference between stateless and stateful inspection, we first need to solidify our understanding of how a network connection works. Since you've mentioned that your networking knowledge is still developing, this section will be especially important.
A single interaction, like your browser fetching a webpage, isn't just one-way. It involves two parts:
- Request: Your computer (the client) sends a request to the web server.
- Response: The web server sends the webpage data back to your computer.
This exchange uses IP addresses and ports. Your client initiates the request from a random, high-numbered port called an ephemeral port and sends it to a well-known port on the server (e.g., port 443 for HTTPS). The server's response travels from its port 443 back to the specific ephemeral port your client used for the request.
This video breaks down this process beautifully. It is essential for understanding what follows.
Stateful vs Stateless Firewalls - You NEED to know the difference
The video 'Stateful vs Stateless Firewalls' by LearnCantrill provides an excellent foundation. We'll start with the introductory part that refreshes TCP/IP connection concepts.
Watch from the beginning to 07:59. Pay close attention to the explanation of request vs. response, ephemeral ports, well-known ports, and how connection direction (inbound/outbound) depends on your perspective (client vs. server). This is a critical building block.
3. Stateless Firewalls: The Packet-by-Packet Inspector
A stateless firewall, also known as a packet filter, is the simpler of the two types. It inspects every single packet in isolation, without any knowledge of previous packets. Its decisions are based solely on the information contained within that one packet's header:
- Source IP Address
- Destination IP Address
- Source Port
- Destination Port
- Protocol (e.g., TCP, UDP, ICMP)
The Analogy: This is like a security guard with no memory. They check every person's ID every single time they enter or leave a building, even if they just saw them 30 seconds ago.
The Consequence: Because a stateless firewall treats the request and the response as two completely separate, unrelated events, you must create rules for both directions of traffic.
- Rule 1 (Outbound Request): Allow traffic from your IP (any ephemeral port) to the web server's IP (port 443).
- Rule 2 (Inbound Response): Allow traffic from the web server's IP (port 443) to your IP (a wide range of ephemeral ports, since you don't know which one will be used).
This second rule is problematic. To allow legitimate return traffic, you have to open up thousands of potential ports, creating a larger attack surface.
This video segment explains the operational logic and drawbacks of stateless firewalls.
Stateful vs Stateless Firewalls - You NEED to know the difference
Let's continue with the LearnCantrill video to see how a stateless firewall handles traffic.
Watch from 07:59 to 12:07. Focus on how the stateless firewall requires two separate rules (one for the request, one for the response) and the security implication of having to allow the entire ephemeral port range for return traffic.
4. Stateful Firewalls: The Intelligent Conversation Tracker
A stateful firewall is far more intelligent. It not only inspects individual packets but also keeps track of the "state" of active network connections in a state table. It understands the context of a conversation.
When your client sends an initial request packet (a SYN packet in TCP), the stateful firewall sees it's a NEW connection. If the rule allows it, the firewall adds an entry to its state table for this connection. When the server's response packet arrives, the firewall checks its state table. It sees that this packet is part of an ESTABLISHED connection that it already approved. Because it's part of a known, legitimate conversation, the firewall allows the response through without needing a separate inbound rule.
The Analogy: This is a smart security guard who checks your ID when you first enter the building and remembers your face. When you pop outside for a moment and come back, they recognize you and let you back in without another full ID check. They know you're part of an "established session."

This method has two major advantages:
- Higher Security: It only allows return traffic that is a direct response to a connection you initiated. You don't need to keep a wide range of ports open for inbound traffic.
- Simpler Administration: You only need to write rules for initiating connections. The firewall handles the return traffic automatically.
Let's see this in action.
Stateful vs Stateless Firewalls - You NEED to know the difference
The final segment of the LearnCantrill video explains the logic and benefits of a stateful firewall.
Watch from 12:07 to 14:04. Notice how the stateful firewall intelligently identifies the response traffic and automatically allows it, simplifying the rules and enhancing security.
The following article provides a good textual summary and a handy table comparing the pros and cons of both approaches.
Traffic Filtering: Stateful, Stateless, and Something Between
This article, 'Traffic Filtering: Stateful, Stateless, and Something Between,' provides a concise written explanation of what we've just seen in the videos.
Read the sections 'Traffic filtering: stateless and stateful,' 'Stateless traffic filtering,' and 'Stateful traffic filtering.' Pay special attention to the table at the end that summarizes the pros and cons of each method.
5. Stateful Firewalls in Linux: iptables and conntrack
Now, let's bring this theory home to your Arch Linux environment. How does Linux implement a stateful firewall?
The classic Linux firewall tool is iptables. By itself, iptables can act as a simple stateless packet filter. However, when combined with the kernel module called conntrack (for "connection tracking"), it becomes a powerful stateful firewall.
conntrack is what maintains the state table. It categorizes all traffic into one of four states:
NEW: A packet trying to start a new connection.ESTABLISHED: A packet that is part of an existing connection.RELATED: A packet that is starting a new connection, but is related to an existing one (e.g., the data channel in some older protocols like FTP).INVALID: A packet that doesn't belong to any known connection or is malformed. Dropping these is a good security practice.
The single most important rule in a typical stateful iptables setup is one that says: "If a packet is in the ESTABLISHED or RELATED state, allow it." This one line automatically permits all return traffic for any connection you've allowed to start.
As an Arch user, the ArchWiki is your best friend. It has an excellent page that demonstrates how these states are used to build a simple stateful firewall.
Simple stateful firewall - ArchWiki
The ArchWiki page 'Simple stateful firewall' explains how iptables uses connection states. This connects the theory directly to the tools on your system.
Read the section titled 'The INPUT chain.' Focus on the explanation of the rule using --ctstate RELATED,ESTABLISHED. Also, note the part that explains that traffic falls into four state categories, which 'is what makes this a "stateful" firewall rather than a less secure "stateless" one.'
Conclusion
You've now covered the fundamental theory behind network firewalls, a critical component of any defensive security posture. Understanding this is key to both building secure systems and recognizing how to test the security of others.
Key Takeaways:
- A firewall is a security system that filters network traffic based on a set of rules, acting as a barrier between trusted and untrusted networks.
- Stateless firewalls inspect each packet in isolation. They are fast but less secure and require complex rulesets to manage both request and response traffic.
- Stateful firewalls maintain a state table to track active connections. They are more intelligent and secure, as they can automatically allow return traffic for legitimate, established connections.
- On Linux, the
iptablesutility, in conjunction with theconntrackkernel module, provides powerful stateful filtering by tracking connection states likeNEW,ESTABLISHED,RELATED, andINVALID.
Next Lesson Preview:
Theory is great, but practice is better. In the next lesson, we will get our hands dirty. You will use your newfound knowledge to create basic iptables rules to allow and block traffic based on IP, protocol, and port, putting this defensive theory into direct, practical application on your Linux system.