Open In App

iptables command in Linux with Examples

iptables is a command line interface used to set up and maintain tables for the Netfilter firewall for IPv4, included in the Linux kernel. The firewall matches packets with rules defined in these tables and then takes the specified action on a possible match.

Syntax:



iptables --table TABLE -A/-C/-D... CHAIN rule --jump Target
TABLE

There are five possible tables:

CHAINS

There are few built-in chains that are included in tables. They are:



Note: User-defined chains can also be created.

OPTIONS
  1. -A, –append : Append to the chain provided in parameters.

    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:

  2. -D, –delete : Delete rule from the specified 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:

  3. -C, –check :Check if a rule is present in the chain or not. It returns 0 if the rule exists and returns 1 if it does 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:

PARAMETERS

The parameters provided with the iptables command is used to match the packet and perform the specified action. The common parameters are:

  1. -p, –proto : is the protocol that the packet follows. Possible values maybe: tcp, udp, icmp, ssh etc.

    Syntax:

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

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

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

    Output:

  2. -s, –source: is used to match with the source address of the packet.

    Syntax:

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

    Example: 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:

  3. -d, –destination : is used to match with the destination address of the packet.

    Syntax:

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

    Example: 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:

  4. -i, –in-interface : matches packets with the specified in-interface and takes the action.

    Syntax:

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

    Example: 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:

  5. -o, –out-interface : matches packets with the specified out-interface.
  6. -j, –jump : this parameter specifies the action to be taken on a match.

    Syntax:

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

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

    iptables -t filter -A FORWARD -j DROP
    

    Output:

Note:

Article Tags :