Iptables: Configuring/Modifying Iptables
Iptables: Configuring/Modifying Iptables
Iptables: Configuring/Modifying Iptables
md 7/2/2020
IPTables
IPTables: a linux firewall system used to allow or block traffic into a network. IPTables uses the chain
concept. A chain is a rule, and these rules fall under INPUT, FORWARD and OUTPUT
INPUT chain: controls incoming packets. This is where you can block or allow incoming/new
connections.
FORWARD chain: controls packets that need to be forwarded/routed to a different end location
OUTPUT chain: controls outgoing packets and connections. The output chain is used to return the
data back to the requester
Configuring/Modifying IPTables
IPTables Verbs
ACCEPT means to let the packet through.
DROP means to drop the packet on the floor.
REJECT blocks an attempted connection and sends an error message to the client (connection
source)
RETURN means stop traversing this chain and resume at the next rule in the previous (calling)
chain. If
-i (interface): the network interface that you want its traffic filtered. Can be one of, eth0, lo, ppp0,
wlp2o, etc. You can get these interfaces by running ifconfig
-p (protocol): the protocol whose traffic is to be filtered
-s (source): The source IP address or hostname of the traffic
-dport (destination port): the destination port number of the protocol e.g. 22, 443, 80, etc
-j (target): the target name (ACCEPT, DROP or RETURN).
The protocol and the corresponding must always be specified together in the same command.
sudo iptables -L
The command would return the current iptables config for the different chains like
1/4
iptables.md 7/2/2020
2/4
iptables.md 7/2/2020
From this output, all chains (INPUT, FORWARD and OUTPUT) are configured to ACCEPT. However if one say
FORWARD was not configured to accept, then you could change that using the command
Only when a chain is configured to ACCEPT all connections can you then control access by blocking or
forwarding IP addresses and/or port numbers.
However in a case when you only want to allow very specific IP Addresses, the default behavior would be to
DROP all connections and only allow individual connections
OR
3/4
iptables.md 7/2/2020
Notes:
It is important to reject connections for all other ports after you have defined any rules that
make use of --dport
If you want to delete all configured rules, use the -F (flush) option
sudo iptables -F
To delete a rule use the -D option and the line number of the rule like
Persisting Changes
After you are done with the changes, they need to be persisted. Use the command below to persist all
changes. (The command is platform dependent). For Ubuntu run
sudo /sbin/iptables-save
You can use which iptables-save to see where the command is located.
4/4