nftables vs iptables: Linux Firewall Setup
Firewalls are the first line of security between your Linux system and the outside world. Firewalls are basically packet filters. They look at network traffic that is coming in and going out, compare it to rules that have already been set, and then determine whether to let it through, block it, or drop it. This filtering is important for both servers and desktops because it makes sure that only valid, expected traffic gets to applications and that malicious or unauthorized packets are blocked.
Not only is a firewall on Linux a utility, but it's also part of the kernel networking stack. Linux has very efficient traffic control that can work on anything from a personal laptop to a high-performance data center router. This is because packet filtering is built right into the kernel. A correctly set up firewall is not an option for system administrators; it is necessary to keep services private, secure, and available.
In the past, Linux employed iptables, which is a user-space tool that works with the kernel's Netfilter framework. For almost twenty years, iptables was the norm. It powered everything from personal firewalls to enterprise gateways. But as time went on, its syntax became hard to use, it was hard to keep track of rules that were the same, and it had trouble growing in big situations.
The Linux kernel added nftables to fix these problems. Nftables is the current replacement for IPv4, IPv6, ARP, and other protocols. It puts them all under one set of rules. It makes it easier to write rules, cuts down on duplication, and adds strong ideas like sets and maps. This lets administrators manage big collections of rules in a smart and efficient way. In addition to making things easier to read, nftables speeds things up a lot by changing how rules are checked and applied.
At the same time, a lot of distributions also push firewalld, which is a front-end manager that makes iptables or nftables easier to use. Administrators can use zones (trust levels for networks) and services (predefined port/protocol combinations) to set policies instead of developing low-level rules. This is very helpful for those who aren't very good with computers or for automated deployment systems because it makes configuration easier while still using the Netfilter engine.
The change from iptables to nftables wasn't just for show; it was to fix long-standing problems with the architecture. With iptables, rules were implemented one after the other, which made things less efficient. Administrators also often copied rules from IPv4 to IPv6 tables.
It was easy to make mistakes when keeping track of big, complicated rulesets.
nftables fixes these problems by:
-
Using the same syntax for all protocols.
-
Allowing atomic updates, which replace the whole ruleset in one action (stopping partial updates that could interrupt traffic).
-
Making it easier to scale by allowing thousands of rules to be handled without a big drop in performance.
-
Giving counters and debugging tools to make things easier to see.
Big Changes Above iptables
Some of the most important benefits of nftables are:
- Efficiency: Optimized evaluation makes it faster to match packets.
- Scalability: Sets and maps let you use one rule to manage more than one address or port.
- Easier Syntax: Having one ruleset for both IPv4 and IPv6 cuts down on repetition and makes things easier.
- Unified Tooling: The nft command takes the place of several other tools, such as iptables, ip6tables, arptables, and ebtables.
firewalld: Making Things Easier
Not all administrators want or need to write raw firewall rules. This is when firewalld comes in handy. As a policy manager, it:
- Puts rules into groups like "public," "internal," or "dmz."
- Sets up services like SSH, HTTP, or DNS to automatically open the right ports.
- Changes are applied dynamically without closing current connections.
Firewalld is easier to use than iptables or nftables for managing Linux firewalls, but it still uses them underneath. For advanced users, nftables is still the most configurable option, but firewalld makes it easier for more people to administer firewalls.
What are Linux Firewalls?
A Linux firewall is a security tool that controls what network traffic can enter, depart, or pass through your system. It works as a traffic filter at its most basic level. Every packet of data that tries to traverse your network interfaces is checked against a set of rules that have already been configured. If it matches an allow rule, it goes through; if it matches a deny rule, it is blocked or dumped without a sound.
This may sound vague, but in reality, it is one of the best protections you can use. Firewalls protect you against both obvious attacks and services that you didn't even know were operating that could be exposed by accident.
-
Control: Firewalls in Linux let you decide what traffic can reach services on your machine, such SSH and HTTP.
-
Isolation: Keep trusted networks (like your office LAN) apart from untrusted ones (like the internet).
-
Enforcement: Make sure that security policies are always followed to meet the needs of the organization or the law.
-
Visibility: Keep track of and log all attempted connections to find any suspicious or illegal behavior.
How Linux Firewalls Work
The Netfilter framework, which is part of the Linux kernel, is what (Linux firewalls)[/docs/network-security-tutorials/best-linux-firewall-solutions-in-2022] use to work. When packets come in or go out of your computer, Netfilter stops them and checks them against your firewall rules. Tools like iptables (old) or nftables (new) take care of these rules.
Chains are smaller groupings of checkpoints that packets pass through. Tables are bigger groups of chains.
-
Tables tell the computer what kind of filtering to do (filtering, NAT, packet modification).
-
Chains tell the packet where to go (input, output, forward).
-
Rules are the exact orders (allow, drop, refuse, log).
You may see this in action by running the following command:
sudo nft list ruleset
This will show all the rules that are currently loaded, such as filtering, NAT, and custom chains.
On older systems still using iptables, the equivalent is:
sudo iptables -L -v -n
Here you’ll see the filter table with its chains and counters showing how many packets matched each rule. After running these commands, you can interpret the output to understand your firewall posture. For example, if the default policy of the INPUT chain is ACCEPT and there are no rules, it means your system is wide open to incoming connections.
Firewalls on Different Types of Systems
-
On Servers: They limit exposure of sensitive services. For example, a web server might only allow ports 80/443 while blocking everything else.
-
On Desktops/Laptops: They protect personal devices when connecting to untrusted Wi-Fi networks by silently dropping unsolicited connections.
-
On Routers/Gateways: They enforce boundaries between internal (trusted) and external (untrusted) networks.
To illustrate, let’s allow only web traffic (HTTP/HTTPS) and drop everything else with nftables:
sudo nft add rule inet filter input tcp dport {80,443} accept
sudo nft add rule inet filter input drop
With just two lines, the firewall ensures only web traffic is accepted. Any other incoming traffic is denied, even if a service is accidentally running.
Tools for Managing Linux Firewalls
There are three main tools to manage Linux firewalls you’ll encounter:
- iptables – The legacy firewall management tool, widely used but verbose.
- nftables – The modern replacement, with cleaner syntax and better scalability.
- firewalld – A higher-level abstraction that manages iptables or nftables under the hood, organizing rules into zones and services.
For example, using firewalld you could allow SSH like this:
sudo firewall-cmd --zone=public --add-service=ssh --permanent
sudo firewall-cmd --reload
This is much simpler than writing raw nftables rules, especially for beginners or in automated environments. Linux firewalls are packet filters at the heart of system security. They decide what comes in, what goes out, and what gets logged for later analysis. Whether managed with iptables, nftables, or firewalld, they play a critical role in protecting Linux systems across servers, desktops, and gateways.
The key takeaway: without a firewall, your Linux system is defenseless at the network layer. With one, you can enforce strict policies, minimize risks, and keep both personal and enterprise environments safe.
Why are Linux Firewalls Important?
Linux firewalls are a core component of system security. They enforce security policies by regulating inbound and outbound traffic, ensuring only authorized communication is allowed. By controlling how packets move through the system, firewalls minimize exposure to attacks, prevent unauthorized access, and contribute to compliance and layered defense strategies.
The Role of Firewalls in System Security
At the heart of Linux security, firewalls serve as gatekeepers. Every packet entering or leaving the system is evaluated against firewall rules. Without them, any open service—even one you forgot was running—could become a backdoor for attackers.
For example, consider a web server. While you need to allow HTTP and HTTPS traffic, you might want to block or restrict access to SSH. A firewall ensures these boundaries are respected, reducing the risk of unauthorized logins or data exfiltration.
# Allow only HTTP/HTTPS
sudo nft add rule inet filter input tcp dport {80,443} accept
# Drop everything else
sudo nft add rule inet filter input drop
With just two rules, you establish a baseline of “only what is needed”, which is a cornerstone of system security.
Regulating Inbound and Outbound Traffic
Linux firewalls operate bidirectionally:
-
Inbound filtering protects services by restricting who can connect and from where.
-
Outbound filtering prevents compromised systems from calling home or spreading malware.
For instance, you can block all outgoing SMTP traffic except from your mail server:
sudo iptables -A OUTPUT -p tcp --dport 25 -d 203.0.113.5 -j ACCEPT
sudo iptables -A OUTPUT -p tcp --dport 25 -j DROP
This ensures that only the designated mail server communicates on port 25, while other applications are denied.
Minimizing Exposure to Attacks
Most attacks begin with scanning. Bots and malicious actors continuously probe IP ranges, looking for open ports or misconfigured services. A properly configured firewall reduces the visible “surface area” of your system, so attackers have fewer opportunities. For example, brute-force SSH attempts are one of the most common attacks on internet-facing servers. A firewall can mitigate these risks by allowing SSH only from trusted subnets:
sudo nft add rule inet filter input ip saddr 192.0.2.0/24 tcp dport 22 accept
sudo nft add rule inet filter input tcp dport 22 drop
Now only machines in your office network can attempt SSH connections, blocking the rest of the internet.
Ensuring Compliance and Layered Defense
In enterprise environments, firewalls are also tied to compliance frameworks such as PCI-DSS (payments), HIPAA (healthcare), and GDPR (data protection). These standards mandate access control at the network layer, making firewalls not just good practice but a legal and regulatory requirement. Beyond compliance, firewalls are part of a layered defense strategy:
-
Application security defends against software flaws.
-
User access control limits what users can do.
-
Firewalls ensure that even if an attacker bypasses one layer, they still face barriers at the network level.
Example: Logging dropped packets for compliance auditing
sudo nft add rule inet filter input log prefix "NFT-DROP: " counter drop
This ensures that every dropped connection attempt is logged for review, providing evidence during audits.
Linux firewalls are not optional—they are critical guardians of system security. By regulating inbound and outbound traffic, they reduce exposure to attacks, enforce least-privilege principles, and provide essential visibility into network activity. In enterprise and regulated environments, they are also a key part of compliance and layered defense strategies. In short: Without a firewall, your Linux system is unnecessarily exposed; with one, you gain control, resilience, and trustworthiness.
What are iptables?
iptables is a tool that runs in user space on Linux systems that lets administrators set up and manage firewall rules. It is the command-line interface for the Netfilter framework in the Linux kernel. This lets you filter packets, regulate traffic, and do Network Address Translation (NAT).
Using iptables in User Space
The Linux kernel does the actual packet filtering, but administrators need a mechanism to tell the kernel what to do with each packet. That is what iptables does. It runs in user space and sends these instructions to Netfilter. You can think of iptables as the "language" you use to instruct the kernel what to do with network traffic.
For instance, to see your current firewall rules in the filter table, you would do this:
sudo iptables -L -v –n
This displays all active rules, showing which ports and IPs are allowed or denied, along with counters indicating how many packets matched each rule.
Using Netfilter with iptables
Netfilter is the Linux kernel framework that looks at and changes packets as they move through the networking stack. It has "hooks" that let you filter, NAT, or change packets. Administrators can use iptables to connect to these hooks and set rules like:
-
Allow packets on port 22 (SSH).
-
Drop packets from a suspicious IP address.
-
Translate (NAT) private IP addresses to a public IP.
Example- Allowing SSH traffic through iptables:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Here, iptables instructs Netfilter to accept any TCP packet destined for port 22.
Organizing Firewall Rules in a Structured Way
iptables organizes firewall rules in a structured way: tables → chains → rules.
-
Tables are categories of processing:
- filter: Default table, responsible for basic packet filtering.
- nat: Handles address translation (e.g., port forwarding, masquerading).
- mangle: Alters packet headers for advanced routing/QoS.
- raw: Provides connection tracking exemptions.
-
Chains are checkpoints where packets are examined:
- INPUT: Packets coming to the local system.
- OUTPUT: Packets generated by the system.
- FORWARD: Packets routed through the system.
-
Rules are the specific instructions (allow, drop, reject, log).
For example, to set a default policy and then allow SSH:
sudo iptables -P INPUT DROP
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
This configuration:
-
Drops all incoming traffic by default.
-
Allows responses to existing connections.
-
Allows new SSH connections on port 22.
Historical Role in Packet Filtering and NAT
iptables has been the main firewall tool for Linux for about 20 years, since it was first included to the Linux kernel in 2001. It took the place of the earlier ipchains tool and became known as the Linux firewall.
In the past, iptables was used for:
- Packet Filtering: Enforcing access control policies on incoming and outgoing traffic.
- NAT (Network Address Translation): Rewriting IP headers so that multiple devices could share one public IP.
- Masquerading: A special form of NAT, often used in home or office networks.
- Port Forwarding: Redirecting requests from one port/IP to another.
Example: Enabling NAT with iptables:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
This rule allows multiple devices on a private LAN to share a single public IP through interface eth0.
In short, iptables is not the firewall itself; it is a user-space utility. It is the interface to Netfilter, which is the real packet filtering engine in the Linux kernel. It groups rules into tables and chains, which lets you construct complicated firewall rules. For almost 20 years, iptables has been the main tool for packet filtering and NAT on Linux. It has formed the backbone of both small and large firewalls.
Even though iptables is now considered outdated, system administrators still need to know how to use it in order to keep systems running and comprehend the advancements made by its successor, nftables.
How do iptables Work?
Iptables operates by sending packets of data through tables of rules. It checks each packet in order, and when the conditions of a rule are met (such the protocol, port, or source IP), the action that was set up as the "target" is taken. These actions can either accept the packet, drop it without saying anything, reject it with an error, or log it for later review.
Chains: INPUT, OUTPUT, and FORWARD
iptables uses chains to arrange traffic filtering. Chains show the different stages of a packet's journey:
-
INPUT → For packets destined to the local machine (e.g., SSH access to a server).
-
OUTPUT → For packets originating from the local machine (e.g., a curl request to download updates).
-
FORWARD → For packets passing through the machine when it acts as a router.
Example: Listing the INPUT chain with verbose output:
sudo iptables -L INPUT -v -n
This shows all the rules applied to incoming traffic and how many packets matched each rule.
Sequential Rule Evaluation
iptables checks rules one at a time, in order. The first rule that matches decides what happens to the packet. The chain's default policy is used if no rules match.
For instance, let's let SSH go first and then get rid of everything else:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -j DROP
When a packet arrives on port 22 (SSH), the first rule matches, and the connection is allowed. All other packets fall to the second rule and are dropped.
Matching Conditions: Protocol, Port, IP
Rules are defined by conditions such as:
-
Protocol (-p tcp, -p udp, -p icmp)
-
Source/Destination IP (-s 192.0.2.0/24, -d 203.0.113.5)
-
Ports (--dport 80, --sport 53)
-
Connection state (-m conntrack --ctstate ESTABLISHED)
Example: Allow HTTP traffic only from a trusted subnet:
sudo iptables -A INPUT -p tcp -s 192.0.2.0/24 --dport 80 -j ACCEPT
This means: If the packet is TCP, comes from the subnet 192.0.2.0/24, and is destined for port 80 → accept it.
Common Actions (Targets): ACCEPT, DROP, REJECT, LOG
Once a rule matches, iptables applies a target:
-
ACCEPT → Let the packet through.
-
DROP → Silently discard it (no response to the sender).
-
REJECT → Deny the packet and send back an error (e.g., ICMP “port unreachable”).
-
LOG → Record the packet details in system logs, then continue to the next rule.
Example: Logging and dropping all other traffic except SSH:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -j LOG --log-prefix "IPTABLES-DROP: "
sudo iptables -A INPUT -j DROP
-
Rule 1 → Accept SSH connections.
-
Rule 2 → Log anything else with the prefix “IPTABLES-DROP.”
-
Rule 3 → Drop the packet.
iptables operates by sending packets through chains of rules that are checked one at a time. Conditions like protocol, IP, and port set rules, while actions like ACCEPT, DROP, REJECT, and LOG decide what happens to the packet. This model shows that iptables is a strong but often complicated firewall tool. To design a safe and working ruleset, the order and specificity of the rules are very important.
What is nftables?
Introduced in kernel version 3.13 (2014), nftables is the new firewall framework for Linux that is meant to replace iptables. It has a single interface for filtering packets, NAT, and classifying traffic over IPv4, IPv6, ARP, and bridging. All of this is done with a simpler syntax and better scalability.
nftables as the Modern Framework
For almost twenty years, iptables was the main part of Linux firewalls. But in big, changing contexts, it became evident that it was too complicated, duplicated, and slow. Linux developers came up with nftables to fix these problems. It is a simpler and more efficient approach to set up the Netfilter framework.
iptables needed several tools (iptables, ip6tables, arptables, ebtables), whereas nftables has a single command-line program that works with all of them:
sudo nft list ruleset
This one command shows all active rules across protocols, eliminating the fragmentation of the older tools.
Integration from Linux Kernel 3.13 Onward
Starting with version 3.13 (January 2014), nftables was added to the Linux kernel. Most major distributions (Debian, Ubuntu, RHEL, Fedora, Arch) have since adopted it as the default firewall backend. They commonly use iptables-nft compatibility layers to make sure older scripts work throughout the transition.
To see if your system supports nftables:
uname –r
You can use nftables if your kernel version is 3.13 or newer. Most contemporary distributions come with it already installed.
Unified Handling of IPv4, IPv6, ARP, and Bridging
One of nftables’ biggest advantages is its ability to handle multiple protocol families in one ruleset.
-
inet → IPv4 + IPv6
-
ip → IPv4 only
-
ip6 → IPv6 only
-
arp → ARP traffic
-
bridge → Bridged packets
For example, with iptables, you would need separate commands for IPv4 and IPv6 SSH rules. With nftables, one rule is enough:
nft add rule inet filter input tcp dport 22 accept
This single rule applies to both IPv4 and IPv6, greatly reducing duplication.
Simplified Syntax and Better Scalability
The syntax for nftables is meant to be clean, consistent, and easy to script. You may arrange IP addresses, ports, or ranges into sets and maps instead of typing complex commands over and again.
For example: Allow HTTP/HTTPS and SSH from a trusted subnet with only one rule:
table inet filter {
set allowed_ports {
type inet_service;
elements = { 22, 80, 443 }
}
chain input {
type filter hook input priority 0; policy drop;
ip saddr 192.0.2.0/24 tcp dport @allowed_ports accept
}
}
This concise syntax makes complex firewalls easier to read, maintain, and audit.
Scalability improvements include:
-
Atomic rule replacement: Entire rulesets are swapped in one operation, avoiding inconsistent states.
-
Efficient lookups: Sets and maps allow thousands of entries to be matched quickly.
-
Counters and monitoring: Built-in tools provide detailed visibility into rule hits and traffic statistics.
The Linux kernel has had nftables since version 3.13. It is a contemporary, unified firewall system. It makes managing firewalls easier by bringing together IPv4, IPv6, ARP, and bridging into one tool. This program has cleaner syntax and is much more scalable than iptables. Today, nftables is the best way to make firewalls on Linux systems that are efficient, easy to manage, and ready for the future.
Why is nftables Considered the iptables Replacement?
For almost 20 years, iptables was the most common way to set up a Linux firewall. But as systems got more complicated and networks got bigger, its flaws became obvious. To fix these issues, nftables was added to the Linux kernel 3.13 as a new way to do things. Because it makes management easier, speeds things up, and makes features work the same way across protocols, nftables is now the default firewall framework.
The Limitations of iptables
iptables served well for many years but suffers from several architectural issues:
-
Rule duplication: Separate binaries (iptables for IPv4, ip6tables for IPv6, arptables, and ebtables) meant administrators often had to duplicate the same logic across multiple tools.
-
Inefficiency: Rules are evaluated sequentially, so large rule sets significantly impact performance.
-
Complexity: The syntax is verbose, and even small misconfigurations can create major security holes.
-
Maintainability: Large iptables configurations are hard to audit, debug, and adapt to fast-changing environments like cloud or containers.
Example — allowing SSH for both IPv4 and IPv6 with iptables:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo ip6tables -A INPUT -p tcp --dport 22 -j ACCEPT
Here you must write separate rules for IPv4 and IPv6, doubling the maintenance effort which exhibits inefficiency and complexity.
Unified Rule Set with nftables
nftables was designed to overcome these limitations by introducing a unified ruleset. Instead of separate tools for each protocol, nftables handles IPv4, IPv6, ARP, and bridging under one umbrella.
Example — allowing SSH for both IPv4 and IPv6 with nftables:
nft add rule inet filter input tcp dport 22 accept
One simple rule replaces two or more iptables commands, immediately reducing duplication and configuration errors.
Performance and Maintainability Benefits
nftables brings multiple technical improvements:
-
Performance: Rules are evaluated more efficiently, and sets/maps allow bulk matching (e.g., thousands of IPs in one rule).
-
Atomic updates: You can replace the entire ruleset in one operation, avoiding half-applied configurations.
-
Cleaner syntax: Rules are shorter, more readable, and easier to maintain.
-
Debugging and visibility: Built-in counters and logging make it easier to monitor rule usage.
Example — allowing multiple ports in one rule with nftables:
nft add rule inet filter input tcp dport {22,80,443} accept
In iptables, you would need three separate rules for the same effect.
Adoption in Modern Linux Systems
Most modern distributions have made nftables the default firewall backend:
-
Debian switched to nftables by default from Buster (10).
-
Ubuntu defaults to nftables in newer releases, while still supporting iptables for compatibility.
-
RHEL 8 and Fedora also use nftables as the underlying framework, even when managed via firewalld.
This adoption means that nftables is no longer an optional upgrade; it is now the standard for Linux firewalls. As iptables is now officially in legacy maintenance mode, administrators are urged to move.
Because it fixes the problems of iptables, nftables is thought to be the best solution. nftables is a modern, scalable, and maintainable firewall foundation for today's systems. It combines IPv4, IPv6, ARP, and bridging rules, has a simpler syntax, improves speed using sets and atomic updates, and is the default across most Linux versions.
Does firewalld Use iptables or nftables?
Yes, firewalld does use iptables or nftables, but not both at the same time. It is a layer for managing firewalls that sits on top of the Linux kernel's Netfilter framework. It does not filter packets on its own. Instead, it turns easy-to-understand rules into the backend firewall engine that is available on the host.
firewalld as a Firewall Management Layer
firewalld was created to make firewall configuration easier. Instead of writing complex iptables or nftables commands, administrators can manage firewall policies with zones (levels of trust) and services (predefined port/protocol groups).
For example, opening SSH in the public zone is as simple as:
sudo firewall-cmd --zone=public --add-service=ssh --permanent
sudo firewall-cmd --reload
Behind the scenes, firewalld translates this request into iptables or nftables rules depending on which backend the system is used.
Iptables is used on relatively older Systems while nftables is used on newer ones.
-
Older Linux distributions (e.g., CentOS 7, RHEL 7, Ubuntu 18.04) used iptables as the backend for firewalld.
-
Modern distributions (e.g., Fedora 32+, RHEL 8+, Ubuntu 20.04 and later) switched to nftables as the backend, since iptables is now legacy.
This transition allows firewalld to continue working seamlessly, while benefiting from nftables’ unified rule set and improved scalability.
Example — opening HTTP in Fedora (uses nftables underneath):
sudo firewall-cmd --zone=public --add-service=http
The user only sees firewalld commands, but nftables rules are being applied in the background.
How to Check which Backend firewalld Uses
You can verify which backend your system is running.
First, check if firewalld is active:
sudo firewall-cmd --state
If it returns running, firewalld is managing your firewall.
To see the backend, inspect system logs:
sudo journalctl -u firewalld | grep "Using backend"
Typical outputs include:
-
Using iptables backend → firewalld is managing iptables.
-
Using nftables backend → firewalld is managing nftables.
One of firewalld's best features is that it immediately works with the system's default firewall engine. Administrators don't have to worry about whether the underlying framework is iptables or nftables; the commands are always the same.
This makes firewalld very useful in businesses where there are many different versions and distributions of Linux. It makes sure that the administration layer is always the same, even when the backends are different.
Firewalld doesn't filter packets on its own; it manages the Linux firewall through a backend. It used iptables on older systems and nftables on newer ones. You may check the backend by looking at system logs or using firewall-cmd. Firewalld automatically adjusts to whichever engine is running, making it easier to maintain firewalls and making them more consistent across all Linux distributions.
How does firewalld Work on Linux Systems?
firewalld is a daemon that automatically controls the Linux kernel's Netfilter firewall. This makes it easier and more flexible to set up network rules. With static tools like iptables or nftables, every change usually requires reloading the whole ruleset. But with firewalld, admins may alter policies on the fly without stopping active connections.
firewalld as a Dynamic Netfilter Manager
Firewalld is a background service (daemon) that turns easy-to-understand firewall ideas into Netfilter rules. It can handle both IPv4 and IPv6 traffic, and it uses either iptables or nftables as its backend, depending on the system.
You can see what it's doing right now with:
sudo firewall-cmd --state
If it returns running, the daemon is active and controlling the firewall.
Zones and Services
firewalld introduces the concepts of zones and services to simplify firewall management:
-
Zones: Predefined trust levels that describe how restrictive the firewall should be. Examples:
-
public → for untrusted networks like Wi-Fi hotspots.
-
home → for private, trusted environments.
-
dmz → for servers exposed to the internet.
-
-
Services: Preconfigured groups of rules representing common protocols and ports (e.g., SSH, HTTP, DNS).
For example, to allow HTTPS traffic in the public zone:
sudo firewall-cmd --zone=public --add-service=https --permanent
sudo firewall-cmd --reload
This immediately configures the firewall to accept HTTPS connections without needing to know the exact port (443/TCP).
Dynamic Rule Application Without Restarts
One of the best things about firewalld is that it can make adjustments on the fly. You can add or remove a rule right away, and the active ruleset will be updated. There is no need to restart the service or lose connections.
For example, opening SSH on the fly for testing:
sudo firewall-cmd --add-service=ssh
This applies instantly. If you are satisfied, you can make it permanent:
sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --reload
Integration with NetworkManager and systemd
firewalld integrates tightly with other core Linux services:
-
NetworkManager: firewalld can automatically switch firewall zones depending on the network connection. For example, when you connect your laptop to a home Wi-Fi, the interface may be assigned the home zone, but when you connect to public Wi-Fi, it switches to public automatically.
-
systemd: firewalld runs as a systemd service, making it easy to manage, enable, or disable at boot.
sudo systemctl status firewalld
sudo systemctl enable firewalld
sudo systemctl disable firewalld
This makes sure that the firewall stays on even after a reboot and works with other system services.
As a dynamic daemon on top of Netfilter, firewalld manages firewall rules in real time without stopping connections. It makes complicated settings easier to understand by grouping rules into services and zones. It works well with NetworkManager, so changes to zones follow network connections. Systemd makes sure that services start up and run smoothly.
In brief, firewalld makes Linux firewalls more flexible, adaptable, and easier to maintain, especially when network conditions change often.
How does iptables Filter Network Traffic?
When packets flow via the Linux kernel's Netfilter hooks, iptables checks them against a set of rules and then takes an action, like ACCEPT, DROP, or REJECT. Each packet's properties, such as its source IP, destination port, and protocol, are tested one by one against a set of rules in chains until a match is found or the chain's default policy chooses what to do.
Packet Inspection Process
The Netfilter framework stops every network packet that comes into or leaves a Linux system. Netfilter uses iptables to figure out what to do with these packets.
For instance, when a TCP packet comes in and is meant for port 22 (SSH), Netfilter checks it against the rules in the INPUT chain.
You may see all of the active packet-filtering rules by using:
sudo iptables -L -v -n
This shows the chains, their policies, and how many packets have matched each rule so far.
Condition Matching: IP, Port, Protocol
iptables rules specify conditions that packets must meet to trigger an action. Common conditions include:
-
Protocol: -p tcp, -p udp, -p icmp
-
Source/Destination IP: -s 192.0.2.0/24, -d 203.0.113.5
-
Port Numbers: --dport 80, --sport 53
Example: Allow HTTP requests only from a trusted subnet:
sudo iptables -A INPUT -p tcp -s 192.0.2.0/24 --dport 80 -j ACCEPT
This rule allows packets if they are TCP, come from the 192.0.2.0/24 subnet, and are destined for port 80.