Ubuntu Linux Save / Restore Iptables Rules

by Vivek Gite · 8 comments

I want to make changes to an iptables configuration. How to do I Save and Restore an iptables Configuration in Ubuntu Linux server?

If you want to make changes to an iptables configuration, it is always good idea to save the current configuration by typing the following commands:
$ sudo -s
$ iptables-save > /root/working.iptables.rules

Now, you can make any changes using command line.

To restore it use the command iptables-restore, enter:
# iptables-restore < /root/working.iptables.rules

Featured Articles:

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!

{ 8 comments… read them below or add one }

1 Michael 06.24.09 at 2:40 pm

Thanks for the tip!
Not only useful for Ubuntu users!

2 Mike 06.24.09 at 3:23 pm

Thanks

Here’s another little tip. If you add a shebang line to the top of the saved iptables file like “#!/usr/bin/env iptables-restore” and make it executable (chmod +x) you can then just execute the saved firewall rules like so ./my-firewall

3 Vivek Gite 06.24.09 at 3:45 pm

@Mike, Excellent tip. I never thought about it.

@Michael, yes it works on all Linux distro.

Appropriate your posts!

4 Joost 06.26.09 at 4:08 pm

Be carefull if you change your iptables file remotely (via ssh). A mistake might lock you out.
In that case it is safer to use iptables-apply. It changes the iptables, but gives you a prompt to confirm the change. If the change locked you out, it will revert to the previous ruleset.
So change the /root/working.iptables.rules file, and use:

iptables-apply -t 15 /root/working.iptables.rules

You have 15 seconds to accept the change.
Only works on newer versions of iptables (Ubuntu jaunty in my case).

5 budacsik 07.01.09 at 10:14 am

tip:
iptables auto on/off

1.)
Run in terminal the next command:
sudo iptables-save > /etc/iptables.rules

2.) Edit /etc/network/interfaces file and add this two line to interface configuration:
pre-up iptables-restore /etc/iptables.rules

To sum:

auto eth1
iface eth1 inet static
address 192.168.2.1
network 255.255.255.0
broadcast 192.168.2.255
pre-up iptables-restore /etc/iptables.rules

3.) Save and test (reboot)

6 badwolf 07.10.09 at 12:27 pm

One other way of safeguarding yourself when you modify iptables via ssh is (before you start fiddling around ;-) )to set a cron job to reset the firewall rules every say 15 minutes. That way you only have to wait at most 15 minutes to get back into your machine. Don’t forget to disable the cron job when you are finished. This presumes that you have root/sudo access to be able to set the cron and run iptables-restore as super user.
Thanks for the info.

7 Salvador 01.29.10 at 12:17 am

budacsik:

You made a little mistake in the line:
pre-up iptables-restore /etc/iptables.rules

it must be:
pre-up iptables-restore < /etc/iptables.rules

8 budacsik 01.29.10 at 7:29 am

Salvador:
You are right! Sorry for mistake.

(I’m sorry, but I can’t edit it.)

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 FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All