How Do I run firewall script as soon as eth0 interface brings up?

At home I connect to Internet via ADSL modem.

As soon as my eth0 comes up I would like to have my firewall script get executed and setup IPTABLE for me :)

Earlier I used to type command /root/fs.dsl.start via sudo. However while reading man page of interface file I came across post-up option. Following step demonstrates usage of post-up option:

1) Copy firewall script to /etc/network/if-up.d/ directory:
# mv /root/fw.dsl.start /etc/network/if-up.d/

2) Open Debian networking configuration file /etc/network/interfaces:
# vi /etc/network/interfaces

3) Setup post-up option, append following line to eth0 configuration section:
# post-up /etc/network/if-up.d/fw.dsl.start

Where,

  • post-up command : Run command after bringing the interface up.

Here is my /etc/network/interfaces after modification:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
name Ethernet LAN card
address 192.168.1.1
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.254
post-up /etc/network/if-up.d/fw.dsl.start

4) Save file and restart networking:
# /etc/init.d/networking restart

5)Verify that iptables rules are loaded:
# iptables -L -n

Tip if you are new to Ethernet (LAN network) configuration under Debian Linux use following etherconf package to configure or reconfigure network. If etherconf is not installed use apt-get to install it:

a) Install etherconf:
# apt-get install etherconf

b) Configure network under Debian Linux:
# dpkg-reconfigure etherconf

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 4 comments… read them below or add one }

1 Jevin Ramjattan 04.04.07 at 3:28 pm

How do I do this on Fedora?

2 nixcraft 04.04.07 at 5:12 pm

Jevin,

Use /etc/eth0/ip-up.local
file.

HTH

3 Tahder 01.11.08 at 12:05 am

How do i use it in Ubuntu? why isn’t working when it is a Debian based….

4 Gabor 03.15.08 at 8:18 am

In section 3) hash mark should be removed from line
# post-up /etc/network/if-up.d/fw.dsl.start

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Linux Shutdown Command and Logfile

Next post: How to save your live CD session online