1

I have a script which loads iptables at /etc/network/if-pre-up.d/iptablesload:

#!/bin/sh

iptables-restore < /etc/iptables.rules

ip6tables-restore < /etc/ip6tables.rules

exit 0

I also have a script which save iptables at /etc/network/if-post-down.d/iptablessave:

#!/bin/sh

# By saving the current iptables rules to the file at the specific path, 
# then they will be loaded on every startup.

echo "Saving current iptables rules..."

iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
   iptables-restore < /etc/iptables.downrules
fi

ip6tables-save -c > /etc/ip6tables.rules
if [ -f /etc/ip6tables.downrules ]; then
   ip6tables-restore < /etc/ip6tables.downrules
fi


exit 0

I have a file at path /etc/iptables.rules which contains all the rules.

When I manually execute iptables-restore < /etc/iptables.rules then I can see all iptables rules.
But after powering on the PC, then all iptables rules are missing (all is empty).

My conclusion is that the scripts in folders /etc/network/if-post-down.d and /etc/network/if-pre-up.d don't execute. Why?

(The file /etc/iptables.rules exists because I manually executed iptables-save -c > /etc/iptables.rules.)


I'm using the exact same scripts that I used for Ubuntu 18 - they worked well.

The above is originally taken from:
https://help.ubuntu.com/community/IptablesHowTo#Solution_.232_.2Fetc.2Fnetwork.2Fif-pre-up.d_and_...2Fif-post-down.d

I also tried copying exactly as they write in the link above, but still iptables failed to load.

4
  • 2
    Correct, they don't execute because the /etc/interfaces file and ifupdown are not used anymore. Ubuntu has switched to networkd, which is just a front end for systemd-networkd. I used to do your method also, and still do on my 16.04 server. I do not like netplan and am doing it now directly with networkd as a post-boot process on my newer server. – Doug Smythies Nov 30 '20 at 23:03
  • Please clarify your release(s). Ubuntu uses yy releases only for specialist snap based releases for IoT appliances/devices or cloud based use (having done so since 2016). Main releases use yy.mm such as used by server & desktops releases. – guiverc Nov 30 '20 at 23:52
  • @guiverc: Release is 20.04 – Dor Dec 1 '20 at 23:57
  • Please correct your question, Ubuntu Core 20 and releases using the yy format are different products to yy.mm releases, having been so since 2016 – guiverc Dec 1 '20 at 23:59
0

It appears that the file you are looking for is now called
/etc/iptables/rules.v4

e.g.

sudo cat /etc/iptables/rules.v4
# position 1
*filter
# [snip] -- I checked the rules, they are there

So you should be able to

# must be root
sudo -i 
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6

and to restore:

# must be root
sudo -i
sudo iptables-restore < /etc/iptables/rules.v4
sudo ip6tables-restore < /etc/iptables/rules.v6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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