iptables Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
iptables is a powerful user-space utility for Linux that allows administrators to configure, manage, and maintain the IP packet filter rules of the Linux kernel's Netfilter framework. It provides a flexible, stateful packet filtering and Network Address Translation (NAT) system to secure and control network traffic.
Linux iptables Command Syntax
The syntax for the iptables command is as follows:
Where:
- table: Specifies the packet filtering table to be used. The default table is 'filter'. Other options are 'nat', 'mangle', and 'raw'.
- chain: Specifies the chain in the selected table to manipulate. Common chains are 'INPUT', 'OUTPUT', 'FORWARD', 'PREROUTING', and 'POSTROUTING'.
iptables Command Options:
- -A: Append a rule to the specified chain.
- -D: Delete a rule from the specified chain.
- -I: Insert a rule in the specified chain at the specified position.
- -R: Replace a rule in the specified chain at the specified position.
- -L: List all rules in the specified chain or all chains.
- -F: Flush (delete) all rules in the specified chain or all chains.
- -Z: Zero (reset) the packet and byte counters in the specified chain or all chains.
Example Usages
-
List all rules in the 'filter' table:
Output:
Explanation: This command lists all the rules in the 'filter' table, showing the default policies for the INPUT, FORWARD, and OUTPUT chains.
-
Block all incoming traffic from a specific IP address:
Explanation: This command appends a rule to the INPUT chain, dropping all incoming packets from the source IP address 192.168.1.10.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Tips
- To make iptables rules persistent across reboots, use the 'iptables-save' and 'iptables-restore' commands or the 'iptables-persistent' package.
Advanced Use Cases of iptables Command in Linux
-
Allow SSH access from a specific IP address only:
Explanation: This command appends a rule to the INPUT chain, allowing incoming SSH (port 22) connections only from the source IP address 192.168.1.10.
-
Limit the rate of incoming connections:
Explanation: This command appends a rule to the INPUT chain, limiting the rate of incoming connections to 1 per second, with a burst allowance of 5 connections, for TCP traffic on port 80.
-
Set up a basic NAT:
Explanation: This command appends a rule to the POSTROUTING chain of the 'nat' table, enabling basic Network Address Translation (NAT) for outgoing traffic on the eth0 network interface.
Turn Learning into Career Growth
Conclusion
-
iptables is a powerful and flexible packet filtering tool for Linux.
-
It can be used for securing and controlling network traffic.
-
iptables rules can be made persistent across reboots.