You are here

Debian/Ubuntu: Loading iptables rules on boot

Submitted by Druss on Wed, 2012-07-04 03:30

If you've ever worked with Debian or Ubuntu servers, you've very likely had to set up a firewall at some point or the other. However, any changes that are made are not saved and loaded if the server is ever rebooted. The following is a quick guide on how to get this happening:

(root or sudo access is required)

  1. Create a directory in /etc named iptables.
  2. Navigate into the new directory.
  3. Assuming that the iptables rules are currently loaded as per your requirements, save them into a file using:
    iptables-save > iptables.rules
    This should save the rules in a file within our iptables directory.
  4. I usually also create another file here that clears all rules and other files if I have variable configurations that I would like to use.
  5. Now that we have our rules saved in a file, we can direct Debian to load them on start up. To do this, navigate to /etc/network/if-pre-up.d/ and create a file within named iptables.
  6. Edit this file and add the following lines to it:
    /sbin/iptables-restore < /etc/iptables/iptables.rules
  7. Save the file and exit the editor.
  8. Now, make sure that the server can actually execute this file by assigning the appropriate permissions:
    chmod +x iptables
  9. Restart the server.
  10. Upon reboot, confirm that the rules have been loaded via a iptables -L command.

I hope this helps!