Debian 8 – Update Iptables on boot

debianiptablesstartup

I am in the process of porting my "configure openvpn server" from ubuntu 14 to debian 8. So far it works well except for this section:

# Set up iptables to forward packets for vpn and do this upon startup.
echo 'iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
iptables -A FORWARD -j REJECT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
exit 0' > /etc/rc.local

It appears that debian 8 does not use this /etc/rc.local file so my vpn server won't forward traffic correctly after a reboot. I have to manually call that script, or execute the commands.

What is the "debian 8 way" for updating the iptables on boot?


Update

After reading that /etc/rc.local should work, I made sure the permissions were set to 755 and updated the script to as follows:

/bin/echo "starting..." > /root/rc.local.log
/sbin/iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
/sbin/iptables -A FORWARD -j REJECT
/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
/bin/echo "completed successfully" >> /root/rc.local.log

exit 0

I then created an empty /root/rc.local.log and gave it 777 permissions before rebooting. The file remained empty, making me think that the /etc/rc.local script is not being executed at all.

Best Answer

The Debian way of setting up iptables on boot is by using the iptables-persistent package.

Simply install the iptables-persistent package, set up the iptables rules like you want them, and then run netfilter-persistent save. (Note that the command starts with netfilter and not iptables.)

See the man page for netfilter-persistent for more details.

The method for saving the tables has changes since Debian 7 (Wheezy). In Wheezy one would do something like: invoke-rc.d iptables-persistent save.

Related Question