LINUX Firewall (original) (raw)

Last Updated : 11 Jun, 2026

Linux Firewall is a security system that monitors and controls incoming and outgoing network traffic based on predefined rules. It protects your system from unauthorized access, malicious traffic, and potential attacks. The two most commonly used firewall tools in modern Linux distributions are firewalld (default in RHEL, CentOS, Fedora) and iptables (traditional, widely used).

Dynamic Firewall Management in Linux

firewalld is a dynamic firewall management tool in Linux that uses the concept of zones to define trust levels for network connections. Zones can represent different environments, such as public, home, work, or internal. Each zone has its own set of rules, and firewalld allows real-time changes without interrupting existing connections.

Checking firewalld Status

**Command:

sudo systemctl status firewalld

**If inactive need to start it using:

**Command:

sudo systemctl start firewalld.

**Output:

[command to check the running status of our Firewalld services.](command status firewalld)

Command to check the running status of our Firewalld services.

output showing services actively running

Here is the output showing services actively running

Example 1: Allowing SSH (Secure Shell or Secure Socket Shell) traffic

SSH (Secure Shell) is a protocol used to securely access and manage remote servers over an encrypted connection. Enabling SSH in the firewall ensures administrators can remotely log in to the system safely.

**Command:

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

**Output:

[As we can see it is done successfully ](allow ssh remote access)

As we can see it is done successfully

Example 2: Allowing incoming traffic on a specific port

Some applications run on non-standard ports instead of predefined services. Allowing a specific port enables external users to access such applications while keeping other ports restricted.

**Command:

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

**Output:

Both the command showed success

Both the command showed success

Example 3: Blocking incoming traffic on a specific IP address

Blocking a specific IP address helps prevent unauthorized access or mitigate malicious activity such as brute-force attacks. firewalld uses rich rules for advanced filtering based on IP, protocol, or other conditions.

**Command:

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

**Output:

we have also mentioned family of IP (ipv4

we have also mentioned family of IP (ipv4)

Example 4: Remove a Rule

Removing unnecessary rules keeps your firewall clean and reduces security risks. It also helps avoid conflicts if services change over time.

**Commands:

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

Example 5: View Active Rules

Verifying active firewall rules is crucial to ensure security policies are correctly applied. This command provides a summary of all ports, services, and rich rules in a specific zone.

**Command:

sudo firewall-cmd --list-all

Types of Linux Firewalls

Linux provides several firewall solutions, each with different levels of complexity and usability.

iptables Traditional Firewall

iptables is a Linux software utility used for controlling network traffic. It performs packet filtering, NAT (Network Address Translation), and packet manipulation, allowing administrators to define rules for incoming and outgoing traffic.

**Working of iptables

When a packet reaches a Linux system, it passes through a structured framework of tables, chains, and rules

**Predefined tables in iptables

Chains in iptables

chains define the stage at which packets are processed within a table. Each chain consists of a series of rules that determine how matching packets are handled. The three main built-in chains are INPUT, OUTPUT, and FORWARD, each serving a distinct purpose in packet filtering and traffic control.

**Note: Each chain uses rules specifying source/destination IP, protocol, and port, with actions like ACCEPT (allow), DROP (discard), or REJECT (block with notification).

Installation and Configuration of a Firewall

Before using iptables or switching firewall tools, ensure the required packages are installed and properly configured.

Install iptables

**For RHEL / CentOS / Fedora:

sudo dnf install iptables iptables-services

**For Ubuntu / Debian:

sudo apt install iptables

Start and Enable iptables Service (RHEL-based systems)

sudo systemctl start iptables
sudo systemctl enable iptables

Disable firewalld (If Using iptables)

Avoid conflicts between firewalld and iptables, as both manage firewall rules.

sudo systemctl stop firewalld
sudo systemctl disable firewalld

**Notes:

Basic Examples of iptables

Example 1: Allow ICMP (Ping) Traffic

Allow ICMP traffic so the server can respond to ping requests for connectivity testing and network diagnostics.

**Syntax:

sudo iptables -I/-A chain_name -s source_ip -p protocol --dport port_number -j action

**Example Command:

sudo iptables -A INPUT -p icmp -j ACCEPT

**Output:

[CREATE FIRST RULE iptables](create first rule iptables)

CREATE FIRST RULE iptables

Example 2: Allow SSH from a Specific IP

Restrict SSH access so that only a trusted IP address can connect to the server, improving security.

sudo iptables -A INPUT -s 192.168.160.51 -p tcp --dport 22 -j ACCEPT

**Output:

TO CHECK  OUTPUT WE USED $ sudo iptables -L

TO CHECK OUTPUT, WE USED $ sudo iptables -L

Example 3: Drop All Traffic from an IP

Block all incoming traffic from a specific IP address, typically used to stop malicious or unwanted access.

**Syntax:

sudo iptables -A chain_name -s source_ip -j action

**Command:

sudo iptables -A INPUT -s 192.168.52.1 -j DROP

**Output:

As we can see 192.168.160.51 has been drop

As we can see 192.168.160.51 has been drop

Example 4: List All Rules

Display all active firewall rules to verify configuration and monitor traffic handling.

**Command:

sudo iptables -L -v -n

Example 5: Flush (Reset) All Rules

Remove all existing firewall rules to reset the configuration, typically used during troubleshooting or reconfiguration.

**Command:

sudo iptables -F

**Output:

As we can see all the rules has been reset