Home > Operation and Maintenance > Linux Operation and Maintenance > How do I set up a firewall in Linux using firewalld or iptables?

How do I set up a firewall in Linux using firewalld or iptables?

Emily Anne Brown
Release: 2025-03-12 18:58:51
Original
921 people have browsed it

Setting up a Firewall in Linux using firewalld or iptables

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:

  1. Installation: Ensure 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).
  2. Start and enable firewalld: Start the service with systemctl start firewalld and enable it to start on boot with systemctl enable firewalld.
  3. Basic Configuration: 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.
  4. Advanced Configuration: For more granular control, you can add specific ports using 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:

  1. Installation: iptables is usually included by default in most Linux distributions.
  2. Basic Configuration: 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.
  3. Saving rules: 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.
  4. Advanced Configuration: 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.

Key Differences Between firewalld and iptables

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

Configuring Specific Firewall Rules to Allow/Deny Particular Ports or Services

Using firewalld:

To allow a specific port (e.g., HTTP on port 80):

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
Copy after login

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
Copy after login

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)
Copy after login

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)
Copy after login

Best Practices for Securing Your Linux System with a Firewall

Regardless of whether you use firewalld or iptables, follow these best practices:

  • Principle of Least Privilege: Only allow necessary traffic. Deny all by default and explicitly allow specific ports and services.
  • Regular Updates: Keep your firewall and operating system updated with the latest security patches.
  • Log Analysis: Regularly review firewall logs to identify suspicious activity.
  • Input Chain Focus: Pay close attention to the INPUT chain, as this controls incoming connections.
  • Statefull Firewalls: Utilize stateful inspection (both firewalld and iptables support this) to track connections and allow return traffic.
  • Avoid Open Ports Unless Necessary: Minimize the number of open ports exposed to the internet.
  • Use a Strong Password Policy: Secure your system by using strong passwords and regularly updating them.
  • Regularly Review Rules: Periodically review your firewall rules to ensure they are still appropriate and effective.
  • Use a separate DMZ: If you need to expose services to the internet, consider using a separate DMZ (demilitarized zone) to isolate those services from your internal network.
  • Consider Intrusion Detection/Prevention Systems (IDS/IPS): Combine your firewall with an IDS/IPS for an added layer of security.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template