How To Reset Linux Firewall Automatically While Testing Configuration With Remote Server Over SSH Session

Q. I’d like to tell my Linux iptables firewall to flush out the current configuration every 5 minutes. This will help when I’m testing a new rules and configuration options. Some time I find myself locked out of my own remote server. How do I reset Linux firewall automatically without issuing hard reboot?

A. You can easily flush out current configuration using iptables command and shell script combo. There is no built in option for this kind of settings. So you need to write a small shell script and call it from crontab file.
Create a firewall reset shell script

Create a /root/reset.fw script:

# reset.fw – Reset firewall
# set x to 0 – No reset
# set x to 1 – Reset firewall
# —————————————————————————————————————
# Added support for IPV6 Firewall
# —————————————————————————————————————
# Written by Vivek Gite
# —————————————————————————————————————
# You can copy / paste / redistribute this script under GPL version 2.0 or above
# =============================================================

# set to true if it is CentOS / RHEL / Fedora box

### no need to edit below ###

if [ “$x” == “1” ];
if [ “$RHEL” == “true” ];
# reset firewall using redhat script
/etc/init.d/iptables stop
/etc/init.d/ip6tables stop
# for all other Linux distro use following rules to reset firewall
### reset ipv4 iptales ###
for table in $(


I am a g33k, Linux blogger, developer, student and Tech Writer for My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

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