I currently have this snippet:
# flush all chains
iptables -F
iptables -t nat -F
iptables -t mangle -F
# delete all chains
iptables -X
Is there a possibility that some impervious rule will stay alive after running this?
The idea is to have a completely clean iptables config, that can be easily replaced by new ruleset (nevermind routes/ifconfig's parameters).
To answer your question succinctly, no: there would not be any "leftover" rules after flushing every table. In the interest of being thorough however, you may want to set the policy for the built-in
INPUT
andFORWARD
chains toACCEPT
, as well:Clear ip6tables rules:
...and that should do it.
iptables -nvL
should produce this (or very similar) output:This will correctly totally reset your iptables system to a very basic state:
All policies will be reset to ACCEPT as well as flushing every table in current use. All chains other than the built in chains will no longer exist.
One can do this in 1 or 2 commands:
Result:
Backups configuration to iptables_backup.conf and clean all rules.
To restore previous configuration:
Whenever I need the firewall disabled is something like this:
iptables-save > iptables.bak
service iptables stop
(i'm on fedora)You can just unload
iptables
' modules from the kernel:UPD Unfortunately, too good to be true. As long as there's a rule or a user-defined chain in a table, corresponding module's reference count is 1, and
modprobe -r
fails. You might delete rules and user-defined chains like so:or:
Also, you might want to unload modules this way (no hardcoding module names):
On the bright side, after this
iptables-save
produces nice empty output :)I've had to block all connections recently what I ended up doing was
as for saving I'd recommend the following
Ubuntu:
RedHat/CentOS:
In addition to backup all current ufw rules Ive used this in the past
I think this may be useful for future reference. Thought I would share.
This worked for me (on Ubuntu 18.04):
It resets (and disables) ufw and then resets iptables clearing and removing all chains. Then it enables the ufw again, but not before it allows port 22 for remote access. The two commands that require user confirmation are "forced" ensuring no input is required. I was able to run this over an active SSH connection.
(source)
Here is how I remove all DROP rules:
Done!