參考網站:iptables – ArchWiki
啟動 iptables & ip6tables
# systemctl start iptables
# systemctl start ip6tables
建立 iptables 規則
# cp /etc/iptables/empty.rules /etc/iptables/iptables.rules
# touch /etc/iptables/ip6tables.rules
開機時啟動 iptables & ip6tables
# systemctl enable iptables
ln -s ‘/usr/lib/systemd/system/iptables.service’ ‘/etc/systemd/system/multi-user.target.wants/iptables.service’
# systemctl enable ip6tables
[@more@]底下以 IPv4 為例
設定防火牆規則
# iptables -nvL –line-numbers
Chain INPUT (policy DROP 18 packets, 1042 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all — lo * 0.0.0.0/0 0.0.0.0/0
2 0 0 DROP all — * * 0.0.0.0/0 0.0.0.0/0 state INVALID
3 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 state NEW
4 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00
5 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x03/0x03
6 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x06/0x06
7 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x05/0x05
8 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x11/0x01
9 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x30/0x20
10 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x18/0x08
11 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x29
12 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x37
13 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F
14 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x01
15 107 7340 ACCEPT all — * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
16 0 0 ACCEPT tcp — * * 192.168.1.0/24 0.0.0.0/0 tcp dpt:22
17 0 0 ACCEPT tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
18 0 0 ACCEPT tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
19 0 0 ACCEPT icmp — * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
20 0 0 ACCEPT icmp — * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
Chain FORWARD (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 1 packets, 64 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all — * lo 0.0.0.0/0 0.0.0.0/0
2 100 16760 ACCEPT all — * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
將目前的規則寫入到 /etc/iptables/iptables.rules
# iptables-save > /etc/iptables/iptables.rules
重新載入防火牆規則
# systemctl reload iptables
# iptables-restore < /etc/iptables/iptables.rules
# cat /etc/iptables/iptables.rules
# Generated by iptables-save v1.4.21 on Mon Sep 29 23:06:35 2014
*mangle
:PREROUTING ACCEPT [49:3160]
:INPUT ACCEPT [49:3160]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [51:7732]
:POSTROUTING ACCEPT [51:7732]
COMMIT
# Completed on Mon Sep 29 23:06:35 2014
# Generated by iptables-save v1.4.21 on Mon Sep 29 23:06:35 2014
*filter
:INPUT DROP [4:124]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state INVALID -j DROP
-A INPUT -p tcp -m tcp ! –tcp-flags FIN,SYN,RST,ACK SYN -m state –state NEW -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,SYN FIN,SYN -j DROP
-A INPUT -p tcp -m tcp –tcp-flags SYN,RST SYN,RST -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,RST FIN,RST -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,ACK FIN -j DROP
-A INPUT -p tcp -m tcp –tcp-flags ACK,URG URG -j DROP
-A INPUT -p tcp -m tcp –tcp-flags PSH,ACK PSH -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,ACK,URG -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A INPUT -p tcp -m tcp –tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN -j DROP
-A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 192.168.1.0/24 -p tcp -m tcp –dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp –dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp –dport 443 -j ACCEPT
-A INPUT -p icmp -m icmp –icmp-type 8 -j ACCEPT
-A INPUT -p icmp -m icmp –icmp-type 0 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Mon Sep 29 23:06:35 2014
# Generated by iptables-save v1.4.21 on Mon Sep 29 23:06:35 2014
*nat
:PREROUTING ACCEPT [4:124]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
COMMIT
# Completed on Mon Sep 29 23:06:35 2014
使用 LEMP for Raspberry Pi 版會出現下面的錯誤訊息
iptables v1.4.21: can’t initialize iptables table `filter’: Table does not exist (do you need to insmod?)