Linux Tips guides, explanations and reader checklists on whilenetworking.com

How to Enable and Configure Firewall Rules Using UFW or firewalld

Firewalls are the primary line of defense for any Linux server, acting as a digital bouncer that decides whether incoming network traffic is allowed or…

Firewalls are the primary line of defense for any Linux server, acting as a digital bouncer that decides whether incoming network traffic is allowed or blocked. In modern Linux distributions, two dominant tools handle this responsibility: ufw (Uncomplicated Firewall) and firewalld. While both achieve the same goal, they operate differently depending on your distribution; Ubuntu typically uses UFW, whereas RHEL, Fedora, and CentOS rely on firewalld. Understanding how to configure these tools is essential for securing web servers, databases, or any headless machine exposed to the internet. This guide provides a practical, step-by-step approach to enabling and configuring firewall rules safely for both systems.

Why You Need a Linux Firewall Before Starting

Before diving into commands, it is crucial to understand the default state of most fresh Linux installations. By default, many distributions have their firewalls disabled or set to “allow all” to facilitate easy setup during testing. However, leaving a server open is a significant security risk. Attackers scan for open ports every second; if your firewall is off, they can access SSH (port 22), web services (ports 80/443), or database interfaces directly. Enabling the firewall restricts traffic to only what you explicitly allow, following the principle of least privilege.

Configuring UFW on Ubuntu and Debian Systems

UFW is renowned for its simplicity and user-friendly interface, making it the default choice for Ubuntu and Debian users. Its syntax is straightforward, avoiding complex command-line parsing issues common with older iptables commands.

Installing and Enabling UFW

If you are on a fresh Ubuntu installation, UFW might already be installed. You can verify this by running ufw status verbose. If the tool is missing, install it using your package manager:

sudo apt update
sudo apt install ufw

Once installed, enable the firewall immediately. Note that enabling the firewall does not block existing connections; it only affects new incoming traffic.

sudo ufw enable

You will likely be prompted to confirm with y. To ensure it stays enabled after a reboot, add it to your startup applications or run ufw --force enable if you encounter issues where the setting doesn’t persist.

Setting Default Policies and Allowing Specific Ports

The most effective security strategy is to set the default policy to “deny” for incoming connections and “allow” for outgoing connections. This ensures that any port not explicitly opened remains closed.

sudo ufw default deny incoming
sudo ufw default allow outgoing

Next, you must open the specific ports your services require. For example, to allow SSH (critical for remote management) and HTTP/HTTPS for a web server:

sudo ufw allow 22/tcp
sudo ufw allow 'Apache Full'

UFW also supports allowing traffic by IP address or range, which is useful if you are testing from a specific location. However, be cautious with this; opening rules for specific IPs can be bypassed if that IP changes.

Managing UFW Rules and Status

To view your current configuration, use sudo ufw status verbose. This output shows the default policies, active rules, and the overall firewall status. If you need to remove a rule, use sudo ufw delete allow 22/tcp followed by confirming with y. You can also reset UFW entirely if you make a mistake, though this should be done only after ensuring no critical services are affected.

Managing Firewalld on RHEL, Fedora, and CentOS

For Red Hat Enterprise Linux (RHEL), Fedora, and CentOS, the standard tool is firewalld. It uses a dynamic zone-based approach, which allows for more granular control over network segments. Unlike UFW’s flat list of rules, firewalld organizes traffic into zones (e.g., public, internal, home) based on trust levels.

Starting and Enabling Firewalld

First, ensure the service is active and set to start on boot:

sudo systemctl start firewalld
sudo systemctl enable firewalld

Check the status with sudo systemctl status firewalld. If it is not installed (rare on RHEL/Fedora base systems), you may need to install it via your package manager.

Working with Zones and Services

The core workflow in firewalld involves adding services or ports to specific zones. The default zone is usually public, which is restrictive by design. To allow SSH access:

sudo firewall-cmd --zone=public --add-service=ssh --permanent
sudo firewall-cmd --reload

The --permanent flag saves the rule, while --reload applies it immediately. You can also open ports directly if a service name isn’t available:

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

Firewalld also supports dynamic modules like rich rules, which allow for advanced logic, such as allowing traffic only from specific IP ranges or limiting the number of connections per second.

Checking Status and Removing Rules

To view active zones and their associated services, run sudo firewall-cmd --get-services. To list all configured ports, use sudo firewall-cmd --list-ports. If you need to remove a rule, use sudo firewall-cmd --zone=public --remove-service=ssh --permanent followed by a reload. Always remember that changes made without the --permanent flag are temporary and will reset after a restart.

Real-World Example: Securing a Web Server Setup

Imagine you have just deployed a new web application on an Ubuntu server that needs to be accessible from the internet, while your internal network remains secure. Here is a concise checklist for this scenario using UFW:

  1. Audit Current State: Run sudo ufw status to ensure no dangerous ports (like 23 or 3389) are open.
  2. Set Defaults: Ensure incoming traffic defaults to deny and outgoing to allow.
  3. Open Essentials: Allow SSH (port 22), HTTP (port 80), and HTTPS (port 443). If you use Nginx, use ufw allow 'Nginx Full' instead of the raw port number for clarity.
  4. Restrict Admin Access: Consider adding a rule to only allow SSH from your office IP address: sudo ufw allow from <YOUR_OFFICE_IP> to any port 22.
  5. Log Connections: Run sudo ufw logging on to start recording blocked attempts, which is invaluable for security auditing.
  6. Verify: Test connectivity from another machine to ensure your web server is reachable but other ports remain closed.

Common Mistakes and Security Considerations

When configuring firewalls, several pitfalls can compromise your system. A frequent mistake is forgetting the --permanent flag in firewalld or failing to reload the daemon after adding rules, leaving the new configuration inactive. Another common error is opening the root user for SSH; always configure your SSH daemon to use a non-root account (like ubuntu or ec2-user) before opening port 22, then restrict access to that specific user via firewall rich rules if supported.

Additionally, never disable the firewall entirely just because you have added specific allow rules. If you accidentally delete all rules and revert the default policy to “allow,” your server becomes vulnerable instantly. Always double-check your ufw status or firewall-cmd --list-all output before rebooting a production server.

Finally, remember that firewalls are not a silver bullet. They filter network traffic but do not protect against malware already inside the system or misconfigured applications. Combine firewall rules with regular system updates, intrusion detection systems, and secure coding practices for comprehensive security.

Conclusion

Configuring a firewall is a fundamental skill for any Linux administrator. Whether you choose UFW for its simplicity on Ubuntu/Debian or firewalld for its dynamic zones on RHEL/Fedora, the core principle remains the same: deny by default and allow only what is necessary. By following the steps outlined above, auditing your rules regularly, and avoiding common configuration traps, you can significantly harden your Linux infrastructure against unauthorized access.