Iptables is one of the main firewall management tools used in Linux systems, allowing you to filter network traffic according to specified rules. While in many server configurations the rules are created once and rarely changed, sometimes you may need to review, update, or completely remove certain firewall rules. In this tutorial, you will learn how to view, filter, and delete active iptables rules in the Linux operating system.
Before you start, make sure you have the iptables firewall installed on your operating system and that you are using a privileged user account with sudo permissions.
First, let’s see how to list the firewall rules. To list all active rules by specification, use the command:
sudo iptables -S

If you want to view only specific rules, such as those in the INPUT, OUTPUT, TCP, etc. chains, you can specify the chain name after the -S argument. For example, to view the INPUT chain:
sudo iptables -S INPUT

Viewing rules in a table format can be useful when you need to compare different rules together. To view all active rules in a table, use the -L argument:
sudo iptables -L

sudo iptables -L INPUT

The first line shows the chain name (in this case, INPUT) along with the default policy (ACCEPT). The next lines show each rule in the chain.
The last (unnamed) column specifies the rule options, such as source/destination ports and states.
When reviewing iptables rules, you can also see the number of packets and the total size in bytes that matched a given rule. This is useful for seeing which rules are actively matching packets. To view this, use the -L and -v parameters together. For example, to view the INPUT chain with -v:
sudo iptables -L INPUT -v

If you want to clear or reset the packet and byte counters, use the -Z parameter. These counters are also cleared when the OS is restarted. This parameter is useful if you want to check whether your server is receiving traffic according to the matching rules.
To clear counters for all chains and rules:
sudo iptables -Z
To clear counters for all rules in a specific chain (e.g., INPUT):
sudo iptables -Z INPUT
To clear counters for a specific rule, specify the chain name and the rule number. For example, to clear counters for the first rule in the INPUT chain:
sudo iptables -Z INPUT 1
One way to delete rules is to provide their specification with the -D parameter. For example, to delete a rule that drops invalid incoming packets:
sudo iptables -D INPUT -m conntrack --ctstate INVALID -j DROP
Note that the -A parameter, which specifies where the rule is added during creation, should not be used when deleting.
Another way is to use the chain name and line number. First, list the rules with line numbers:
sudo iptables -L --line-numbers

Then, to delete a specific rule:
sudo iptables -D INPUT 3
Iptables allows you to flush chains - this removes all rules in that chain.
Flush a single chain:
sudo iptables -F INPUTFlush all chains:
sudo iptables -F
Iptables provides great flexibility and allows you to effectively manage firewall rules on Linux systems. With just a few basic iptables commands, you can perform detailed rule and chain viewing, filtering, editing, and deletion. If you have any questions or encounter difficulties, contact our live support or email us at: [email protected]
How to install LAMP server (Linux, Apache, MySQL, PHP)LAMP ("Linux-Apache-MySQL-PHP") is a popular open-source stack for hosting dynamic websites. Because it is free, highly adaptable,...
How to run Grafana in Docker containerGrafana is a leading open-source tool for visualizing time series data. It’s widely used to visualize data from various...