2016-03-06 17:50:33 +02:00
|
|
|
# iptables
|
|
|
|
|
2017-07-20 20:53:15 +02:00
|
|
|
> Program that allows configuration of tables, chains and rules provided by the Linux kernel firewall.
|
2020-11-01 14:18:01 -05:00
|
|
|
> More information: <https://www.netfilter.org/projects/iptables/>.
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- View chains, rules, packet/byte counters and line numbers for the filter table:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables --verbose --numeric --list --line-numbers`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- Set chain [P]olicy rule:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables --policy {{chain}} {{rule}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- [A]ppend rule to chain policy for IP:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables --append {{chain}} --source {{ip}} --jump {{rule}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- [A]ppend rule to chain policy for IP considering [p]rotocol and port:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables --append {{chain}} --source {{ip}} --protocol {{protocol}} --dport {{port}} --jump {{rule}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2021-11-05 00:36:44 +01:00
|
|
|
- Add a NAT rule to translate all traffic from the `192.168.0.0/24` subnet to the host's public IP:
|
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables --table {{nat}} --append {{POSTROUTING}} --source {{192.168.0.0/24}} --jump {{MASQUERADE}}`
|
2021-11-05 00:36:44 +01:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- [D]elete chain rule:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables --delete {{chain}} {{rule_line_number}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- Save `iptables` configuration of a given [t]able to a file:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
`sudo iptables-save --table {{tablename}} > {{path/to/iptables_file}}`
|
2018-08-12 10:23:48 -06:00
|
|
|
|
2023-09-12 08:54:42 -04:00
|
|
|
- Restore `iptables` configuration from a file:
|
2018-08-12 10:23:48 -06:00
|
|
|
|
|
|
|
`sudo iptables-restore < {{path/to/iptables_file}}`
|