Automatically loading iptables rules on Debian/Ubuntu

If you want your iptables rules automatically loaded every time your networking comes up on your Debian or Ubuntu server, you can follow these easy steps.

First, get your iptables rules set up the way you like them. Once you've verified that everything works, save the rules:

iptables-save > /etc/firewall.conf

Next, open up /etc/network/if-up.d/iptables in your favorite text editor and add the following:

#!/bin/sh
iptables-restore < /etc/firewall.conf

Once you save it, make it executable:

chmod +x /etc/network/if-up.d/iptables

Now, the rules will be restored each time your networking scripts start (or restart). If you need to save changes to your rules in the future, you can manually edit /etc/firewall.conf or you can adjust your rules live and run:

iptables-save > /etc/firewall.conf

Thanks to Ant for this handy tip.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Printed from: http://rackerhacker.com/2009/11/16/automatically-loading-iptables-on-debianubuntu/ .
© Major Hayden 2010.

2 Comments   »

  • Bartek says:

    Thanks for the post but quick question, what's the difference between doing this and just setting a line in /etc/network/interfaces?

    pre-up iptables-restore < /etc/iptables.up.rules

    Thanks!

  • major says:

    Bartek:

    That method would have the same effect. I normally just try to keep the /etc/network/interfaces clear of extra stuff, but that's just my own personal preference. ;-)

Trackbacks/Pingbacks

  1. Circumventing Hulu Regional Restrictions in Mac OS X | langui.sh

RSS feed for comments on this post , TrackBack URI

Leave a Reply