CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Network Transfer & Tools / iptables

iptables

Filter and NAT packets with netfilter rules

Advancedfirewallsecuritynetfilter

iptables manages the kernel's packet-filtering rules: which incoming, outgoing and forwarded packets are accepted, dropped or rewritten (NAT). Even where nftables, ufw, firewalld, Docker or Kubernetes write the rules for you, iptables is how you read and debug them.

Syntaxiptables [-t TABLE] -A|-I|-D|-R CHAIN [N] RULE-SPEC -j TARGET | iptables -L|-S [CHAIN] [-n -v --line-numbers] | -P CHAIN POLICY

Watch it run

Before touching a server's firewall, see what rules exist and which chain policies apply.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
iptables -L

Even listing needs root. The version string tells you this is the nf_tables variant: the rules are stored as nftables in the kernel.

Anatomy of a command

$
commandsudo

Reading and changing rules both need root (CAP_NET_ADMIN).

How it works

Netfilter is a set of hooks in the kernel's network path. A packet arriving on an interface passes PREROUTING (where DNAT happens), then the routing decision: if it's addressed to this host it goes through INPUT and on to the local socket; if not, and forwarding is on, through FORWARD and POSTROUTING (where SNAT/MASQUERADE happens) and out. Packets the host itself sends go through OUTPUT and POSTROUTING. Each hook has chains in several tables (filter for accept/drop, nat for address rewriting, mangle and raw). Rules in a chain are checked top to bottom and the first one whose matches all fit decides with its target; if none matches, the chain's policy (ACCEPT or DROP) applies. Connection tracking (conntrack) lets one rule accept all packets of ESTABLISHED connections, so you only write rules for new ones. Rules live only in kernel memory: iptables-save dumps them and something must restore them at boot.

Incoming packets pass PREROUTING, the routing decision, then INPUT (for this host) or FORWARD; rules are checked top down and the first match wins.

SYN :22localACCEPTnot localreplyPacket inens5PREROUTINGnat: DNATconntrack: NEWRoutingfor this host?INPUTfilter tableLocal socketsshd :22OUTPUTlocal repliesFORWARDfilter tablePOSTROUTINGnat: SNAT/MASQ
1/8
A SYN arrives for port 22

A client at 198.51.100.23 opens an SSH connection. The packet enters through ens5 and hits the PREROUTING hook, where conntrack sees it as a NEW connection and nat rules could rewrite its destination.

← → to step

Options you'll actually use

-L [CHAIN] -n -vList rules numerically with packet/byte counters and interfaces
--line-numbersShow rule positions (for -I N and -D N)
-S [CHAIN]Print rules as the commands that create them
-A CHAINAppend a rule at the end
-I CHAIN [N]Insert a rule at position N (default 1, the top)
-D CHAIN N|RULEDelete a rule by number or by exact specification
-P CHAIN ACCEPT|DROPSet the chain's default policy
-F [CHAIN]Flush (delete) all rules in a chain or table
-t nat|mangle|rawWork on another table (default filter)
-p, --dport, -s, -d, -i, -oMatch protocol, port, source, destination, in/out interface
-m conntrack --ctstateMatch connection state: NEW, ESTABLISHED, RELATED, INVALID
-j ACCEPT|DROP|REJECT|LOGWhat to do with matching packets
-ZZero the packet and byte counters
iptables-save / -restoreDump all rules to text / load them back atomically

Common mistakes

sudo iptables -P INPUT DROP before adding an ACCEPT rule for SSH (or for ESTABLISHED)

Your own SSH session's packets match nothing and are dropped instantly: you're locked out.

Add lo, ESTABLISHED,RELATED and SSH ACCEPT rules first, set the policy last; or schedule a rollback (e.g. at now + 5 min: iptables-restore < backup)
iptables -A INPUT -s BAD_IP -j DROP to block an attacker

-A appends after your ACCEPT rules, and since the first match wins, the attacker's traffic is accepted before reaching it.

iptables -I INPUT 1 -s BAD_IP -j DROP
Relying on INPUT rules to protect ports published by Docker

Published ports are DNATed and pass through FORWARD, so INPUT never sees them; Docker's own rules accept them.

Put filters in the DOCKER-USER chain, or bind published ports to 127.0.0.1.
Configuring rules and forgetting to save them

Rules exist only in kernel memory and disappear at reboot, often unnoticed until the next maintenance window.

netfilter-persistent save (Ubuntu) or iptables-save > /etc/sysconfig/iptables (RHEL)
Mixing raw iptables commands with ufw or firewalld on the same host

The managers rewrite the rule set on reload and your manual rules vanish or conflict.

Pick one tool per host and make all changes through it.

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List the filter table's rules with numeric output, counters and line numbers.

  2. 2

    Add a rule that rejects (not drops) TCP connections to port 8081 with a TCP reset, then show the INPUT chain as commands.

  3. 3

    Insert a rule at the top of INPUT that drops everything from 192.0.2.66, then delete it again by its number.

  4. 4

    Save the current rules to /tmp/rules.v4 and show the file.

Related commands

firewall-cmdManage firewalld zones, services and portsufwManage the host firewall the simple waynmapScan hosts and ports, detect servicesssSee listening ports and open connections
PrevioustelnetNextfirewall-cmd
Linux / Network Transfer & Tools / iptables

iptables

Filter and NAT packets with netfilter rules

Advancedfirewallsecuritynetfilter

iptables manages the kernel's packet-filtering rules: which incoming, outgoing and forwarded packets are accepted, dropped or rewritten (NAT). Even where nftables, ufw, firewalld, Docker or Kubernetes write the rules for you, iptables is how you read and debug them.

Syntaxiptables [-t TABLE] -A|-I|-D|-R CHAIN [N] RULE-SPEC -j TARGET | iptables -L|-S [CHAIN] [-n -v --line-numbers] | -P CHAIN POLICY

Watch it run

Before touching a server's firewall, see what rules exist and which chain policies apply.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
iptables -L

Even listing needs root. The version string tells you this is the nf_tables variant: the rules are stored as nftables in the kernel.

Anatomy of a command

$
commandsudo

Reading and changing rules both need root (CAP_NET_ADMIN).

How it works

Netfilter is a set of hooks in the kernel's network path. A packet arriving on an interface passes PREROUTING (where DNAT happens), then the routing decision: if it's addressed to this host it goes through INPUT and on to the local socket; if not, and forwarding is on, through FORWARD and POSTROUTING (where SNAT/MASQUERADE happens) and out. Packets the host itself sends go through OUTPUT and POSTROUTING. Each hook has chains in several tables (filter for accept/drop, nat for address rewriting, mangle and raw). Rules in a chain are checked top to bottom and the first one whose matches all fit decides with its target; if none matches, the chain's policy (ACCEPT or DROP) applies. Connection tracking (conntrack) lets one rule accept all packets of ESTABLISHED connections, so you only write rules for new ones. Rules live only in kernel memory: iptables-save dumps them and something must restore them at boot.

Incoming packets pass PREROUTING, the routing decision, then INPUT (for this host) or FORWARD; rules are checked top down and the first match wins.

SYN :22localACCEPTnot localreplyPacket inens5PREROUTINGnat: DNATconntrack: NEWRoutingfor this host?INPUTfilter tableLocal socketsshd :22OUTPUTlocal repliesFORWARDfilter tablePOSTROUTINGnat: SNAT/MASQ
1/8
A SYN arrives for port 22

A client at 198.51.100.23 opens an SSH connection. The packet enters through ens5 and hits the PREROUTING hook, where conntrack sees it as a NEW connection and nat rules could rewrite its destination.

← → to step

Options you'll actually use

-L [CHAIN] -n -vList rules numerically with packet/byte counters and interfaces
--line-numbersShow rule positions (for -I N and -D N)
-S [CHAIN]Print rules as the commands that create them
-A CHAINAppend a rule at the end
-I CHAIN [N]Insert a rule at position N (default 1, the top)
-D CHAIN N|RULEDelete a rule by number or by exact specification
-P CHAIN ACCEPT|DROPSet the chain's default policy
-F [CHAIN]Flush (delete) all rules in a chain or table
-t nat|mangle|rawWork on another table (default filter)
-p, --dport, -s, -d, -i, -oMatch protocol, port, source, destination, in/out interface
-m conntrack --ctstateMatch connection state: NEW, ESTABLISHED, RELATED, INVALID
-j ACCEPT|DROP|REJECT|LOGWhat to do with matching packets
-ZZero the packet and byte counters
iptables-save / -restoreDump all rules to text / load them back atomically

Common mistakes

sudo iptables -P INPUT DROP before adding an ACCEPT rule for SSH (or for ESTABLISHED)

Your own SSH session's packets match nothing and are dropped instantly: you're locked out.

Add lo, ESTABLISHED,RELATED and SSH ACCEPT rules first, set the policy last; or schedule a rollback (e.g. at now + 5 min: iptables-restore < backup)
iptables -A INPUT -s BAD_IP -j DROP to block an attacker

-A appends after your ACCEPT rules, and since the first match wins, the attacker's traffic is accepted before reaching it.

iptables -I INPUT 1 -s BAD_IP -j DROP
Relying on INPUT rules to protect ports published by Docker

Published ports are DNATed and pass through FORWARD, so INPUT never sees them; Docker's own rules accept them.

Put filters in the DOCKER-USER chain, or bind published ports to 127.0.0.1.
Configuring rules and forgetting to save them

Rules exist only in kernel memory and disappear at reboot, often unnoticed until the next maintenance window.

netfilter-persistent save (Ubuntu) or iptables-save > /etc/sysconfig/iptables (RHEL)
Mixing raw iptables commands with ufw or firewalld on the same host

The managers rewrite the rule set on reload and your manual rules vanish or conflict.

Pick one tool per host and make all changes through it.

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List the filter table's rules with numeric output, counters and line numbers.

  2. 2

    Add a rule that rejects (not drops) TCP connections to port 8081 with a TCP reset, then show the INPUT chain as commands.

  3. 3

    Insert a rule at the top of INPUT that drops everything from 192.0.2.66, then delete it again by its number.

  4. 4

    Save the current rules to /tmp/rules.v4 and show the file.

Related commands

firewall-cmdManage firewalld zones, services and portsufwManage the host firewall the simple waynmapScan hosts and ports, detect servicesssSee listening ports and open connections
PrevioustelnetNextfirewall-cmd