Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Every time I reboot I loose the iptables rules that took me so looong to enter. I know I can save them and restore them on boot, but is there anyway to save them forever? Do I really need to restore them on boot every time? Seriously?

The problem is I have a HUGE list of IPs in which I use a while loop to load them in. This can take upwards of 10 minutes.

This is my home FTP server. It's a small vm with 1gb ram and very little processing power. There are so many IPs because I've pretty much given up on the Asian continent. I don't need them to be hitting up my FTP server everyday with brute force. I also block gov. monitors, trackers and spammers.

This is the while loop I use to load in the list.

grep INPUT block.list | while read LISTA; do sudo iptables -A $LISTA; done
share|improve this question
    
Your question is a little bit ambiguous, when you say 'save them forever' I thought you meant as in to a file, but after reading the rest it sounds like what you actually mean is you don't want to have to reload them manually at boot every time. Also, I think the number of rules that need to be reloaded will be the same so it will still take awhile but you won't have to do it manually, following pankaj's answer. –  Jeff Welling Mar 30 '12 at 2:54
    
possible duplicate of iptables resets when server reboots –  Eliah Kagan Aug 5 '12 at 0:37
add comment

1 Answer

up vote 8 down vote accepted

First, create a file with the contents of iptables-save:

sudo iptables-save > /etc/iptables_rules

It doesn't really matter where you put the file, all you have to do is make sure that the next line refers to the same file. Next, open /etc/rc.local and add this line:

/sbin/iptables-restore < /etc/iptables_rules

From now on, every time your computer powers up or restarts iptables will load rules from the file that you specified.

share|improve this answer
    
I get -bash: /etc/iptables_rules: Permission denied on the first command. Which is weird using sudo and all. –  capdragon Mar 30 '12 at 14:57
3  
I had to use sudo sh -c '/sbin/iptables-save > /etc/iptables.save' to get around the Permission denied error. Reference –  capdragon Mar 30 '12 at 17:02
    
u can create file in any directory it is not mandatory to create file in /etc/ –  pankaj sharma Mar 31 '12 at 5:16
1  
I would not recommend using /etc/rc.local for this. It is executed later than the rest of your system startup and potentially leaving your system in a non-firewalled state. I would suggest to place this one-line script in /etc/network/if-up.d/firewall (new file). This makes sure it will be executed every time you enable your network interfaces. –  gertvdijk Jan 21 '13 at 19:07
1  
The permissions error is normal/expected. sudo applies only to the command before the ">", the file redirection is done separately by your shell. –  ImaginaryRobots Jan 21 '13 at 19:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.