How can set these iptables rules to run at startup

You may want to use the iptables-persistent package rather than mess with your boot scripts. First, run your script to set up the firewall rules. Secondly, run sudo apt-get install iptables-persistent, and follow the prompts. When it asks to save the current rules, hit "Yes" at both prompts. Now, on reboots, your iptables rules will be restored.


NOTE: If you change your rules after this, you will need to do the following command(s) after the changes:

To save your IPv4 iptables rules: sudo su -c 'iptables-save > /etc/iptables/rules.v4'

To save your IPv6 ip6tables rules: sudo su -c 'ip6tables-save > /etc/iptables/rules.v6'


Assuming you have the firewall rules in:

/etc/iptables.up.rules

Perhaps the most obvious answer will be to create a file called iptables in:

/etc/network/if-pre-up.d

with the content:

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules

and make it executable using

sudo chmod +x /etc/network/if-pre-up.d/iptables

This way before your network interface is activated your rules will be loaded.