Today I'm going to show you how to make your iptables rule sets more readable with comments and also how to make sure iptables rules persistent or stay put after you reboot the system. Right now all these rules are only in the kernel when you reboot they'll all disappear unless you find a way to save.
Difference between Iptables and UFW in Ubuntu 22.04
Iptables is a kernel level ip filtering mechanism. It does allow you to make routing decisions and so on on IP packets, whereas UFW is a simplified firewall mechanism that is implemented on top of iptables. UFW is not as flexible but is easier to configure for common scenarios.
UFW provides a basic default firewall and allows you to easily turn on and off basic services. To use IPtables you need to understand TCP/IP connections, more complicated protocols (e.g. NFS) and it can still be complicated.
While UFW is not capable of some of the things that iptables is it is a much simplified firewall and if it provides the capabilities that you need it is much simpler to configure and easier to have confidence that it is doing what you actually want.
Add comments to Iptables rules
Comments make your rule sets more readable so let's get rid of this SSH rule and add it back with a comment so first will delete the rule. And then we'll add it back with a comment. To add a comment all you have to do is add the comment module
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT -m comment --comment 'allow ssh for all'
Make Iptables rules persistent on reboot
if you're running debian or its derivatives you need to install the IP tables persistent package from the apt repository.
sudo apt install iptables-persistent
during the process of installation it will ask you to save ipv4 and ipv6 rules to be used when rebooting the system, you can say yes to save the files or No to save the rules manually using the following commands
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
sudo sh -c "ip6tables-save > /etc/iptables/rules.v6"
Reboot your system and check the iptables rules
sudo iptables -nL
you should see all your rules intact.
Also to refer similar articles on Ubuntu and wordpress visit the following page
Recent Comments