April 25, 2025

How to Configure Firewall in Linux: Step-by-Step Guide

A firewall is an essential security feature that controls network traffic to and from your system. In Linux, firewall configurations can be managed using different tools like iptables, firewalld, and UFW (Uncomplicated Firewall).

In this guide, we will cover step-by-step firewall configuration in Linux using firewalld (for RHEL-based systems) and UFW (for Ubuntu/Debian-based systems).

1. Checking Firewall Status

Before configuring the firewall, check whether it is active on your system.

For firewalld (RHEL, CentOS, Fedora):

sudo systemctl status firewalld


For UFW (Ubuntu, Debian):

sudo ufw status

If the firewall is not running, enable it with:

sudo systemctl start firewalld   # For firewalld
sudo ufw enable                  # For UFW


2. Allowing or Blocking Services

Using Firewalld (RHEL, CentOS, Fedora)

To allow a service, use:

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload


To deny a service, use:

sudo firewall-cmd --remove-service=ftp --permanent
sudo firewall-cmd --reload

Using UFW (Ubuntu, Debian)

To allow traffic on a port, use:

sudo ufw allow 80/tcp   # Allow HTTP traffic
sudo ufw allow 443/tcp  # Allow HTTPS traffic


To deny a service, use:

sudo ufw deny 21/tcp   # Block FTP


3. Allowing or Blocking Specific IP Addresses

To allow traffic from a specific IP:

  • Firewalld:
  sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
  sudo firewall-cmd --reload
  • UFW:
  sudo ufw allow from 192.168.1.100

To block a specific IP:

  • Firewalld:
  sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.200" drop'
  sudo firewall-cmd --reload
  • UFW:
  sudo ufw deny from 192.168.1.200


4. Allowing or Blocking Specific Ports

To allow a port:

  • Firewalld:
  sudo firewall-cmd --permanent --add-port=8080/tcp
  sudo firewall-cmd --reload
  • UFW:
  sudo ufw allow 8080/tcp

To block a port:

  • Firewalld:
  sudo firewall-cmd --permanent --remove-port=8080/tcp
  sudo firewall-cmd --reload
  • UFW:
  sudo ufw deny 8080/tcp


5. Listing Firewall Rules

To view current firewall rules, use:

  • Firewalld:
  sudo firewall-cmd --list-all
  • UFW:
  sudo ufw status numbered


6. Deleting Firewall Rules

To remove a specific service or port:

  • Firewalld:
  sudo firewall-cmd --permanent --remove-service=http
  sudo firewall-cmd --permanent --remove-port=8080/tcp
  sudo firewall-cmd --reload
  • UFW:
  sudo ufw delete allow 80/tcp
  sudo ufw delete allow 8080/tcp

To remove a specific IP rule:

  • Firewalld:
  sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
  sudo firewall-cmd --reload
  • UFW:
  sudo ufw delete allow from 192.168.1.100


7. Resetting Firewall Rules

If you want to reset all firewall rules:

  • Firewalld:
  sudo firewall-cmd --complete-reload
  • UFW:
  sudo ufw reset


8. Disabling the Firewall (If Necessary)

To temporarily disable the firewall:

  • Firewalld:
  sudo systemctl stop firewalld
  • UFW:
  sudo ufw disable

To permanently disable it on system startup:

  • Firewalld:
  sudo systemctl disable firewalld
  • UFW:
  sudo systemctl disable ufw


Why Choose Pheonix Solutions?
At Pheonix Solutions, we provide expert firewall configuration and managed security solutions to keep your Linux servers protected. Here’s why businesses trust us:

  1. Expert Support – Our Linux specialists configure and optimize firewalls for maximum security.
  2. Customized Security Solutions – Tailored firewall rules based on your business needs.
  3. Optimized Performance – We ensure security without compromising speed or uptime.
  4. Proactive Threat Management – Detecting and mitigating risks before they become a problem.
  5. Reliable Assistance – Our team is available to help you with firewall configurations and troubleshooting.

Secure your Linux infrastructure with Pheonix Solutions and stay protected against cyber threats!

Conclusion

Configuring a firewall in Linux is essential for securing your server from unauthorized access and cyber threats. By following this guide, you can allow or block services, manage specific IP addresses, and secure your system efficiently.

Whether you’re using Firewalld (for RHEL-based systems) or UFW (for Ubuntu/Debian-based systems), proper firewall management is key to enhancing security and performance.

Leave a Reply

Your email address will not be published. Required fields are marked *