How to use iptables Firewall Rules on Linux
Iptables is a cornerstone of Linux system administration, providing a robust and flexible way to manage network traffic. As a command-line utility, it interfaces with the Linux kernel’s netfilter framework, acting as a powerful firewall. By defining rules, administrators can precisely control the flow of data in and out of a system, enhancing security and optimizing network performance. This article will delve into the fundamentals of iptables, covering its syntax, common commands, and practical applications. We’ll explore how to list, add, delete, save, and restore rules, giving you a solid foundation for securing your Linux server.

Introduction
Iptables is a powerful Linux utility that allows system administrators to configure the kernel’s built-in firewall. Iptables uses a set of rules to determine how to filter network traffic. Each rule specifies what type of traffic to filter and what action to take on matching traffic.
In this guide, we will discuss some basic iptables rules and commands to help secure your server. By default, iptables blocks all incoming traffic and allows all outgoing traffic. This is not very secure, so we will need to add some rules to make our server more secure. Understanding how to use iptables Firewall Rules on Linux is a fundamental skill for any Linux system administrator.
Prerequisites
Before we get started, let’s go over the basic syntax for iptables. The general syntax for iptables is as follows:
$ iptables -A <chain> -p <protocol> -s <source> -d <destination> -j <action>
Where:
Chains
Chains are used to group together related iptables rules. There are three built-in chains:
- INPUT: This chain is used for filtering incoming traffic to the server.
- OUTPUT: This chain is used for filtering outgoing traffic from the server.
- FORWARD: This chain is used for filtering traffic that is being routed through the server.
Or you also create our own custom chains.
Actions
There are two main actions that we can take with iptables: ACCEPT and DROP.
- ACCEPT: This action allows the traffic to pass through.
- DROP: This action drops the traffic.
Basic Commands
Listing Rules
The iptables -L
command is used for listing all the rules in a chain.
$ iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
The -v
option is used for listing the rules with verbose output.
$ iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Adding Rules
The iptables -A
command is used for adding a rule at the end of a chain.
$ iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
This rule allows traffic from the IP address range 192.168.1.0/24 to be accepted on the INPUT chain.
The -I
option is used for adding a rule at the specified position in a chain.
$ iptables -I INPUT 2 -s 192.168.1.0/24 -j ACCEPT
This rule inserts the previous rule as the second rule in the INPUT chain.
The -p
option is used for specifying the protocol and --dport
option is used for specifying the destination port.
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT
This rule allows TCP traffic on port 22 (SSH) to be accepted on the INPUT chain.
Deleting Rules
The iptables -D
command is used for deleting a rule at the specified position in a chain.
$ iptables -D INPUT 2
This command deletes the second rule from the INPUT chain.
-F
: option is used for deleting all the rules in a chain.
$ iptables -F INPUT
This command flushes all the rules from the INPUT chain.
-X
: The option is used for deleting a user defined chain.
$ iptables -X mychain
This command deletes a user-defined chain named "mychain".
-P
This option is used for specifying the default policy for a chain.
$ iptables -P INPUT DROP
This command sets the default policy for the INPUT chain to DROP, meaning that any traffic not explicitly allowed by a rule will be dropped.
Saving Rules
The iptables-save
command is used for saving the current iptables rules.
$ iptables-save > /etc/iptables.rules
This command saves the current iptables rules to a file named /etc/iptables.rules
.
The iptables-restore
command is used for restoring the saved iptables rules.
$ iptables-restore < /etc/iptables.rules
This command restores the iptables rules from the file /etc/iptables.rules
. These are key commands for how to use iptables Firewall Rules on Linux effectively.
Conclusion
In this guide, we learned how to list, delete, save, and restore iptables rules. Using these basic commands, you can begin configuring your firewall to protect your server.
Alternative Solutions for Managing Firewall Rules
While iptables is a powerful tool, it can be complex to manage directly. Several alternative solutions provide a more user-friendly interface and enhanced features. Here are two such alternatives:
1. UFW (Uncomplicated Firewall)
UFW is a front-end for iptables designed to simplify firewall configuration. It provides a more intuitive command-line interface and easier-to-understand syntax. UFW is particularly well-suited for users who are new to firewalls or who prefer a less technical approach.
Explanation:
UFW abstracts away much of the complexity of iptables by using simple commands to define rules based on application profiles or port numbers. It also provides logging capabilities and the ability to reset the firewall to its default state.
Code Example:
To allow SSH traffic (port 22) using UFW, you would use the following command:
sudo ufw allow ssh
Or, to allow traffic on a specific port:
sudo ufw allow 80/tcp # Allows TCP traffic on port 80 (HTTP)
sudo ufw allow 443 # Allows traffic on port 443 (HTTPS)
To enable the firewall:
sudo ufw enable
To check the status of the firewall:
sudo ufw status
UFW manages the underlying iptables rules, so you don’t need to directly manipulate them. This makes it a significantly easier solution for many common firewall tasks. Using UFW is a different way how to use iptables Firewall Rules on Linux but with a simpler syntax.
2. Firewalld
Firewalld is a dynamic firewall management tool that provides a higher-level abstraction than iptables. It uses the concept of "zones" to define different trust levels for network connections. Firewalld is particularly useful in environments with frequently changing network configurations, such as laptops or servers that connect to multiple networks.
Explanation:
Firewalld allows you to define different zones (e.g., home, work, public) and assign network interfaces to those zones. Each zone has its own set of rules and trust levels. This makes it easy to apply different firewall policies depending on the network you’re connected to. Firewalld uses a command-line interface (firewall-cmd
) and can be managed through a graphical user interface as well.
Code Example:
To set the default zone to "home":
sudo firewall-cmd --set-default-zone=home
To add the SSH service to the "home" zone:
sudo firewall-cmd --zone=home --add-service=ssh --permanent
To reload the firewall to apply the changes:
sudo firewall-cmd --reload
To list all the rules in the "home" zone:
sudo firewall-cmd --zone=home --list-all
Firewalld provides a more sophisticated approach to firewall management, allowing for dynamic updates and zone-based configurations. It’s another effective method for implementing how to use iptables Firewall Rules on Linux, offering greater flexibility and control in complex network environments. By using these alternative solutions, understanding how to use iptables Firewall Rules on Linux can be achieved through different interfaces and methodologies.