Master iptables: Essential Commands, Installation, and Real-World Firewall Rules
This guide provides a comprehensive overview of iptables, covering installation, kernel module loading, core commands, rule syntax, practical examples for blocking ports, IPs, and networks, as well as saving and restoring configurations, helping administrators secure Linux firewalls effectively.
iptables Command Syntax Order
-L Show all rules of the selected chain; if no chain is specified, all chains are shown. Can be combined with -Z to list and zero counters.
-n Show numeric IPs and ports.
-t Specify table.
-A Append rule to the end of the chain.
-I Insert rule at the beginning of the chain.
-D Delete a rule.
-p Specify protocol (tcp, udp, icmp, all).
--dport Destination port.
--sport Source port.
-s Source IP.
-d Destination IP.
-m Specify module.
-i Input network interface.
-o Output network interface.
-j Target action (DROP, ACCEPT, REJECT, etc.).
-F Flush all rules.
-X Delete user‑defined chains.
-Z Zero counters.
# author : by www.yuchaoit.cn1. Install iptables
yum install iptables-services -y2. Load kernel modules
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state3. Start the firewall
# Stop firewalld and enable iptables
systemctl stop firewalld
systemctl disable firewalld
systemctl start iptables.service
systemctl enable iptables.service4. iptables Core Commands
1. View rules
# -n --numeric (numeric output)
# -L list rules
iptables -nL
# Example output omitted for brevity2. Flush rules
iptables -F # Delete all rules
iptables -X # Delete user‑defined chains
iptables -Z # Zero counters3. Add firewall rule
iptables -t filter -A INPUT -p tcp --dport 6379 -j DROP4. View connection states
NEW: new connection
ESTABLISHED: established connection
RELATED: related connection
INVALID: invalid or unrecognizable5. Delete a specific rule
iptables -nL --line-numbers
iptables -D INPUT 1 # delete rule number 1 in INPUT chain2. Rule Practice Scenarios
1. Block access to port 22
# Danger: use with caution
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP2. Block a specific IP on a specific interface
iptables -F
iptables -I INPUT -p tcp -s 10.0.0.52 -i ens33 -j DROP3. Use negation (!)
# Block all IPs except 10.0.0.53 on interface ens33
iptables -A INPUT -p tcp ! -s 10.0.0.53 -i ens33 -j DROP4. Allow only 10.0.0.0/24 network
iptables -A INPUT -p tcp ! -s 10.0.0.0/24 -j DROP5. Single entry point for bastion host
iptables -I INPUT -p tcp ! -s 10.0.0.61 -j DROP6. Match a range of ports
Allow only the 172.16.1.0/24 network to access ports 22, 6379, and 80.
iptables -I INPUT -p tcp ! -s 172.16.1.0/24 -m multiport --dport 22,6379,80 -j DROP7. Reject ping (ICMP echo request)
# REJECT provides feedback, DROP is silent
iptables -A INPUT -p icmp --icmp-type 8 -j REJECT8. Drop ping silently
iptables -A INPUT -p icmp --icmp-type 8 -j DROP9. Practice Exercises
# Example: block 10.0.0.51 from accessing the host
iptables -I INPUT -s 10.0.0.51 -j DROP
# Example: allow only 10.0.0.1 to SSH to bastion
iptables -I INPUT -p tcp -s 10.0.0.1 --dport 22 -j ACCEPT
# Example: block all access to port 6379
iptables -I INPUT -p tcp --dport 6379 -j DROP4. Save and Restore Rules
iptables-save > /opt/www.yuchaoit.cn_iptables.txt # Save to file
iptables-restore < /opt/www.yuchaoit.cn_iptables.txt # RestoreTips
1. Backup before changes.
2. Avoid locking yourself out.
3. Use temporary rules for testing.
4. If locked out, use cloud console or physical access to fix.Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
