Home Server How to make iptables persistent after reboot on Linux

How to make iptables persistent after reboot on Linux

by Abraham
iptables persistent after reboot on linux

iptables is a user-space utility software that allows a system administrator to customize the Linux kernel firewall’s IP packet filter rules, which are implemented as various Netfilter modules. The filters are structured into tables with chains of rules governing how network traffic packets are treated. iptables is a powerful utility for configuring port access on your PC or server. It gives the necessary control to specify what network traffic is allowed or denied to the system.

To set up, manage, and analyze the firewall tables of IPv4 and IPv6 packet filter rules in the Linux kernel, system administrators and developers utilize the iptables and ip6tables programs. Unfortunately, any changes performed with these commands are lost when the Linux server is rebooted. As a result, we must persistently preserve those rules across reboots. This article demonstrates how to permanently preserve iptables firewall rules on an Ubuntu Linux system.

Experienced Linux administrators are probably familiar with the annoyance and suffering of a reboot that entirely deletes a system’s iptables rules. This is because, by default, iptables rules do not remain after a reboot. After establishing your system’s iptables rules, you must take one more step to ensure the rules stay in place after a reboot.

Making iptables persistent after reboot on Linux

This guide will illustrate how to make iptables rules durable after rebooting your Ubuntu and Cent OS.

Before we begin, ensure you have specific rules set up on your system. This tutorial assumes that you have already configured the rules with iptables. Type to see a list of rules:

sudo iptables -L
list iptables rules

List iptables rules

This should show you the current status of all the access and block rules you’ve set up on your system.

How to save iptables rules on Ubuntu?

Install the iptables-persistent package using the apt package manager to make your iptables rules permanent after reboot:

sudo apt-get install iptables-persistent
install iptables persistent

Install iptables-persistent

Any presently mentioned iptables rules would be stored in the IPv4 and IPv6 files indicated below:

/etc/iptables/rules.v4

/etc/iptables/rules.v6

To integrate new rules in your system, just use the iptables command to update persistent iptables. Run the iptables-save command to make modifications permanent after a reboot:

sudo iptables-save > /etc/iptables/rules.v4

sudo ip6tables-save > /etc/iptables/rules.v6
save iptables rules

Save iptables rules

To remove persistent iptables rules, open the relevant /etc/iptables/rules.v* file and delete any undesired rules.

How to save iptables rules on CentOS

Install the iptables-services package using the dnf package manager to make your iptables rules persistent after reboot:

sudo dnf install iptables-services

Any current iptables rules will be stored in the IPv4 and IPv6 files listed below:

/etc/sysconfig/iptables

/etc/sysconfig/ip6tables

Make sure that the firewalld service is disabled, and the iptables service is enabled in the system by executing the lines of code provided below:

sudo systemctl stop firewalld

sudo systemctl disable firewalld

sudo systemctl start iptables

sudo systemctl enable iptables

You can then use the following command to ensure that the service is running:

sudo systemctl status iptables

To integrate new rules in your system, just use the iptables command to update persistent iptables. Run the iptables-save command to make modifications permanent after a reboot:

sudo iptables-save > /etc/sysconfig/iptables

sudo ip6tables-save > /etc/sysconfig/ip6tables

Note: To remove persistent iptables rules, edit the corresponding /etc/sysconfig/iptables or /etc/sysconfig/ip6tables file and delete any lines that include undesired rules.

Conclusion

This post taught you how to permanently save and restore iptables rules on Linux, specifically on Debian/Ubuntu or CentOS/RHEL/Rocky/Alma Linux systems. Remember to configure the Linux firewall with only one service. Many systems now have their own iptables front end, such as firewalld or ufw, which makes the firewall more user pleasant and saves your rules by default. I hope you are now able to make your iptables persistent. Thanks for reading.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.