Home Learn Linux iptables vs. firewalld: Choosing your Linux firewall solution

iptables vs. firewalld: Choosing your Linux firewall solution

This article compares iptables and firewalld, two key firewall management tools in Linux. Understand their unique features, ease of use, and management capabilities to make an informed decision on the best tool for securing and managing network traffic on your Linux system.

by Divya Kiran Kumar
iptables vs. firewalld

Welcome to another deep dive into the world of Linux administration! Today, we’re tackling a critical aspect of network security: firewall management. As a Linux administrator, I’ve navigated the complex waters of Linux firewalls, primarily focusing on two primary tools: iptables and firewalld. I’ll share my experiences, preferences, and some handy tips to help you manage your Linux firewall effectively.

Understanding the basics of Linux firewalls

Before we jump into iptables and firewalld, let’s set the stage. A firewall in Linux acts as a gatekeeper, controlling incoming and outgoing network traffic based on predefined security rules. It’s your first line of defense against unauthorized access and malicious attacks.

iptables: the traditional approach

iptables has been the backbone of Linux firewall management for years. It’s known for its robustness and flexibility but can be quite complex for beginners.

How iptables works

iptables uses tables, chains, and rules to filter network traffic. The tables categorize the nature of the rules, while chains define when these rules are applied.

Key tables in iptables

iptables uses several tables, each designed for a specific type of packet processing. The most commonly used tables are:

  1. Filter table:
    • Purpose: The default and perhaps the most important table in iptables. It’s used for allowing or denying packets.
    • Chains: It contains three chains:
      • INPUT: Handles incoming packets destined for the host.
      • FORWARD: Manages packets being routed through the host.
      • OUTPUT: Deals with packets originating from the host itself.
  2. NAT table:
    • Purpose: Used for Network Address Translation (NAT), crucial in modifying source or destination addresses of packets, often used for routing or port forwarding.
    • Chains:
      • PREROUTING: Alters packets as soon as they come in.
      • POSTROUTING: Changes packets after they have been routed.
      • OUTPUT: Used for NAT of locally generated packets on the host.
  3. Mangle table:
    • Purpose: This is used for specialized packet alteration.
    • Chains: It has the same chains as the Filter table (INPUT, FORWARD, OUTPUT) and also PREROUTING and POSTROUTING. It allows for altering packet headers.
  4. Raw table:
    • Purpose: Used primarily for configuring exemptions from connection tracking.
    • Chains: Mainly uses the PREROUTING chain to set marks on packets for processing in other tables.
  5. Security table:
    • Purpose: Used for Mandatory Access Control networking rules, such as those used by SELinux.
    • Chains: It follows the standard chains but is less commonly used in everyday iptables configurations.

Chains in iptables

Chains are predefined points in the network stack where packets can be evaluated against the rules in a table. The main chains are:

  1. INPUT chain:
    • Function: Controls the behavior of incoming connections. If a packet is destined for the local system, it will be processed through this chain.
  2. FORWARD chain:
    • Function: Handles packets that are not meant for the local system but need to be routed through it. This is essential for machines acting as routers.
  3. OUTPUT chain:
    • Function: Manages packets that are generated by the local system and are going out to the network.

Each of these chains can contain multiple rules, and these rules dictate what happens to network packets at each point. For instance, in the Filter table’s INPUT chain, you can have rules that drop packets from suspicious sources, or in the FORWARD chain, you might have rules that decide which packets can be routed through your system.

Basic iptables syntax

The general syntax for iptables is:

iptables [-t table] -[A/I/D] chain rule-specification [j target]
  • -t table specifies the table (filter, nat, mangle).
  • -A/I/D adds, inserts, or deletes a rule.
  • chain is the chain (INPUT, FORWARD, OUTPUT) where the rule is placed.
  • rule-specification defines the conditions for the rule.
  • -j target specifies the target action (ACCEPT, DROP, REJECT).

Let’s dive into some examples to deepen your understanding of iptables. We’ll explore various scenarios, illustrating how iptables rules are crafted and applied.

Example 1: Allowing SSH Access

Suppose you want to allow SSH access (typically on port 22) to your server from a specific IP address.

Command:

iptables -A INPUT -p tcp --dport 22 -s 192.168.1.50 -j ACCEPT

Explanation:

  • -A INPUT: Appends a rule to the INPUT chain.
  • -p tcp: Specifies the protocol, in this case, TCP.
  • --dport 22: Indicates the destination port, which is 22 for SSH.
  • -s 192.168.1.50: Allows only the IP address 192.168.1.50.
  • -j ACCEPT: The target action, which is to accept the packet.

Example 2: Blocking a Specific IP Address

If you need to block all traffic from an offending IP address, say 10.10.10.10, you can use iptables to drop all packets from that source.

Command:

iptables -A INPUT -s 10.10.10.10 -j DROP

Explanation:

  • -A INPUT: Appends the rule to the INPUT chain.
  • -s 10.10.10.10: Specifies the source IP address to match.
  • -j DROP: Drops the packet, effectively blocking the source IP.

Example 3: Port Forwarding

Port forwarding is a common task, especially in server environments. Let’s say you want to forward HTTP traffic (port 80) to a different port, say 8080.

Command:

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

Explanation:

  • -t nat: Specifies the NAT table.
  • -A PREROUTING: Appends the rule to the PREROUTING chain for altering packets as soon as they come in.
  • -p tcp: Indicates the TCP protocol.
  • --dport 80: Matches packets intended for port 80.
  • -j REDIRECT: Redirects the packet.
  • --to-port 8080: The new destination port for the packet.

Example 4: Limiting Connections Per IP

To prevent potential denial-of-service attacks, you might want to limit the number of concurrent connections per IP.

Command:

iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j DROP

Explanation:

  • -A INPUT: Appends this rule to the INPUT chain.
  • -p tcp --syn: Matches the initial packet (SYN) of a TCP connection.
  • --dport 80: Specifies the destination port (HTTP in this case).
  • -m connlimit: Uses the connection limit matching extension.
  • --connlimit-above 20: Sets the connection limit per IP address.
  • -j DROP: Drops packets that exceed the limit.

Example 5: Logging Dropped Packets

For diagnostic purposes, it’s often useful to log packets that are dropped.

Command:

iptables -A INPUT -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
iptables -A INPUT -j DROP

Explanation:

  • -A INPUT: Appends this rule to the INPUT chain.
  • -j LOG: Enables logging.
  • --log-prefix "IPTables-Dropped: ": Adds a prefix to log messages for easy identification.
  • --log-level 4: Sets the log level (4 corresponds to warning).
  • The second command then drops packets after logging.

Personal take: iptables

I appreciate iptables for its raw power and precision. However, its complexity and the need for manual rule management can be daunting for beginners.

firewalld: the modern solution

firewalld represents a modern approach to managing Linux firewalls, emphasizing simplicity and user-friendliness while still offering robust capabilities. It was introduced to address some of the complexities and challenges associated with iptables, especially for those who may not be deeply versed in network administration.

The philosophy and design of firewalld

firewalld is built around the concept of ‘zones’ and ‘services’, which abstract the traditional approach of iptables into more manageable components. This design is particularly beneficial in dynamic environments where network interfaces and conditions frequently change.

  1. Zones: These are predefined or user-defined labels that represent the level of trust for network connections and devices. For example, a ‘public’ zone can be less trusted, allowing limited access, whereas a ‘home’ or ‘internal’ zone might allow more access. This zoning concept simplifies the management of different network environments and policies.
  2. Services: Instead of managing individual ports and protocols, firewalld allows administrators to manage groups of ports and protocols as a single entity, referred to as a service. This approach makes it easier to enable or disable access for complex applications without having to remember specific port numbers.
  3. Dynamic Management: One of the standout features of firewalld is its ability to apply changes without needing a restart. This dynamic nature allows administrators to modify firewall settings on the fly, which is a significant improvement over iptables, where changes typically require a reload of the entire firewall or flushing of existing rules.
  4. Rich Language and Direct Interface: firewalld offers a ‘rich language’ for more complex rules, providing greater flexibility. Additionally, it provides a ‘direct interface’ for compatibility, allowing it to use iptables rules directly, which is particularly useful for users transitioning from iptables or with specific iptables rules they need to maintain.
  5. Integration with Other Tools: firewalld is well-integrated with other system management tools and interfaces, such as NetworkManager, which makes it a more seamless part of the overall system security architecture.

In practice

For system administrators, especially those in dynamic network environments or those who prefer a more straightforward configuration approach, firewalld offers a compelling option. It strikes a balance between flexibility and ease of use, catering to both novice users and experienced professionals who need a quick and efficient way to manage firewall rules. The ability to dynamically apply changes and the intuitive management of zones and services make firewalld a strong contender in the realm of Linux firewall management.

How firewalld works

firewalld operates on zones and services, simplifying the management process. Zones define the trust level of network connections, and services represent the network services allowed through the firewall.

firewalld syntax and commands

firewalld uses firewall-cmd for its operations. The basic syntax is:

firewall-cmd [options] <command>

Let’s explore some practical examples of using firewalld, showcasing its functionality and ease of use. These examples will help illustrate how firewalld manages network traffic using zones and services, offering a user-friendly approach to firewall management in Linux.

Example 1: Adding a service to a zone

Suppose you want to allow HTTP traffic on your server. You can easily do this by adding the HTTP service to a zone, such as the default zone.

Command:

firewall-cmd --zone=public --add-service=http --permanent

Explanation:

  • --zone=public: Specifies the zone to which you’re adding the rule, in this case, the ‘public’ zone.
  • --add-service=http: Adds the HTTP service, which by default corresponds to port 80.
  • --permanent: Makes the rule permanent across reboots. Without this, the rule would be temporary.

Example 2: Opening a specific port

If you need to open a specific port, like port 8080, you can add a port rule directly to a zone.

Command:

firewall-cmd --zone=public --add-port=8080/tcp --permanent

Explanation:

  • --add-port=8080/tcp: Opens TCP port 8080.
  • The other options are the same as in the previous example.

Example 3: Removing a service from a zone

To remove a service from a zone, such as disabling SSH access, use the following command.

Command:

firewall-cmd --zone=public --remove-service=ssh --permanent

Explanation:

  • --remove-service=ssh: Removes the SSH service from the specified zone, thereby blocking SSH access.

Example 4: Listing active rules

To view the active rules in a specific zone, you can list the services and ports enabled.

Command:

firewall-cmd --zone=public --list-all

Explanation:

  • --list-all: Lists all settings including services and ports for the ‘public’ zone.

Example 5: Blocking an IP address

To block a specific IP address, you can add a rich rule to a zone.

Command:

firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="10.10.10.10" reject' --permanent

Explanation:

  • --add-rich-rule: Adds a more complex rule, known as a rich rule.
  • rule family="ipv4": Specifies that this rule applies to IPv4 addresses.
  • source address="10.10.10.10": The IP address to block.
  • reject: The action to take, in this case, rejecting the packets.

Example 6: Enabling Masquerading

Masquerading (a form of NAT) is useful, for example, in a scenario where your machine is acting as a gateway.

Command:

firewall-cmd --zone=public --add-masquerade --permanent

Explanation:

  • --add-masquerade: Enables masquerading in the specified zone, allowing your system to translate the addresses of network packets.

Personal take: firewalld

firewalld’s zone-based approach and simpler syntax make it more accessible, especially for those new to firewall management. Its dynamic nature, not requiring a restart for changes to take effect, is a significant plus.

iptables vs. firewalld: a comparative look

Let’s compare iptables and firewalld in various aspects:

1. Ease of use and user-friendliness

  • iptables: It’s a powerful tool with a steep learning curve. iptables requires an understanding of detailed network protocols and complex command syntax. It’s less forgiving of errors, making it daunting for beginners but a favorite for experienced users who desire fine-grained control.
  • firewalld: Designed with user-friendliness in mind, firewalld abstracts complex configurations into more manageable elements like zones and services. Its commands are more intuitive, making it accessible to users with various skill levels. The graphical interface available for firewalld further enhances its appeal for those who prefer GUI over command-line interaction.

2. Flexibility and granular control

  • iptables: Offers unmatched granularity. You can define rules that can manipulate almost every aspect of network packets, allowing for intricate configurations tailored to very specific needs.
  • firewalld: While it provides sufficient flexibility for most standard use cases, it abstracts and simplifies certain complexities. This design choice makes it less daunting but also less granular compared to iptables.

3. Performance and resource utilization

  • iptables: It operates directly with netfilter, the Linux kernel’s packet filtering framework, which can translate to marginally better performance, especially in high-throughput scenarios.
  • firewalld: The performance difference for typical use cases is negligible, but it may fall slightly behind iptables in extremely high-demand environments due to its additional abstraction layer.

4. Statefulness and dynamic management

  • iptables: Traditionally seen as less dynamic, requiring manual rule reloading to apply changes. However, iptables can be used in stateful configurations, allowing for complex rule sets based on the state of network connections.
  • firewalld: It shines with its dynamic handling of rules. Changes can be made on the fly without the need for a complete firewall restart, which is crucial for maintaining connections in dynamic network environments.

5. Integration and forward compatibility

  • iptables: Universally supported and deeply integrated into many Linux systems, it’s the go-to choice for legacy systems and those who require scripts and tools built around iptables.
  • firewalld: Offers better integration with modern Linux distributions and features like NetworkManager. It’s more future-proof, considering the evolving nature of network management in Linux environments.

6. Specific use cases and scenarios

  • iptables: Ideal for complex network environments, like custom-configured servers or specialized network gateways where precise control over every packet is necessary.
  • firewalld: More suited for standard server setups, desktops, and users needing a balance between functionality and ease of use. It’s also preferable in environments where changes to firewall settings are frequent and need to be applied without downtime.

7. Learning curve and community support

  • iptables: Has a vast amount of documentation and community support, given its long history. However, the learning curve is significant, requiring more time and effort to master.
  • firewalld: Easier for beginners to pick up, with growing community support and documentation. It’s becoming more prevalent in modern Linux distributions, which aids in fostering a supportive user base.

This table provides a straightforward comparison, making it easier for you to understand the key differences and make an informed decision based on their specific requirements and preferences.

Comparing iptables and firewalld: Key differences at a glance

iptables firewalld
Complex syntax, steep learning curve User-friendly, easier syntax
Highly flexible, granular control Less flexible but more straightforward
Direct interaction with kernel netfilter, slightly faster Indirect interaction, marginally slower
Requires manual rule reloading for changes Dynamic, changes applied without restart
Universally available on older and newer distributions Mainly available on newer distributions
Ideal for seasoned administrators needing precise control Suited for quick setups and less complex environments
Command-line based, scriptable Command-line with GUI options, zone-based
Extensive community support and documentation Growing support, more aligned with modern Linux features
Better for complex, custom network configurations Better for standard server setups and desktops
Less future-proof, but universally supported More future-proof, aligns with modern Linux features

Conclusion

The choice between iptables and firewalld hinges on specific needs, technical expertise, and the nature of the environment they are to be implemented in. iptables stands out for its precision and granular control, making it a preferred choice for seasoned administrators who need detailed management of complex network configurations. On the other hand, firewalld offers a more streamlined, user-friendly approach, with dynamic rule management and a simpler syntax, making it suitable for those who seek ease of use or manage less complex environments. While iptables excels in environments where stability and detailed packet control are paramount, firewalld aligns better with modern Linux distributions and scenarios requiring frequent, hassle-free updates. Ultimately, the decision should align with the user’s comfort level, the specific requirements of the network infrastructure, and the desired balance between complexity and convenience.

You may also like

1 comment

grokker November 26, 2023 - 12:35 AM

This should at least include ufw. Or just firewalld vs ufw. Why would anyone be using iptables (or more recently nftables) directly?

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.