Linux – Add and delete firewall rules in iptables

In Linux, we use the iptabes to manage different firewall rules.

1. List all the rules.

  • iptables -L

iptables-add-delete-rules-1
 

2. Allow incoming tcp port 8888 only from 123.123.123.123.

  • iptables -A INPUT -i eth0 -p tcp –dport 8888 -s 123.123.123.123 -j ACCEPT -m comment –comment “001 Testing rule comment”

 

3. List the rules again.
iptables-add-delete-rules-2
 

4. To delete the rule you can use the following command.

  • iptables -D INPUT -i eth0 -p tcp –dport 8888 -s 123.123.123.123 -j ACCEPT -m comment –comment “001 Testing rule comment”

 

5. Alternatively, you can first list the firewall rules in line numbers.

  • iptables -L INPUT –line-numbers

iptables-add-delete-rules-3
 

6. And then delete a specific rule by providing the line number, in my case which is 11.

  • iptables -D INPUT 11

iptables-add-delete-rules-4
 

7. Finally, make sure you have save the changes to the server iptables confif file. (Thanks Philip =D)
For Ubuntu Lucid:

  • iptables-save > /etc/iptables.firewall.rules

For CentOS/Fedora

  • iptables-save > /etc/sysconfig/iptables

 

Done =)

Reference: StackOverflow – iptables remove specific rules

Advertisement

5 thoughts on “Linux – Add and delete firewall rules in iptables”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.