Table of Contents
Managing firewall rules is an important task on any Linux server, and knowing how to list or delete iptables rules helps you control network access more effectively.
Step 1: List All iptables Rules
To start managing iptables effectively, you first need to view all existing firewall rules, and this step helps you quickly check every chain and rule so you can understand what needs to stay or be removed.
sudo iptables -L -n -v
Short intro for the second command:
sudo iptables -L --line-numbers
Step 2: List Rules for a Specific Chain
To view rules inside a specific iptables chain (such as INPUT, OUTPUT, or FORWARD), you can use a simple command that helps you quickly check only the chain you need without scanning the entire firewall configuration.
iptables -L INPUT -n --line-numbers
Short intro for the second command:
iptables -L OUTPUT -n --line-numbers
Step 3: Delete an iptables Rule by Line Number
To remove a specific firewall rule safely and precisely, you can delete it using its line number, which ensures you don't accidentally remove the wrong rule and helps maintain a clean and organized iptables configuration.
sudo iptables -D INPUT 3
Step 4: Delete iptables Rules by Matching the Rule
When you need to remove a specific firewall rule based on its exact parameters, deleting it by matching the rule is a clean and efficient method that ensures accuracy and keeps your iptables configuration organized.
sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT
Short intro for second code:
sudo iptables -D OUTPUT -p udp --dport 53 -j DROP
Step 5: Flush (Delete) All Rules
To completely reset your firewall settings, you can flush all iptables rules at once, which removes every rule from all chains and gives you a clean configuration for troubleshooting or rebuilding your firewall.
sudo iptables -F
Short intro for second code:
sudo iptables -t nat -F
Step 6: Save iptables Changes
To ensure your firewall rules remain active after a reboot, you should save the iptables configuration using the appropriate command for your Linux distribution.
sudo netfilter-persistent save
Alternative for RHEL/CentOS-based systems:
sudo service iptables save
Step 7: Check Current Active Rules
To make sure your firewall settings are correctly applied and still active after modifications, you can easily review the currently enforced iptables rules using a simple command.
sudo iptables -S
Short intro for second code:
sudo iptables -L -v -n
