Linux Firewall Tutorial: Iptables Tables, Chains, Rules Fundamentals
Linux Firewall Tutorial: Iptables Tables, Chains, Rules Fundamentals
Linux Firewall Tutorial: Iptables Tables, Chains, Rules Fundamentals
Fundamentals
iptables firewall is used to manage packet filtering and NAT rules. IPTables comes with all Linux
distributions. Understanding how to setup and configure iptables will help you manage your Linux
firewall effectively.
iptables tool is used to manage the Linux firewall rules. At a first look, iptables might look complex
(or even confusing). But, once you understand the basics of how iptables work and how it is
structured, reading and writing iptables firewall rules will be easy.
This article is part of an ongoing iptables tutorial series. This is the 1st article in that series.
This article explains how iptables is structured, and explains the fundamentals about iptables
tables, chains and rules.
On a high-level iptables might contain multiple tables. Tables might contain multiple chains.
Chains can be built-in or user-defined. Chains might contain multiple rules. Rules are defined for
the packets.
So, the structure is: iptables -> Tables -> Chains -> Rules. This is defined in the following diagram.
Just to re-iterate, tables are bunch of chains, and chains are bunch of firewall rules.
1. Filter Table
Filter is default table for iptables. So, if you dont define you own table, youll be using filter table.
Iptabless filter table has the following built-in chains.
INPUT chain Incoming to firewall. For packets coming to the local server.
OUTPUT chain Outgoing from firewall. For packets generated locally and going out of the local
server.
FORWARD chain Packet for another NIC on the local server. For packets routed through the local
server.
2. NAT table
Iptables NAT table has the following built-in chains.
PREROUTING chain Alters packets before routing. i.e Packet translation happens immediately
after the packet comes to the system (and before routing). This helps to translate the destination ip
address of the packets to something that matches the routing on the local server. This is used for DNAT
(destination NAT).
POSTROUTING chain Alters packets after routing. i.e Packet translation happens when the
packets are leaving the system. This helps to translate the source ip address of the packets to something
that might match the routing on the desintation server. This is used for SNAT (source NAT).
3. Mangle table
Iptabless Mangle table is for specialized packet alteration. This alters QOS bits in the TCP header.
Mangle table has the following built-in chains.
PREROUTING chain
OUTPUT chain
FORWARD chain
INPUT chain
POSTROUTING chain
4. Raw table
Iptables Raw table is for configuration excemptions. Raw table has the following built-in chains.
PREROUTING chain
OUTPUT chain
If the criteria is matched, it goes to the rules specified in the target (or) executes the special values
mentioned in the target.
Target Values
Following are the possible special values that you can specify in the target.
RETURN Firewall will stop executing the next set of rules in the current chain for this packet. The
control will be returned to the calling chain.
If you do iptables list (or) service iptables status, youll see all the available firewall rules on your
system. The following iptable example shows that there are no firewall rules defined on this
system. As you see, it displays the default input table, with the default input chain, forward chain,
and output chain.
target
destination
target
destination
target
destination
Note: If you dont specify the -t option, it will display the default filter table. So, both of the
following commands are the same.
(or)
# iptables --list
The following iptable example shows that there are some rules defined in the input, forward, and
output chain of the filter table.
# iptables --list
num target
destination
0.0.0.0/0
num target
destination
0.0.0.0/0
num target
destination
num target
ACCEPT
all -- 0.0.0.0/0
ACCEPT
icmp -- 0.0.0.0/0
ACCEPT
esp -- 0.0.0.0/0
0.0.0.0/0
ACCEPT
ah -- 0.0.0.0/0
0.0.0.0/0
ACCEPT
udp -- 0.0.0.0/0
224.0.0.251
ACCEPT
udp -- 0.0.0.0/0
0.0.0.0/0
ACCEPT
tcp -- 0.0.0.0/0
ACCEPT
all -- 0.0.0.0/0
ACCEPT
tcp -- 0.0.0.0/0
10 REJECT
all -- 0.0.0.0/0
destination
0.0.0.0/0
0.0.0.0/0
0.0.0.0/0
0.0.0.0/0
0.0.0.0/0
0.0.0.0/0
udp dpt:5353
udp dpt:631
tcp dpt:631
state RELATED,ESTABLISHED
reject-with icmp-host-prohibited
The rules in the iptables list command output contains the following fields:
Lets Change
-A is for append. If it makes it easier for you to remember -A as add-rule (instead of appendrule), it is OK. But, keep in mind that -A adds the rule at the end of the chain.
Again, it is very important to remember that -A adds the rule at the end.
Typically the last rule will be to drop all packets. If you already have a rule to drop all packets, and
if you try to use -A from the command-line to create new rule, you will end-up adding the new
rule after the current drop all packets rule, which will make your new rule pretty much useless.
Once youve mastered the iptables, and when you are implementing it on production, you should
use a shell script, where you use -A command to add all the rules. In that shell script, your last line
should always be drop all packets rule. When you want to add any new rules, modify that shell
script and add your new rules above the drop all packets rule.
Syntax:
-A chain Specify the chain where the rule should be appended. For example, use INPUT chain for
incoming packets, and OUTPUT for outgoing packets.
If you dont know what chain means, you better read about iptables fundamentalsfirst.
-p is for protocol
Use all to allow all protocols. When you dont specify -p, by default all protocols will be used. It is
not a good practice to use all, and always specify a protocol.
Use either the name (for example: tcp), or the number (for example: 6 for tcp) for protocol.
-s is for source
For network mask use /mask. For example: -s 192.168.1.0/24 represents a network mask of
255.255.255.0 for that network. This matches 192.168.1.x network.
-d is for destination
-j is target
This specifies what needs to happen to the packet that matches this firewall rule.
You can also specify other user defined chain as target value.
-i is for in interface
You might over look this and assume that -i is for interface. Please note that both -i and -o are for
interfaces. However, -i for input interface and -o for output interface.
Indicates the interface through which the incoming packets are coming through the INPUT,
FORWARD, and PREROUTING chain.
For example: -i eth0 indicates that this rule should consider the incoming packets coming through
the interface eth0.
If you dont specify -i option, all available interfaces on the system will be considered for input
packets.
Indicates the interface through which the outgoing packets are sent through the INPUT,
FORWARD, and PREROUTING chain.
If you dont specify -o option, all available interfaces on the system will be considered for output
packets.
Note: All of these options have two dashes in front of them. For example, there are two hyphens in
front of sport.
You can specify either the port number or the name. For example, to use SSH port in your firewall
rule, use either sport 22 or sport ssh.
Using port number in the rule is better (for performance) than using port name.
To match range of ports, use colon. For example, 22:100 matches port number from 22 until 100.
Possible values are: SYN, ACK, FIN, RST, URG, PSH. You can also use ALL or NONE
When you use icmp protocol -p icmp, you can also specify the ICMP type using icmp-type
parameter.
For example: use icmp-type 0 for Echo Reply, and icmp-type 8 for Echo.
WARNING: Playing with firewall rules might render your system inaccessible. If you dont know
what you are doing, you might lock yourself (and everybody else) out of the system. So, do all your
learning only on a test system that is not used by anybody, and you have access to the console to
restart the iptables, if you get locked out.
-A INPUT This indicates that we are appending a new rule (or adding) to the INPUT chain. So,
this rule is for incoming traffic.
-i eth0 Incoming packets through the interface eth0 will be checked against this rule.
-p tcp dport 22 This rule is for TCP packets. This has one tcp option called dport 22, which
indicates that the destination port for this rule on the server is 22 (which is ssh).
In simple terms the above rule can be stated as: All incoming packets through eth0 for ssh will be
accepted.
# iptables -L
target
ACCEPT
DROP
tcp -- anywhere
all -- anywhere
destination
anywhere
tcp dpt:ssh
anywhere
As you see from the above output, it has the following two rules in sequence.
Instead of adding the firewall rules from the command line, it might be better to create a shell
script that contains your rules as shown below.
# vi iptables.sh
# sh -x iptables.sh
# iptables -L INPUT
target
ACCEPT
DROP
tcp -- anywhere
all -- anywhere
destination
anywhere
tcp dpt:ssh
anywhere
Similar to iptables append/add command, there are few other commands available for iptables. Ill
cover them in the upcoming articles in the iptables series. Ill also provide several practical firewall
rule examples that will be helpful in real life scenarios.
iptables --flush