Skip to content

Save iptables Rules After Reboot

Haikel Fazzani Haikel Fazzani
2025-02-18
Updated Fri Jul 18 2025 00:00:00 GMT+0000 (Coordinated Universal Time)

This guide explains how to keep your Linux firewall rules (iptables) safe even after a reboot. We’ll use simple commands and examples for Ubuntu, CentOS 7, and other Linux systems.


Why Save iptables Rules?

iptables is like a security guard for your Linux system. It controls incoming and outgoing traffic. But by default, iptables rules disappear after a reboot. This guide shows you how to make them permanent.


3 Easy Ways to Save iptables Rules

1. Manual Method: Save and Restore Rules

This works on all Linux systems (Ubuntu, CentOS, etc.).

Steps:

  1. Save your current rules to a file:

    sudo iptables-save > /home/user/my_iptables_rules.txt  

    (Replace /home/user/ with your home folder.)

  2. Restore rules after reboot by adding this line to /etc/rc.local (a startup script):

    sudo iptables-restore < /home/user/my_iptables_rules.txt  

Example:

Fix Common Errors:


2. Ubuntu/Debian: Use iptables-persistent

This tool automatically saves and restores rules.

Steps:

  1. Install it:

    sudo apt update  
    sudo apt install iptables-persistent  
  2. During installation, say “Yes” to save current rules.

  3. To save rules later:

    sudo netfilter-persistent save  

Example:

Where Are Rules Stored?


3. CentOS 7: Use iptables-services

CentOS 7 uses firewalld by default, but you can switch to iptables.

Steps:

  1. Stop and disable firewalld:

    sudo systemctl stop firewalld  
    sudo systemctl disable firewalld  
  2. Install iptables services:

    sudo yum install iptables-services  
  3. Save rules:

    sudo service iptables save  
  4. Start iptables on boot:

    sudo systemctl enable iptables  
    sudo systemctl start iptables  

Example:


Troubleshooting

Problem 1: Rules Disappear After Reboot

Problem 2: “iptables-save Permission Denied”

Problem 3: Conflict with firewalld (CentOS)


Best Practices

  1. Backup Rules: Save rules before making changes.
    sudo iptables-save > /home/user/backup_rules.txt  
  2. Test Rules: Apply rules temporarily (sudo iptables-restore < file.txt) and test before saving.
  3. Add Comments: Use -m comment --comment "My rule" to explain rules.

Conclusion

Now you know how to save iptables rules permanently on any Linux system! Use:

For more help, check:


save iptables rules iptables

More Insights.