Setting up a firewall in Linux using either firewalld
or iptables
involves different approaches due to their architectural differences. firewalld
is a dynamic firewall daemon that provides a user-friendly interface for managing firewall rules, while iptables
is a command-line utility that directly manipulates the kernel's netfilter framework.
Using firewalld:
firewalld
is installed. On most distributions, this is done using the package manager (e.g., apt install firewalld
on Debian/Ubuntu, dnf install firewalld
on Fedora/CentOS/RHEL).systemctl start firewalld
and enable it to start on boot with systemctl enable firewalld
.firewalld
uses "zones" to define different network contexts (e.g., "public", "internal", "dmz"). Each zone has a default set of rules. You can list zones with firewall-cmd --get-active-zones
. To add a service, like SSH (port 22), to the default zone (usually "public"), use firewall-cmd --permanent --add-service=ssh
. To make the changes permanent, use the --permanent
flag. Reload the firewall with firewall-cmd --reload
to apply the changes.firewall-cmd --permanent --add-port=80/tcp
(for HTTP) or ranges using firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'
(for allowing traffic from a specific subnet).Using iptables:
iptables
is usually included by default in most Linux distributions.iptables
uses chains (e.g., INPUT
, OUTPUT
, FORWARD
) to manage rules. Each rule specifies the source/destination IP addresses, ports, protocols, and action (ACCEPT, DROP, REJECT). For example, to allow SSH connections: iptables -A INPUT -p tcp --dport 22 -j ACCEPT
.iptables
rules are not persistent across reboots. You need to save them using a script or a utility like iptables-save
and load them at boot time using a startup script. The exact method varies depending on your distribution.iptables
offers extremely fine-grained control, allowing for complex rule sets with various matching criteria and custom chains. However, this requires a deep understanding of networking and iptables
syntax.The primary difference lies in their approach to firewall management. firewalld
provides a higher-level, user-friendly interface built on top of iptables
. It simplifies common firewall tasks, making it easier to manage zones, services, and ports. iptables
, on the other hand, provides direct, low-level control over the netfilter framework, offering greater flexibility but requiring more technical expertise.
Here's a table summarizing the key differences:
Feature | firewalld | iptables |
---|---|---|
Interface | Command-line tool with user-friendly options | Command-line only, complex syntax |
Configuration | Zones, services, ports, rich rules | Chains, rules with specific matching criteria |
Persistence | Built-in persistence mechanism | Requires manual saving and loading at boot |
Complexity | Easier to learn and use | Steeper learning curve, more complex |
Flexibility | Less flexible than iptables | Highly flexible, allows for intricate rules |
Dynamic Updates | Supports dynamic updates | Manual updates required |
Using firewalld:
To allow a specific port (e.g., HTTP on port 80):
firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
To deny a specific port (e.g., FTP on port 21):
This is less straightforward with firewalld
. You'd likely need to create a custom zone or use rich rules to achieve this precisely. Generally, firewalld
is designed to allow by default and deny explicitly.
To allow a specific service (e.g., SSH):
firewall-cmd --permanent --add-service=ssh firewall-cmd --reload
Using iptables:
To allow a specific port (e.g., HTTP on port 80):
iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT # If you want to allow outgoing traffic on port 80 as well. service iptables save # Save the rules (method varies by distribution)
To deny a specific port (e.g., FTP on port 21):
iptables -A INPUT -p tcp --dport 21 -j DROP service iptables save # Save the rules (method varies by distribution)
Regardless of whether you use firewalld
or iptables
, follow these best practices:
INPUT
chain, as this controls incoming connections.firewalld
and iptables
support this) to track connections and allow return traffic.Remember to always test your firewall rules in a controlled environment before deploying them to a production system. Incorrectly configured firewall rules can render your system inaccessible.
The above is the detailed content of How do I set up a firewall in Linux using firewalld or iptables?. For more information, please follow other related articles on the PHP Chinese website!