26

Everytime my server reboots it seems I have to reset my iptables to default, I used to use firestarter but removed it a while ago. Is there any settings that are making iptables change when the server reboots?

5 Answers 5

19

On Ubuntu 16.04:
sudo apt-get install iptables-persistent

After that, run next commands every time you want save iptables changes permanently:
sudo netfilter-persistent save
sudo netfilter-persistent reload

3
  • 3
    How does this differ really from the existing answers which all say to install iptables-persistent and then use iptables-save and such?
    – Thomas Ward
    Mar 15, 2018 at 13:58
  • 2
    All commands which you need in a one place. Ready to use.
    – Rib47
    Nov 7, 2018 at 10:56
  • No needs to manually call load (ref). Only save the changes.
    – SAMPro
    May 11, 2020 at 18:54
17

This is the default behavior.

You may want to take a look at the package iptables-persistent to automatically set iptables rules at startup from a configuration file.

0
2

iptables-persistent by default does NOT automatically save the system's iptables when rebooting.

However, I modified the source code of the package in Ubuntu 14.04 to automatically save the rules on reboot, shutdown (halt), or runlevel 1. When the system boots to runlevels 2-5, the rules are restored. This way, saving and loading the rules happens automatically without user intervention.

To use this version, please use the following commands:

sudo apt-get remove iptables-persistent
sudo dpkg -r iptables-persistent
wget http://dinofly.com/files/linux/iptables-persistent_0.5.8_all.deb
sudo dpkg --install iptables-persistent_0.5.8_all.deb

See my full blog post here:

http://blog.eamster.tk/?p=389

Use at your own risk. All that was modified is the init script stop function calls the save rules function so that when the system is shutdown, rebooted, or in level1, the rules are automatically saved.

I've tested this updated deb package on Ubuntu 12.04, Ubuntu 14.04, and Ubuntu 15.04. It should work on any version of debian as well.

2

Save the rules in a file

iptables-save > /etc/iptables/rules.v4

Then restore the file by following commands by including in rc.local file

#vim /etc/rc.local 

/sbin/iptables-restore < /etc/iptables.rules.v4

This works fine for me

2
  • 1
    Please note that these instructions refer to /etc/iptables/rules.v4 and /etc/iptables.rules but just one of these should be used in both cases.
    – Rick-777
    Jul 9, 2020 at 22:27
  • Somewhat correct.. But, 1: You need to install iptables-persistent. 2: Fix your typo. /etc/iptables.rules.v4 should be: /etc/iptables/rules.v4. 3: You need to note that you need to be root, or use sudo. If using sudo see my answer. 4: You do not need to run restore. This command is run on a reboot automatically (Ubuntu 20/22) from when you installed iptables-persistent. Downvoted till fixed.
    – B. Shea
    Dec 11, 2023 at 15:50
0

sudo apt install iptables-persistent

Then add bad IPs to your iptables.

Once satisfied with rules, save them:

sudo sh -c 'iptables-save > /etc/iptables/rules.v4'

(Or IPv6: sudo sh -c 'iptables-save > /etc/iptables/rules.v6')

Done. Reboot/test. Whatever you saved pre-reboot should be loaded again.


Wrote this because all the answers on Internet (and here) are wrong (unless you are root - and if you are root why use sudo?)

You cannot run SUDO with a redirect (or pipe) to a root area without ('bash -c' or 'sh -c').

2
  • you could just sudo su - then use the other answers. Though I've always just modified rules.v# manually and reloaded iptables...never did live edits and then saved it from the live edits.
    – rtaft
    Dec 11, 2023 at 15:49
  • @rtaft You can always become root to do root based things. This should go without saying, but you are correct. I am pointing how to use a redirect in this context using sudo. All the answers I've found today (not just stack) simply are wrong (ones that use sudo). They obviously didn't test the command before posting it.
    – B. Shea
    Dec 11, 2023 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .