Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux iptables 防火牆設定實踐

iptables 是 Linux 中常用的封包過濾工具。以下以三網卡環境為例,說明 NAT、轉發、使用者分群與封鎖策略的基本設定。

環境範例

  • eth0:192.168.0.1/24,內部網路。
  • eth1:192.168.20.1/24,外部網路。
  • eth2:192.168.50.1/24,會議室網路。
  • ppp0:由 eth1 撥號上網。

基本清理與 NAT

iptables -F
iptables -t nat -F
iptables -P FORWARD ACCEPT
iptables -X poweruser
iptables -X qquser
iptables -X httpuser

iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

開放基本服務

iptables -A FORWARD -p tcp --dport 53 -j ACCEPT
iptables -A FORWARD -p udp --dport 53 -j ACCEPT
iptables -A FORWARD -p tcp --dport 25 -j ACCEPT
iptables -A FORWARD -p tcp --dport 110 -j ACCEPT

VPN 與會議室網路

iptables -A FORWARD -s 192.168.10.0/24 -d 192.168.0.0/24 -j ACCEPT
iptables -A FORWARD -s 192.168.0.0/24 -d 192.168.10.0/24 -j ACCEPT
iptables -A FORWARD -s 192.168.50.0/24 -d 192.168.0.0/24 -j ACCEPT
iptables -A FORWARD -s 192.168.0.0/24 -d 192.168.50.0/24 -j ACCEPT

使用者分群

可建立 poweruser 與 httpuser 自訂鏈,依 MAC 位址將使用者導向不同策略。例如 httpuser 只允許 DNS、HTTP、HTTPS、郵件等服務,並可拒絕特定網站。

封鎖與預設政策

iptables -A FORWARD -m state --state INVALID -j DROP
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -P FORWARD DROP

透過 NAT、轉發規則、自訂鏈與狀態追蹤,iptables 可實現多網段隔離、服務限制與流量管控。

Copyright © Linux教程網 All Rights Reserved