Back to the command reference

iptables

NetworkAdvanced In real Linux

Configure Linux firewall rules

Name: iptables: IP (packet filter) tables

This command needs a real Linux system; you can practise it in real Linux mode.

Description

iptables configures the Linux kernel's packet filtering (firewall) rules, organised in tables (filter, nat …) and chains (INPUT, OUTPUT, FORWARD …). Packets are matched against rules in order to be accepted, dropped or translated.

Rules are lost on reboot unless saved. Newer systems use nftables underneath, and ufw or firewalld are easier for beginners.

Syntax

iptables [OPTION]... CHAIN RULE

Common options

-LList rules
-nWith -L: numeric addresses and ports, no lookups (faster)
-vMore detail (packet and byte counts, interfaces)
--line-numbersShow rule numbers (for deleting or inserting)
-AAppend a rule
-IInsert a rule at the top of a chain (or at a position)
-DDelete a rule
-FDelete every rule in a chain
-PSet a chain’s policy, e.g. -P INPUT DROP
-p / --dport / -jProtocol / port / target (ACCEPT, DROP)
-s / --source ADDRESSSource address, e.g. -s 192.168.1.0/24
-i IFACEThe incoming interface, e.g. -i eth0
-t TABLEThe table, e.g. -t nat (filter by default)

Examples

  • $ sudo iptables -L -n

    Show current rules

    Explain
  • $ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

    Allow SSH

    Explain

Related