Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
|
iptables [2025/11/12 22:16] 127.0.0.1 Externe Bearbeitung |
iptables [2025/11/16 04:42] (aktuell) admin |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | IPTables ist eine hauseigene [[firewall|Firewall]] für [[linux|Linux]] ab Kernel 2.4. Genaugenommen ist es ein Tool das im Hintergrund [[Netfilter]] konfiguriert. Seit etwa Mitte 2018 wurde iptables durch [[nftables]] (netfilter tables) ersetzt, die Syntax blieb weitgehend gleich. [[ebtables]] ist wie IPTables aber für Layer 2 Frames. | ||
| + | Netfilter hat 3 Tabellen | ||
| + | |||
| + | * filter - Paketfilter | ||
| + | * nat - [[NAT]] | ||
| + | * mangle - Paketheader manipulieren | ||
| + | |||
| + | Targets | ||
| + | |||
| + | * return | ||
| + | * queue | ||
| + | * drop | ||
| + | * accept | ||
| + | * reject | ||
| + | * log - nach / | ||
| + | |||
| + | Policies | ||
| + | |||
| + | * INPUT | ||
| + | * OUTPUT | ||
| + | * FORWARD | ||
| + | |||
| + | < | ||
| + | # statistics | ||
| + | iptables -L -v | ||
| + | </ | ||
| + | |||
| + | {{iptables_architektur.png}} | ||
| + | |||
| + | {{iptables_example_firewall.png}} | ||
| + | |||
| + | {{selinux_iptables.pdf}} | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <box red> | ||
| + | |||
| + | < | ||
| + | iptables -L < | ||
| + | iptables -D < | ||
| + | iptables -F # flush rules | ||
| + | iptables -X < | ||
| + | |||
| + | // default policy -> target | ||
| + | iptables -P < | ||
| + | |||
| + | // delete rules | ||
| + | iptables -L < | ||
| + | iptables -D < | ||
| + | |||
| + | // append rule | ||
| + | iptables -A < | ||
| + | |||
| + | // Policy-chain erstellen | ||
| + | iptables -N < | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | -A - Regel anhängen | ||
| + | -I - An bestimmter Position einfügen | ||
| + | -i - Input interface | ||
| + | -o - Output interface | ||
| + | -s - Source IP | ||
| + | -d - Destination IP | ||
| + | -j - Action | ||
| + | |||
| + | -p - Protokoll (tcp/ | ||
| + | --dport - Zielport/ | ||
| + | --sport - Quellport | ||
| + | --sports, --dports - mehrere Ports | ||
| + | -state, --ctstate - RELATED/ | ||
| + | </ | ||
| + | |||
| + | IP blocken | ||
| + | |||
| + | < | ||
| + | iptables -I INPUT -s 167.114.157.154 -j DROP | ||
| + | </ | ||
| + | |||
| + | Alle eingehenden TCP ausser Port 22 verbieten | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp -m tcp -m multiport ! --dports 22 -j DROP | ||
| + | </ | ||
| + | |||
| + | Sonstiges | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -i lo -j ACCEPT # allow incoming on lo | ||
| + | iptables -A OUTPUT -o lo -j ACCEPT # allow outgoing on lo | ||
| + | iptables -A INPUT -m state --state ESTABLISHED, | ||
| + | iptables -A INPUT -m conntrack --ctstate ESTABLISHED, | ||
| + | iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT # established outgoing connections | ||
| + | </ | ||
| + | |||
| + | Assuming eth0 is your external network, and eth1 is your internal network, this will allow your internal to access the external | ||
| + | |||
| + | < | ||
| + | iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Block or reject packets/ | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -m conntrack --ctstate INVALID -j DROP # drop invalid packets | ||
| + | iptables -A INPUT -s 15.15.15.51 -j DROP # block an ip | ||
| + | iptables -A INPUT -s 15.15.15.51 -j REJECT # reject an ip (with answer!) | ||
| + | </ | ||
| + | |||
| + | To block connections from a specific IP address, e.g. 15.15.15.51, | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -i eth0 -s 15.15.15.51 -j DROP | ||
| + | </ | ||
| + | |||
| + | Allow incoming SSH | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW, | ||
| + | sudo iptables -A OUTPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Allow incoming SSH from Specific IP address or subnet | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp -s 15.15.15.0/ | ||
| + | iptables -A OUTPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Allow outgoing SSH | ||
| + | |||
| + | < | ||
| + | iptables -A OUTPUT -p tcp --dport 22 -m conntrack --ctstate NEW, | ||
| + | iptables -A INPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Allow Incoming Rsync from Specific IP Address or Subnet | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp -s 15.15.15.0/ | ||
| + | iptables -A OUTPUT -p tcp --sport 873 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Allow incoming HTTP (port 80) | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp --dport 80 -m conntrack --ctstate NEW, | ||
| + | iptables -A OUTPUT -p tcp --sport 80 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Allow incoming HTTP (Port 80) and HTTPS (Port 443) | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW, | ||
| + | iptables -A OUTPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | Allow [[mysql|MySQL]] only from specific IP or Subnet | ||
| + | |||
| + | < | ||
| + | iptables -A INPUT -p tcp -s 15.15.15.0/ | ||
| + | iptables -A OUTPUT -p tcp --sport 3306 -m conntrack --ctstate ESTABLISHED -j ACCEPT | ||
| + | </ | ||
| + | |||
| + | If you want to delete the rule that drops invalid incoming packets (-A INPUT -m conntrack --ctstate INVALID -j DROP) | ||
| + | |||
| + | < | ||
| + | iptables -D INPUT -m conntrack --ctstate INVALID -j DROP | ||
| + | </ | ||
| + | |||
| + | Delete Rule by Chain and Number | ||
| + | |||
| + | < | ||
| + | iptables -L --line-numbers # list rules | ||
| + | |||
| + | Chain INPUT (policy DROP) | ||
| + | num target | ||
| + | 1 ACCEPT | ||
| + | 2 ACCEPT | ||
| + | 3 DROP | ||
| + | 4 UDP udp -- anywhere | ||
| + | 5 TCP tcp -- anywhere | ||
| + | 6 ICMP icmp -- anywhere | ||
| + | 7 REJECT | ||
| + | 8 REJECT | ||
| + | 9 REJECT | ||
| + | 10 | ||
| + | … | ||
| + | |||
| + | iptables -D INPUT 3 # delete rule nr. 3 | ||
| + | </ | ||
| + | |||
| + | Siehe auch [[https:// | ||
| + | |||
| + | =====Links===== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||