iptables-restore — Restore firewall rules
Linux / Network and Firewall / iptables-restore.html
Reads an iptables-save file and applies the whole rule set at once.
Basic Information
| Command Name | iptables-restore |
|---|---|
| One-line Introduction | Reads an iptables-save file and applies the whole rule set at once. |
| Provided by | iptables (netfilter project) |
| Official Documentation | https://man7.org/linux/man-pages/man8/iptables-restore.8.html |
| Supported Operating Systems | Linux |
| Installation Status | Installed with the iptables package |
| Category | Linux networking (tags: Address, port and firewall management (tags: firewall, restore, rule, iptables)) |
What is this command?
iptables-restore reads a rule set and replaces the tables with it.
By default it flushes existing rules, which can lock you out of a remote host.
What problems does it solve?
- Roll back to a backed-up policy.
- Deploy identical rules to many hosts.
- Apply rules automatically at boot.
- Recover rules removed by mistake.
Key features
Restore from file
Point it at a backup file to replace the rules.
- When to use: When a change broke something.
- What you get: The rules from the file take effect.
- Limitations: Current rules are lost, so remote work needs care.
Merge mode
With -n existing rules are kept and new ones are added.
- When to use: When adding only a few rules.
- What you get: Rules are layered on the existing policy.
- Limitations: Duplicate rules can accumulate.
Check before apply
Use -t to test syntax without applying.
- When to use: Before applying on a production host.
- What you get: Errors are reported without changing anything.
- Limitations: Correct syntax can still be a risky policy.
Installation and usage
Check whether it is installed
iptables-restore --version
sudo apt install iptables
Real-world examples
Restore backup
Re-apply a saved rule set.
sudo iptables-restore < /root/iptables.backup
Merge rules
Add rules without flushing.
sudo iptables-restore -n < /root/extra.rules
Syntax test
Validate syntax without applying.
sudo iptables-restore -t < /root/iptables.backup