Practical iptables basics: save/restore rules, list/flush, set default policies, allow established connections, remove rules by line, raw table viewing, restrict inbound to specific subnets/ports with logging, and NAT port-forward examples. Includes IPv6 equivalents (ip6tables).
-
IPv6 firewall uses
ip6tables(legacy)ip6tables -L -v --line-numbers
Same concepts as iptables, but applies to IPv6 traffic.
-
Modern alternative (often preferred):
nftablesMany distros now use nftables; iptables commands may be compatibility wrappers.
-
iptables-save -c > fileExports current IPv4 rules to
fileincluding packet/byte counters (-c).
(Youriptables-save -c filecorrected: output is written to stdout, so redirect it.) -
iptables-restore < fileRestores rules from
file.
(Youriptables-restore filecorrected: it reads from stdin.)
IPv6 equivalents
ip6tables-save -c > file.v6
ip6tables-restore < file.v6-
Lists all rules in filter table with verbose counters and line numbers.
iptables -L -v --line-numbers
Useful add-on:
iptables -SShows rules in a format closer to what you typed.
-
Flushes (deletes) rules in the current table (default
iptables -F
filter).
To flush NAT too:
iptables -t nat -F-
Sets chain default policy to ACCEPT/REJECT/DROP.
iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT
To set a default drop stance:
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP-
Allows return traffic for connections you already permitted.
iptables -A INPUT -i interface -m state --state RELATED,ESTABLISHED -j ACCEPT
(Spelling corrected:ESTABLISHED.)
Modern match module (preferred):
iptables -A INPUT -i interface -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT-
Deletes rule number 7 in the INPUT chain.
iptables -D INPUT 7
-
Lists rules in the raw table (often used for NOTRACK / conntrack tuning).
iptables -t raw -L -n
Note: “disabling statefulness” is done by adding NOTRACK rules in raw table, not just listing it.
-
Sets default inbound policy to DROP (blocks anything not explicitly allowed).
iptables -P INPUT DROP
-
iptables -A OUTPUT -o iface -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
Allows new and established outbound SSH connections.
-
iptables -A INPUT -i iface -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
Allows inbound packets that belong to established SSH connections (server replies).
(Conntrack form)
iptables -A OUTPUT -o iface -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i iface -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT-
iptables -A OUTPUT -o iface -p icmp --icmp-type echo-request -j ACCEPT
Allows outbound ping requests.
-
iptables -A INPUT -i iface -p icmp --icmp-type echo-reply -j ACCEPT
Allows inbound ping replies.
(Your original had -i/-o swapped; corrected.)
-
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
Enables routing between interfaces (temporary).
-
sudo sysctl -w net.ipv4.ip_forward=1
Same via sysctl.
-
Traffic hitting
iptables -t nat -A PREROUTING -i eth0 -p tcp -d pivotip --dport 443 -j DNAT --to-destination attkip:443
pivotip:443oneth0is forwarded toattkip:443.
-
Rewrites source address so replies route back through the pivot.
iptables -t nat -A POSTROUTING -o eth0 -p tcp -s targetsubnet/cidr -d attkip --dport 443 -j SNAT --to-source pivotip
Common alternative when pivot has dynamic IP:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE-
Inserts an allow rule at the top of FORWARD chain.
iptables -t filter -I FORWARD 1 -j ACCEPT
-
iptables -A INPUT -s 1.1.1.0/24 -p tcp -m multiport --dports 80,443 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
Allows HTTP/HTTPS from that subnet (new and established traffic).
(Your chain name typo corrected:INPUT; andmultipartcorrected tomultiport.) -
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
Allows return traffic for anything already permitted.
-
iptables -P INPUT DROP
Drops everything else not allowed.
-
iptables -A OUTPUT -o eth0 -j ACCEPT
Allows outbound traffic on eth0.
-
iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT
Allows loopback traffic.
-
iptables -N LOGGING
Creates a custom chain named LOGGING.
-
iptables -A INPUT -j LOGGING
Sends INPUT traffic (that reaches this rule) into LOGGING chain.
-
iptables -A LOGGING -m limit --limit 4/min -j LOG --log-prefix "DROPPED "Logs at most 4 messages per minute with prefix “DROPPED ” (prevents log flooding).
-
iptables -A LOGGING -j DROP
Drops packets after logging.
Where logs go: typically kernel log (/var/log/kern.log, /var/log/messages, or journald). Exact location depends on rsyslog/systemd config.
iptables -t nat -L -v --line-numbers
iptables -t mangle -L -v --line-numberssudo apt install iptables-persistent
sudo netfilter-persistent save- List:
ip6tables -L -v --line-numbers
- Default drop inbound:
ip6tables -P INPUT DROP
- Allow established:
ip6tables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT