close
The Wayback Machine - https://web.archive.org/web/20241006020910/https://www.geeksforgeeks.org/linux-firewall/
Open In App

LINUX Firewall

Last Updated : 05 Jul, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Linux OS, we must know that it is a system based on Unix-like. Systems that are called Unix-like or Unix-based are designed to behave and function similarly to the Unix OS. Linux OS comes under the open-source family (code designed to be publicly accessible) and is based on the Linux kernel. It was initially released on September 17, 1991, by Linus Torvalds.

It has a modular design, which helps us with system customization according to our needs. Linux is open-source software that has helped in the growth and development of our developer community which is contributing to the open-source community. It also offers a command line interface for interacting with OS and also has a graphical interface for users who are not used to working with the command line interface.

What is Linux Firewall (firewalld)

A virtual wall in the security system world is designed to protect our system from unwanted traffic and unauthorized access to our system. The security system in Linux OS is known as Linux Firewall, which monitors and governs the network traffic (outbound/inbound connections). It can be used to block access to different IP addresses, Specific subnets, ports (virtual points where network connections begin and end), and services. We have a daemon’s name called Firewalld which is used to maintain the firewall policies. A dynamically managed firewall tool in a Linux system is known as Firewalld, it can be updated in real-time if there are any changes in the network environment.

This Firewalld works in concepts of zones (segments). We can check whether our firewall services are running or not by using the commands sudo (user access) and systemctl (use to control and manage the status of services).

sudo systemctl status firewalld

command to check the running status of our Firewalld services.

Command to check the running status of our Firewalld services.

output showing services actively running

Here is the output showing services actively running 

Some rules of Firewall

To protect our system from unauthorized access and to control network traffic (incoming and outgoing). We can do customization in ports, addresses, protocols, etc. some common examples are listed below:

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

By using this we can allow all incoming traffic on the SHH port so that we can connect to the system remotely.

sudo firewall-cmd --zone=public --add-services=ssh --permanent
sudo firewall-cmd --reload
As we can see it is done successfully

As we can see it is done successfully 

Rule 2: Allowing incoming traffic on a specific port

We are allowing traffic on a specific TCP port 8080 you can replace it with requirements.

sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
Both the command showed success

Both the command showed success

Rule 3: Blocking incoming traffic on a specific IP address

We are blocking incoming traffic on IP 192.168.52.1 you can replace it with your requirements.

sudo firewall-cmd --zone=public --add-rich='rule family="ipv4" source address="192.168.52.1" reject'
sudo firewall-cmd --reload
we have also mentioned family of IP (ipv4

we have also mentioned family of IP (ipv4)

Types of Linux Firewalls

There is more than one Linux firewall option available. When we come to drop down and research, we have a few popular names IPCop, iptables, Shorewall, and UFW But one of the most popular is the “iptables“ firewall.

Iptables Working:

Linux-based software that performs manipulation functions, packet filtering, and NAT (network address translation) is known as Iptables. With the help of Iptables which allows system administrators to control incoming and outgoing traffic by setting up the rules. 

When a packet is received in a Linux base system, it has to go through the chains and tables in the iptables firewall. The most commonly used tables are filter and nat but we have five predefined tables in iptables (raw, nat, filter, security, and mangle).

Types of Tables

We will discuss five predefined tables:

  1. Security Table: It is often used in conjunction with other security tools like SELinux, it is also used for MAC (Mandatory Access Control) rules, which can further be used to set rules related to security labels and access controls. It has four built-in chains: OUTPUT, FORWARD, INPUT, and SECMARK.
  2. Mangle Table: It is used to modify packets by setting the packet’s ToS/DSCP field, altering packet header fields, and changing packet marks. It has Five built-in chains: POSTROUTING, FORWARD, OUTPUT, PREROUTING, and INPUT.
  3. Nat Table: It stands for network address translation, which helps in sharing a single public IP address between multiple devices. It has two built-in chains: PREROUTING and POSTROUTING.
  4. Raw Table: It is used for the configuration of low-level packet processing. It has limited built-in chains, but the user can create additional chains if required.
  5. Filter Table: It is used for packet filtering. It has three built-in chains. INPUT, OUTPUT, and FORWARD.

Here filters are responsible for filtering the packets on the defined rules based on the source and destination of the IP address, port number, and protocol type. And Chains there are three different types of built-in chains.

Types of Chains

Chain Rule: Rules that are described for a particular task. Subdivided into three types:

  1. INPUT: Filter incoming traffic in the local system.
  2. OUTPUT: Filter Outgoing traffic for the local system.
  3. FORWARD: Packets forwarded from one system to another go throw it.

Configure a Firewall on Linux OS

We will be configuring iptables in our operating system.

To install iptables

 sudo dnf install iptables 
This command is use to install iptables

This command is use to install iptables

Basic Syntax for using iptables

sudo iptables [option] CHAIN-rule [-j target]

Note: 

  1. Output Chains: Traffic going through local machines has to pass through these output chains.
  2. Input Chains: Traffic has to go from every rule that has been assigned within input chains.
  3. Forward Chains: Traffic going from the arising network location to another network location has to pass through forward chains.

We have some common iptables options

Options Descriptions
-C [CHECK]: This is to check and find a rule that matches the requirements of the string.
-D [DELETE]: This is used to delete a specific rule.
-A [APPEND]: This is used to append or add rules.
-I [INSERT]: This can add a rule to a particular position in a string.
-L  [LIST]: To display all the rules we can use this.
-v [VERBOSE]: This is used to get more information in the list option.
-X [DELETE CHAIN]: This deletes the entire supplied string.
-p [Protocol_name]: It is used to define the name of the protocol.
-N [NEW CHAIN]: To create a new chain.
-j [job]: It tells what operation has to be done with the packet.
-F [Flush]: It is to delete all rules.
-s [specify]: It is a flag used to specify the source of the packet. 

Common Firewall Issues and Troubleshooting Tips

We have three basic Policies. Let’s discuss Some Basic Operations and their Syntax

  1. DROP: It can block an incoming signal, which basically states that the firewall is blocked for that particular IP.
  2. ACCEPT: It allows the IP we provide to make users go into the system.
  3. REJECT: It works similarly to Drop, but in ‘drop‘ the sender is blocked without any notification whereas in ‘reject‘ a message states the reason for not being able to connect.

Some basic operations and their syntax

Creating our first rule

The first rule to allow incoming ICMP (ping) traffic on the INPUT chain:

sudo iptables -A INPUT -p icmp -j ACCEPT

Uses ‘-A‘ to append the rule at the end of the INPUT chain. ‘-p icmp’ tells that rule is applying to ICMP traffic. ‘-j ACCEPT‘ tells you to accept(allow) any traffic that matches the rule.

CREATE FIRST RULE iptables

CREATE FIRST RULE iptables

The syntax for using policies

# Refer context mentioned above to see the use-case of [ -I , -A , -p , -s ,-j ] 
sudo iptables -I/-A name_chain -s source_ip -p protocol_name --dport port_number -j action_to_do 

Example:

Accept Rule: If we have to accept an IP (source) 192.168.160.51 on port number 22 using TCP protocol. 

sudo iptables -A INPUT -s 192.168.160.51 -p tcp --dport 22 -j ACCEPT
TO CHECK  OUTPUT WE USED $ sudo iptables -L

TO CHECK OUTPUT, WE USED $ sudo iptables -L

Drop Rule: If we have to Drop an IP (source) 192.168.160.51.

sudo iptables -A/-I chain_name -s source_ip -j action_to_do
As we can see 192.168.160.51 has been drop

As we can see 192.168.160.51 has been drop

Reset Rule: To reset all iptables rule we use -F.

sudo iptables -F
BERJAYA

As we can see all the rules has been reset

Conclusion

In this article, we have discussed Linux Operating System, Linux Firewall, and how to configure Linux firewall. Linux is based on Unix-like which has a modular design. To protect our system, we have a Linux firewall and firewalld. We will discuss the different types of Linux firewall and their rules. In the end, we learned about how to configure a firewall using iptables. By understanding this article one can secure their Linux system with the required firewall configuration. 



Previous Article
Next Article

Similar Reads

Identification of Web Application Firewall using WAFW00F in Kali Linux
A firewall is a security device that monitors incoming and outgoing traffic just like IPS and IDS. A firewall can be hardware and it can also be software. The firewall is a security mechanism that monitors and filters incoming traffic and also blocks outsiders from gaining unauthorized access to the internal systems of your company or organization.
3 min read
How To Setup an Iptables Firewall to Enable Remote Access to Services in Linux
A system administrator can modify the IP packet filter rules of the Linux kernel firewall, which are implemented as various Netfilter modules, using the user-space utility application iptables. The filters are arranged in several tables, each of which has a set of rules on how to handle packets of network data. Using a set of programmable table rul
5 min read
10 Useful firewall-cmd Commands in Linux
"Firewall-cmd" is a command-line tool used to manage firewalld, a dynamic firewall management tool found in many Linux distributions, such as Fedora, Red Hat, and CentOS. When it comes to managing firewall rules, Firewalld is more user-friendly and dynamic than the previous IPtables system. Administrators can configure the firewall by adding or rem
7 min read
How To Turn Off Firewall On Kali Linux?
Kali Linux is a popular operating system among cybersecurity professionals and enthusiasts due to its wide range of pre-installed tools for penetration testing and security auditing. However, for beginners, navigating its various features and configurations can be daunting. One common task that beginners may encounter is disabling the firewall. In
2 min read
How to setup Portmaster Firewall for Linux
"Linux is secure by default" You must have heard this a lot of times but guess what? It's a myth. Linux doesn't need a firewall or antivirusA base Linux system normally is not that secure compared to a base Windows system because by default it "normally" doesn't come with a firewall or antivirus, the only few things a default Linux distribution has
7 min read
How to Open Ports in Linux Server Firewall
Management of the server firewall and therefore control of the ports that are open is a very significant and fundamental procedure that any system administrator needs to master in order to control the network accessibility by closing sensitive ports. Firewalls are boundaries, that regulate traffic on the network based on a configured security polic
5 min read
How to Secure Your Linux Server Using UFW Firewall?
Maintaining a dependable operating system and safeguarding all the internal data require secure Linux server systems. Setting up a strong firewall management system is one of the easiest and most efficient ways to protect the security of the server. A user-friendly GUI for the potent Linux firewall utility tables is called Uncomplicated Firewall (U
5 min read
How to Disable the Firewall for Red Hat Linux
Linux systems, including those running Red Hat Enterprise Linux (RHEL), often come with a firewall enabled by default. The firewall serves as a crucial line of defense, protecting the system from unauthorized access and various network threats. However, there might be scenarios where you need to disable the firewall, such as for troubleshooting, te
5 min read
Setting up and Securing Ubuntu server with a Basic Firewall
VPS(Virtual Private Servers) are commonly used to host and serve many types of services. There are many providers that provide virtual servers. Many of these provide VPS's with their custom-built Linux OS which is lightweight compared to their desktop versions. These OS have built-in security but we can make them better. This article helps you to s
3 min read
How To Install apf-firewall on Ubuntu
APF, or Advanced Policy Firewall, is a firewall that is occasionally observed on Liquid Web servers. It is essentially an interface to iptables, which is Linux's standard interface for controlling network ports. Interacting with iptables may be complicated and error-prone, but APF substantially simplifies it. APF, on the other hand, is still only a
3 min read
How to Flush the Iptables and Clear the Firewall Rules
Iptables is a robust and versatile tool for managing firewall rules on a Linux system. However, beginners may find it challenging to work with. This article is designed to help newcomers understand the process of flushing iptables and clearing all firewall rules on a Linux system in greater detail, offering step-by-step guidance and practical examp
5 min read
Ubuntu Disable Firewall
Firewalls are vital components of network security, regulating incoming and outgoing traffic to protect systems from unauthorized access and potential threats. In Ubuntu, the Uncomplicated Firewall (UFW) provides a user-friendly interface for managing firewall settings. This article not only explains how to disable the firewall in Ubuntu but also c
3 min read
Difference Between Arch Linux and Kali Linux
Arch Linux is an open-source Linux-based Operating System that is freely available for use. It belongs to the Pacman-based Linux family. It is a light weighted OS for daily use and could even be used by professionals. It was first released in March 2002. It is a beginner-friendly Linux Operating System. Features of Arch Linux: Minimalist approach:
4 min read
Neofetch In Linux – Snap cool screenshots of your Linux
Neofetch is a fancy, highly customizable, and aesthetic command-line system information screenshot utility tool. Written in the Bash scripting language, it works inside a terminal window, on triggering up the command it by default displays the operating system, software, and hardware of your system on the right side of the column alongside an ASCII
4 min read
Exodus - Copy Linux Binaries From One Linux System
Exodus is an easy-to-implement program, that is used for bundling dependencies, and also helps to effectively copy Linux ELF binaries from one to another machine securely, and this is very handy when you don't have root access, also if there are issues in the availability of the packages in the Linux distribution, Commonly Server oriented distribut
3 min read
Basic CentOS Linux Commands in linux
CentOS is a free and open-source operating system that aims to provide a stable reliable, and community-supported platform for servers and other enterprise applications. In this article, we will be covering CentOS Linux basics commands and functions of CentOS and also we will look into the advanced commands. These CentOS commands allow you to manag
3 min read
Linux on Azure – Linux Distributions
Azure, the cloud computing platform from Microsoft, offers a playground for deploying and managing your Linux-based operating systems. Think of it as a canvas of boundless resources, security, and scalability, painted onto which you can unleash the boundless creativity and flexibility of Linux. This powerful synergy unlocks a world of opportunity,
10 min read
Linux Virtualization : Resource throttling using cgroups
In Linux Virtualization – Chroot Jail article, we discussed about kernel namespaces and process jailing. To understand this article, you may not need to read the earlier one, but I strongly suggest that you go through it once before diving into resource throttling. It should help tremendously in understanding what's going on. What are cgroups? cgro
9 min read
Kali Linux - Password Cracking Tool
Password cracking is a mechanism that is used in most of the parts of hacking. Exploitation uses it to exploit the applications by cracking their administrator or other account passwords, Information Gathering uses it when we have to get the social media or other accounts of the C.E.O. or other employees of the target organization, Wifi Hacking use
5 min read
XSS-Freak - XSS Scanner Fully Written in Kali Linux
XSS or Cross-Site Scripting is the most emerging security flaw in Web Applications. When the arbitrary or malicious JavaScript is executed by the web application then it is said to be an XSS Vulnerable Website. There are various XSS Scanners through which we can detect the XSS on the target domain. XSS-Freak is an XSS Scanner developed in the Pytho
3 min read
Tracing memory usage in Linux
Memory is a critical resource in computing because its usage affects the performance and stability of a system. It's necessary to trace the memory usage of the system in order to determine the program that consumes all CPU resources or the program that is responsible for slowing down the activities of the CPU. Tracing memory usage also becomes nece
3 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?
In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its software, libraries, and configuration files. Containers
2 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
Internal and External Commands in Linux
The UNIX system is command-based i.e things happen because of the commands that you key in. All UNIX commands are seldom more than four characters long. They are grouped into two categories: Internal Commands : Commands which are built into the shell. For all the shell built-in commands, execution of the same is fast in the sense that the shell doe
4 min read
Cool Custom Welcome Messages on Linux terminal
There is a Linux hack which can be used to display a custom message on the terminal every time we open it. This can be done by manipulating the .bashrc shell script. What is .bashrc ? It is a shell script that Bash runs whenever it is started interactively. You can put any command in that file that you could type at the command prompt. Steps on how
3 min read
Useful and time saving bash commands in Linux
There are many useful bash scripts that can make things easier. Many things can be done just using the terminal. Here is a list of some of such useful bash scripts through which we can do things that cannot be done manually. 1. Counting the number of files(or directory) in a given directory: Go to the directory in which you want to count the number
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
Process Control Commands in Unix/Linux
Process control commands in Unix are: bg - put suspended process into background fg - bring process into foreground jobs - list processes bg Command : bg is a process control command that resumes suspended process while keeping them running in the background. User can run a job in the background by adding a "&" symbol at the end of the command.
3 min read
Processes in Linux/Unix
A program/command when executed, a special instance is provided by the system to the process. This instance consists of all the services/resources that may be utilized by the process under execution. Whenever a command is issued in Unix/Linux, it creates/starts a new process. For example, pwd when issued which is used to list the current directory
6 min read
Article Tags :