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)
- Create a directory in
/etc
namediptables
. - Navigate into the new directory.
- 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. - 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.
- 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 namediptables
. - Edit this file and add the following lines to it:
#!/bin/bash
/sbin/iptables-restore < /etc/iptables/iptables.rules - Save the file and exit the editor.
- Now, make sure that the server can actually execute this file by assigning the appropriate permissions:
chmod +x iptables
- Restart the server.
- Upon reboot, confirm that the rules have been loaded via a
iptables -L
command.
I hope this helps!
- Log in to post comments