I have a kvm VM with Ubuntu 14.04 set as a firewall in a virtual network (with eth0 and eth1)

This is not a course but at work. I have to learn this.

Intermittently eth1 will fail to come up.

In /etc/network/interfaces both eth0 and eth1 are set manually.

When eth0 should be up there is a up command /etc/interim-fw.sh that allows nothing and no forwarding between the ports.

Then eth1. There is a pre-up where i send a comment to a log file that it has reached this point. Then link up also and then up command to /etc/firewall.sh that will load the iptables with all rules that are needed.

Randomly this will not load and only reach pre-up of eth1. But once the VM has loaded if I run bash /etc/firewall.sh it will load completely and all will be fine!

So my question is: How can I solve this? Why am I getting a race condition, if that is what I am getting?


auto lo  
iface lo inet loopback  

auto eth0  
iface eth0 inet manual  
  up ip a a dev eth0  
  up ip li set up dev eth0  
  up /etc/interim-fw.sh  
  down ifconfig eth0 down  

 auto eth1  
 iface eth1 inet manual  
    up ip a a dev eth1  
    up ip li set up dev eth1  
    up ip route add default via  
    post-up /etc/firewall.sh  
    down ifconfig $IFACE down  


!/bin/bash -e

I_INT=eth0  # Intranet-LAN-Interface  
I_DMZ=eth1  # DMZ-LAN-Interface  
STATE_NEW="-m state --state NEW"  
PING="-p icmp --icmp-type 8"                        #ICMP packet type 8 alias echo request (= ping request )
REM="-m comment --comment"
iptables-restore << @@@@@@@@

-A INPUT -i lo -j ACCEPT

-A INPUT  -p icmp -m icmp --icmp-type any  $STATE_NEW  $REM "traceroute" -j ACCEPT
-A INPUT  -p udp      --dport 33434:33523  $STATE_NEW  $REM "traceroute" -j ACCEPT

-A INPUT   -i $I_INT -p tcp           --dport 22345 $STATE_NEW  $REM "ssh" -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -p tcp --dport 22345 $STATE_NEW  $REM "ssh" -j ACCEPT

-A OUTPUT -o $I_DMZ -d $SATURN -p tcp --dport 53  $STATE_NEW $REM "dns"  -j ACCEPT
-A OUTPUT -o $I_DMZ -d $SATURN -p udp --dport 53  $STATE_NEW $REM "dns"  -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -d $SATURN -p tcp --dport 53  $REM "dns" $STATE_NEW  -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -d $SATURN -p udp --dport 53  $REM "dns" $STATE_NEW  -j ACCEPT

-A FORWARD -i $I_INT -o $I_DMZ -d $MASTER -p tcp --dport 53  $REM "dns" $STATE_NEW  -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -d $MASTER -p udp --dport 53  $REM "dns" $STATE_NEW  -j ACCEPT


-A FORWARD -i $I_INT -o $I_DMZ -d $UBUNTU -p tcp --dport 80 $STATE_NEW $REM "Ubuntu"  -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -d $REACTOS -p tcp -m multiport --dports 80,443 $STATE_NEW $REM "Reactos"  -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -d $MOZILLA -p tcp -m multiport --dports 80,443 $STATE_NEW $REM "Mozilla"  -j ACCEPT
-A FORWARD -i $I_INT -o $I_DMZ -d $PYTHON -p tcp -m multiport --dports 80,443 $STATE_NEW $REM "Python"  -j ACCEPT
-A OUTPUT            -o $I_DMZ -d $UBUNTU -p tcp --dport 80 $STATE_NEW $REM "Ubuntu"  -j ACCEPT

-A FORWARD -i $I_INT -o $I_DMZ -d $WWW -p tcp --dport 80 $STATE_NEW $REM "www"  -j ACCEPT
-A OUTPUT            -o $I_DMZ -d $WWW -p tcp --dport 80 $STATE_NEW $REM "www"  -j ACCEPT


} && {
  test $(sysctl -n net.ipv4.ip_forward) -eq 0 && sysctl net.ipv4.ip_forward=1  #Forwarding aktivieren
        echo "ok"
  echo "GOT HERE - ok!" >> /tmp/fw.log
  exit 0
} || {
  test $(sysctl -n net.ipv4.ip_forward) -ne 0 && sysctl net.ipv4.ip_forward=0  #Forwarding de-aktivieren

echo "GOT HERE - not ok " >> /tmp/fw.log

This does not duplicate How to save rules of the iptables? Reason is that my Senior Admin insists that I use this /etc/firewall.sh script. Also the script runs fine but randomly fails.

Thank you for your comments so far. I would really appreciate any help if trying to find out the cause of this. Thanks. Bob

  • good lord who knows, you are running a VM and so netwroking is that much more complicated, and it sounds as if you are running custom scripts so who knows? Perhaps run the scripts all at once after both devices are up. My guess is you are making this more complicated then it has to be, there are way too many "iptables scripts" that are way too complex.
    – Panther
    Jul 14, 2017 at 19:53
  • Just use sudo apt-get install iptables-persistent
    – Panther
    Jul 14, 2017 at 19:53
  • 1
    it's not a good idea to post a work-in-progress question on a Stack Exchange site, especially one with high traffic. If your question lacks details, it's likely to be closed. If we left it open to wait for you to finish it, that wouldn't help you, because questions asked on Friday will be several pages back by Monday, and while editing does bump the post, it's only on the front page, which moves very fast. Editing your question after it's closed will send it to the reopen queue, and that will get some more eyes on it, but the edit has to be good enough to convince folks to reopen.
    – Zanna
    Jul 15, 2017 at 9:12
  • 3
    If your senior admins insist on a particular solution you should ask them for help with its implementation. Jul 17, 2017 at 9:51
  • This is another example of recreating the wheel when better solutions exits. If you were using iptables-persistent you would be able to file a bug report. As it is now, you have obscured your iptables rules by creating so many $VARIABLES it is hard to read the script. I really do not understand why people insist on making obscure iptables scripts, makes it difficult for people like me to debug =) .
    – Panther
    Jul 17, 2017 at 15:46

1 Answer 1


Your situation is complicated as you are running a VM and you seem to be using some sort of script and you are trying to call the script in your network pre/post up/down rules.

IMO most of the "scripts" to generate rules for iptables are overly complex and completely unnecessary. Most scripts may seem "smart" but often the only purpose is obscurification of iptables.

Easiest way to save/restore your rules is with iptables-persistent

sudo apt-get install iptables-persistent

Once you install the package it will restore your rules no matter if you manually entered them or generated them from some sort of script.

If you really want help with your script you need to post it as well as your configuration files.

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