Ipables
@woozer:/etc/nginx/sites-available » sudo iptables -L -nv --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 730K 360M ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
2 0 0 REJECT all -- !lo * 127.0.0.0/8 0.0.0.0/0 reject-with icmp-port-unreachable
3 3515 351K ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 3
4 856 45500 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
5 336 25775 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 11
6 6308 268K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
7 226K 23M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
8 976K 534M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
9 12568 1520K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3000
10 332K 238M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:4791
11 14312 1043K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
12 3988 316K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8888
13 4051 300K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8181
14 7476 4796K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8008
15 432 45268 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8282
16 0 0 ACCEPT all -- * * 96.126.119.66 0.0.0.0/0 state NEW
17 69460 114M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
18 98440 6129K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables_INPUT_denied: "
19 103K 6474K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables_FORWARD_denied: "
2 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT 301K packets, 271M bytes)
num pkts bytes target prot opt in out source destination
@woozer:/etc/nginx/sites-available »
Control Network Traffic with iptables
Updated Wednesday, February 28, 2017 by Linode
Use promo code DOCS10 for $10 credit on a new account.
iptables is an application that allows users to configure specific rules that will be enforced by the kernel's netfilter framework. It acts as a packet filter and firewall that examines and directs traffic based on port, protocol and other criteria. This guide will focus on the configuration and application of iptables rulesets and will provide examples of ways they are commonly used.
![Control Network Traffic with iptables][1]
By default, the iptables tool is included with your Linode-supplied distribution. In order to use iptables, you will need root (sudo) privileges.
Use Linux iptables to Manage IPv4 Traffic
The iptables Command
Many options can be used with the iptables command. As stated above, iptables sets the rules that control network traffic. You can define different tables to handle these rules through chains, lists of rules that match a subset of packets. The table contains a variety of built-in chains, but you can add your own.
Basic iptables Parameters and Syntax
Before we begin creating rules, let's review the syntax of an iptables rule.
For example, the following command adds a rule to the beginning of the chain that will drop all packets from the address 198.51.100.0:
| ----- | |
1
|
iptables -I INPUT -s 198.51.100.0 -j DROP
|
The sample command above:
- Calls the
iptablesprogram - Uses the
-Ioption for insertion. Using a rule with the insertion option will add it to the beginning of a chain and will be applied first. To indicate a specific placement in the chain, you may also use a number with the-Ioption. - The
-sparameter, along with the IP address (198.51.100.0), indicates the source. - Finally, the
-jparameter stands for jump. It specifies the target of the rule and what action will be performed if the packet is a match.
| Parameter | Description |
|---|---|
-p, --protocol | The protocol, such as TCP, UDP, etc. |
-s, --source | Can be an address, network name, hostname, etc. |
-d, --destination | An address, hostname, network name, etc. |
-j, --jump | Specifies the target of the rule; i.e. what to do if the packet matches. |
-g, --goto chain | Specifies that the processing will continue in a user-specified chain. |
-i, --in-interface | Names the interface from where packets are received. |
-o, --out-interface | Name of the interface by which a packet is being sent. |
-f, --fragment | The rule will only be applied to the second and subsequent fragments of fragmented packets. |
-c, --set-counters | Enables the admin to initialize the packet and byte counters of a rule. |
Default Tables
Tables are made up of built-in chains and may also contain user-defined chains. The built-in tables will depend on the kernel configuration and the installed modules.
The default tables are as follows:
- Filter - This is the default table. Its built-in chains are:
- Input: packets going to local sockets
- Forward: packets routed through the server
- Output: locally generated packets
- Nat - When a packet creates a new connection, this table is used. Its built-in chains are:
- Prerouting: designating packets when they come in
- Output: locally generated packets before routing takes place
- Postrouting: altering packets on the way out
- Mangle - Used for special altering of packets. Its chains are:
- Prerouting: incoming packets
- Postrouting: outgoing packets
- Output: locally generated packets that are being altered
- Input: packets coming directly into the server
- Forward: packets being routed through the server
- Raw - Primarily used for configuring exemptions from connection tracking. The built-in chains are:
- Prerouting: packets that arrive by the network interface
- Output: processes that are locally generated
- Security - Used for Mandatory Access Control (MAC) rules. After the filter table, the security table is accessed next. The built-in chains are:
- Input: packets entering the server
- Output: locally generated packets
- Forward: packets passing through the server
Basic iptables Options
There are many options that may be used with the iptables command:
| Option | Description |
|---|---|
-A --append | Add one or more rules to the end of the selected chain. |
-C --check | Check for a rule matching the specifications in the selected chain. |
-D --delete | Delete one or more rules from the selected chain. |
-F --flush | Delete all the rules one-by-one. |
-I --insert | Insert one or more rules into the selected chain as the given rule number. |
-L --list | Display the rules in the selected chain. |
-n --numeric | Display the IP address or hostname and post number in numeric format. |
-N --new-chain | Create a new user-defined chain. |
-v --verbose | Provide more information when used with the list option. |
-X --delete-chain | Delete the user-defined chain. |
Insert, Replace or Delete iptables Rules
iptables rules are enforced top down, so the first rule in the ruleset is applied to traffic in the chain, then the second, third and so on. This means that rules cannot necessarily be added to a ruleset with iptables -A or ip6tables -A. Instead, rules must be inserted with iptables -I or ip6tables -I.
Insert
Inserted rules need to be placed in the correct order with respect to other rules in the chain. To get a numerical list of your iptables rules:
| ----- | |
1
|
sudo iptables -L -nv --line-numbers
|
For example, let's say you want to insert a rule into the [basic ruleset][2] provided in this guide, that will accept incoming connections to port 8080 over the TCP protocol. We'll add it as rule 7 to the INPUT chain, following the web traffic rules:
| ----- | |
1
|
sudo iptables -I INPUT 7 -p tcp --dport 8080 -m state --state NEW -j ACCEPT
|
If you now run sudo iptables -L -nv again, you'll see the new rule in the output.
Replace
Replacing a rule is similar to inserting, but instead uses iptables -R. For example, let's say you want to reduce the logging of denied entries to only 3 per minute, down from 5 in the original ruleset. The LOG rule is ninth in the INPUT chain:
| ----- | |
1
|
sudo iptables -R INPUT 9 -m limit --limit 3/min -j LOG --log-prefix "iptables_INPUT_denied: " --log-level 7
|
Delete
Deleting a rule is also done using the rule number. For example, to delete the rule we just inserted for port 8080:
| ----- | |
1
|
sudo iptables -D INPUT 7
|
Editing rules does not automatically save them. See our section on [deploying rulesets][3] for the specific instructions for your distribution.
View Your Current iptables Rules
IPv4:
IPv6:
On most distributions, iptables has no default rules for either IPv4 and IPv6. As a result, on a newly created Linode you will likely see what is shown below - three empty chains without any firewall rules. This means that all incoming, forwarded and outgoing traffic is allowed. It's important to limit inbound and forwarded traffic to only what's necessary.
| ----- | |
1 2 3 4 5 6 7 8
|
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
|
Configure iptables
iptables can be configured and used in a variety of ways. The following sections will outline how to configure rules by port and IP, as well as how to blacklist (block) or whitelist (allow) addresses.