How to Find and Remove Firewall Rules in IPTables

We’ll show you how to find out and remove firewall rules in iptables. Iptables is a command line utility that allows system administrators to configure packet filtering rules installed on Linux. Iptables requires elevated privileges to work and must be run by the root user, otherwise it won’t be able to function.

How to find out firewall rules in iptables

Iptables lets you know all the rules that are already added to the packet filtering rule set. In order to be able to check this, you must have SSH access to the server. Connect to Linux VPS using SSH and run the following command:

sudo iptables -nvL

To run the command above, your user must have sudo privileges. Otherwise, you need to add the sudo user to your Linux VPS or use the superuser.

If there are no rules added to the packet filtering, the output should be similar to the one below:

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

Since NAT (Network Address Translation) can also be configured using IPTables, you can use IPTables in the list of NAT rules:

sudo iptables -t nat -n -L -v

The output will be similar to the one below if there are no added rules:

Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (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

How to remove firewall rules in IPTables

At some point, it might be necessary to remove a specific firewall rule in Iptables on the server. For this purpose, the following syntax must be used:

iptables [-t table] -D chain rulenum

For example, if you have a firewall rule to block all connections from 111.111.111.111 to the server on port 22, and you want to remove this rule, you can use the following command:

sudo iptables -D INPUT -s 111.111.111.111 -p tcp --dport 22 -j DROP

Now that you have removed the iptables firewall rule you need to save the changes to make them permanent.

In case you are using Ubuntu VPS, you need to install an additional package for this purpose. To install the required package use the following command:

sudo apt-get install iptables-persistent

On Ubutnu 14.04 you can save and reload firewall rules using below commands:

sudo /etc/init.d/iptables-persistent save
sudo /etc/init.d/iptables-persistent reload

IN Ubuntu 16.04 use the following commands:

sudo netfilter-persistent save
sudo netfilter-persistent reload

If you are using CentOS VPS you can save changes using below command:

service iptables save

Sidebar