iptables

iptables-persistent, netfilter-persistent

Basic options

There are a few basic concepts within iptables:

Tables

Within each table, there are a few available chain options. The values will be listed down under each table, and will be further discussed in a later section.

  1. filter (default)

    • INPUT

    • FORWARD

    • OUTPUT

  2. nat

    • ...

  3. mangle

    • ...

  4. raw

    • ...

Chain

  1. INPUT

  2. FORWARD

  3. OUTPUT

Target

  1. ACCEPT

  2. DROP

  3. REJECT

Basic flag options

  1. -A OR --append

  2. -I OR --insert

  3. -D OR --delete

  4. -t OR --table

  5. -L OR --list

  6. -p OR --protocol

  7. -P or --policy

  8. -j OR --jump

  9. -s OR --source

  10. -d OR --destination

  11. --sport OR --source-port

  12. --dport OR --destination-port

Add/insert rules examples

  1. Add an INPUT rule to DROP all traffic regardless of the protocol, destination port, source address, or any other parameters

  1. Add an INPUT rule to DROP all SSH traffic (TCP port 22)

An additional rule can be added to allow SSH traffic from a source address range. Note that the -I flag option is used instead of -A to insert, rather than append the new rule. This allow sthe new rule to take precedence over the previously defined rule (for matching traffic).

View the rules

Persistent rules with iptables-persistent

  1. Install iptables-persistent

  1. Update rules with iptables command

  2. Save to rules file: /etc/iptables/rules.v4 (For IPv4)

Persistent rules with netfilter-persistent

All the same steps as above for iptables-persistent, with some additional steps:

  1. Install netfilter-persistent and iptables-persistent

  • seems like installing iptables-persistent automatically adds the current rules in the iptables into the file: /etc/iptables/rules.v4

  1. Enable netfilter-persistent service

Check to ensure that it is enabled. It doesn't matter if its not running yet, as it will be started on reboot as long its enabled.

  1. Verify if the rules have been added to the configuration file: /etc/iptables/rules.v4

Otherwise, manually add the rules using the iptables-save tool

The iptables-save tool simply returns the current iptables rules

  1. Reboot the system

  1. Check iptables rule

The iptables rules should persist from the previous configurations.

Example use case

Last updated