How to save iptables firewall rules permanently on Linux

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

To save the iptables firewall rules permanently on Linux, you can use the iptables-save command to save the current rules to a file, and then use a script or systemd service to restore the rules at boot time.

Here are the steps to save iptables firewall rules permanently on Linux:

  1. First, use the iptables-save command to save the current rules to a file. You can use any file name and location, but it is recommended to use the default location for iptables rules, which is /etc/sysconfig/iptables on RedHat-based distributions and /etc/iptables/rules.v4 on Debian-based distributions.
sudo iptables-save > /etc/sysconfig/iptables    # for RedHat-based distributions
sudo iptables-save > /etc/iptables/rules.v4     # for Debian-based distributions

2. Once the rules are saved, create a script that will load the saved rules at boot time. You can use any text editor to create the script, but it is recommended to use a systemd service on newer systems. Here is an example systemd service that loads the saved iptables rules on boot:

[Unit]
Description=Load iptables rules

[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /etc/sysconfig/iptables     # for RedHat-based distributions
ExecStart=/sbin/iptables-restore /etc/iptables/rules.v4      # for Debian-based distributions

[Install]
WantedBy=multi-user.target

3. Save the script as /etc/systemd/system/iptables-restore.service and enable it with the following command:

sudo systemctl enable iptables-restore.service

4. Finally, reboot the system to verify that the saved iptables rules are loaded at boot time.

Related Posts

Journey Through Bihar: An Authentic Field Guide to Historic Sights, Arts, Food & Living Culture

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now There…

Read More

Experiencing Lucknow: The Ultimate City Guide to Happenings, Shows & Weekend Outings

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now While…

Read More

Best Knee Replacement Hospitals in India: A Clinical Insider’s Roadmap

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Living…

Read More

Events in Kolkata: Complete Guide to Upcoming Events, Tickets & Things to Do

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Kolkata…

Read More

The Indian Expat’s Handbook: Decoding Visa Requirements and Moving Abroad

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now Heading…

Read More

Navigating DevOps Training in China: Practical Skills, Infrastructure Automation, and Industry Certifications

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now When…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments