2016-03-06 17:50:33 +02:00
|
|
|
# iptables
|
|
|
|
|
2023-09-18 05:57:51 +02:00
|
|
|
> Configure tables, chains and rules of the Linux kernel IPv4 firewall.
|
|
|
|
> Use `ip6tables` to set rules for IPv6 traffic. See also: `iptables-save`, `iptables-restore`.
|
|
|
|
> More information: <https://manned.org/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
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
`sudo iptables {{[-vnL --line-numbers|--verbose --numeric --list --line-numbers]}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
- Set chain policy rule:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
`sudo iptables {{[-P|--policy]}} {{chain}} {{rule}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
- Append rule to chain policy for IP:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
`sudo iptables {{[-A|--append]}} {{chain}} {{[-s|--source]}} {{ip}} {{[-j|--jump]}} {{rule}}`
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
- Append rule to chain policy for IP considering protocol and port:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
`sudo iptables {{[-A|--append]}} {{chain}} {{[-s|--source]}} {{ip}} {{[-p|--protocol]}} {{tcp|udp|icmp|...}} --dport {{port}} {{[-j|--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:
|
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
`sudo iptables {{[-t|--table]}} {{nat}} {{[-A|--append]}} {{POSTROUTING}} {{[-s|--source]}} {{192.168.0.0/24}} {{[-j|--jump]}} {{MASQUERADE}}`
|
2021-11-05 00:36:44 +01:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
- Delete chain rule:
|
2016-03-06 17:50:33 +02:00
|
|
|
|
2025-03-13 04:41:47 +02:00
|
|
|
`sudo iptables {{[-D|--delete]}} {{chain}} {{rule_line_number}}`
|