iptables command in Linux (original) (raw)

Last Updated : 11 May, 2026

iptables is a Linux command-line firewall tool used to control and secure network traffic by defining packet-filtering rules. It works with the Netfilter framework in the Linux kernel to decide whether packets are accepted, dropped, or forwarded.

Core Components of iptables

iptables works using a structured model consisting of tables, chains, rules, and targets.

Tables in Iptables

Tables are collections of chains that define how packets should be processed for specific purposes, iptables uses different tables for handling various types of packet processing.

Built-in Chains of Tables in Iptables

Chains are ordered lists of rules that determine what action should be taken when a packet matches certain conditions. Each table contains predefined chains

Rules

Rules are the conditions applied to packets within chains. A rule matches a packet if it meets certain criteria. Common criteria include:

Targets (Actions)

Targets specify what happens when a packet matches a rule:

**Example:

Imagine you want to block SSH from a specific IP but allow all other traffic:

iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j DROP

**Output:

This rule drops incoming SSH connections from 192.168.1.100 while letting other traffic pass normally.

Syntax

iptables [options] [chain] [rule specification] [target]

User-Defined Chain Operations

User-defined chains can also be created. The following are the some of the possible one with examples:

1. -A, --append : Add a rule to a chain

Add a rule to a chain

**Syntax:

iptables [-t table] --append [chain] [parameters]

**Example:

This command drops all the traffic coming on any port.

iptables -t filter --append INPUT -j DROP

**Output:

appending the chain parameters

2. -D, --delete : Remove a rule from a chain

Remove a rule from a chain

**Syntax:

iptables [-t table] --delete [chain] [rule_number]

**Example:

This command deletes the rule 2 from INPUT chain.

iptables -t filter --delete INPUT 2

**Output:

deleting the chains in iptables

3. -C, --check: Check if a rule exists

Check if a rule is present in the chain or not.

**Syntax:

iptables [-t table] --check [chain] [parameters]

**Example:

This command checks whether the specified rule is present in the INPUT chain.

iptables -t filter --check INPUT -s 192.168.1.123 -j DROP

**Output:

checking the rules

Common Parameters in iptables

The iptables command uses parameters to define which packets to match and what actions to take. These parameters help you control traffic precisely. One of the most important parameters is -p (or --proto), which specifies the protocol of the packet, such as TCP, UDP, or ICMP. This allows you to filter traffic based on how the data is being sent.

1. -p, --proto: Protocol

Is the protocol that the packet follows. Possible values maybe: tcp, udp, icmp, etc.

**Syntax:

iptables [-t table] -A [chain] -p {protocol_name} -j {target}

**Example:

This command appends a rule in the INPUT chain to drop all udp packets.

sudo iptables -t filter -A INPUT -p udp -j DROP

**Output:

This command does not produce a visible output. After running it, all incoming UDP traffic will be blocked according to the rule.

**Verifying Command:

iptables --list

proto parameter of iptable

2. -s, --source: Source Address

Is used to match with the source address of the packet.

**Syntax:

iptables [-t table] -A [chain] -s {source_address} [target]

**Example: Accept all packets from 192.168.1.230

This command appends a rule in the INPUT chain to accept all packets originating from 192.168.1.230.

iptables -t filter -A INPUT -s 192.168.1.230 -j ACCEPT

**Output:

**Verifying Command:

iptables --list

source parameter with iptable

3. -d, --destination: Destination Address

Matches packets going to a specific IP address.

**Syntax:

iptables [-t table] -A [chain] -d {destination_address} [target]

**Example: Drop all packets going to 192.168.1.123

This command appends a rule in the OUTPUT chain to drop all packets destined for 192.168.1.123.

iptables -t filter -A OUTPUT -d 192.168.1.123 -j DROP

**Output:

**Verifying Command:

iptables --list

--destination parameter in iptables

4. -i, --in-interface: Input Interface

Matches packets with the specified in-interface and takes the action.

**Syntax:

iptables [-t table] -A [chain] -i {interface} [target]

**Example: Drop all packets coming from the wireless interface wlan0

This command appends a rule in the INPUT chain to drop all packets destined for wireless interface.

iptables -t filter -A INPUT -i wlan0 -j DROP

**Output:

**Verifying Command:

iptables --list --verbose

interface parameter in iptable

5. -o, --out-interface: Output Interface

Matches packets with the specified out-interface.

**Syntax:

iptables [-t table] -A [chain] -o {interface} -j {target}

**Example: Allow all packets leaving through the Ethernet interface eth0:

iptables -t filter -A OUTPUT -o eth0 -j ACCEPT

**Output:

**Verifying Command:

iptables --list

6. -j, --jump: Target Action

This parameter specifies the action to be taken on a match.

**Syntax:

iptables [-t table] -A [chain] [parameters] -j {target}

**Example: Drop all packets passing through the FORWARD chain

This command adds a rule in the FORWARD chain to drop all packets.

iptables -t filter -A FORWARD -j DROP

**Output:

**Verifying Command:

iptables --list

--jump parameter with iptables

Basic iptables Operations

1. Flush All Rules

Removes all rules and user-defined chains

**Command:

sudo iptables --flush

**Output:

Chain INPUT (policy ACCEPT) target prot opt source destination

Chain FORWARD (policy ACCEPT) target prot opt source destination

Chain OUTPUT (policy ACCEPT) target prot opt source destination

2. Save Configuration

Saves current firewall rules

**Command:

sudo iptables-save

**Output:

Generated by iptables-save v1.8.7 on Thu Mar 27 10:00:00 2026

*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -p tcp --dport 22 -j ACCEPT -A OUTPUT -p tcp --sport 22 -j ACCEPT COMMIT

Completed on Thu Mar 27 10:00:00 2026

**Command:

sudo iptables-save > /etc/iptables/rules.v4

3. Restore Configuration

Restores previously saved rules

**Command:

sudo iptables-restore < /etc/iptables/rules.v4

**Output:

Reasons to chose iptables in Linux

The following are the some of the reasons to use Iptables in Linux:

Benefits of Using iptable Command

The following are the benefits of using iptable command

Features

The following are the some of the features of Iptables:

Use cases

The following are the some of the usecases of Iptables