| 1 | # 僅允許必要的 OUTPUT 流量 |
| 2 | iptables -A OUTPUT -o eth0 -p tcp --dport 80 -j ACCEPT |
| 3 | iptables -A OUTPUT -o eth0 -p tcp --dport 443 -j ACCEPT |
| 4 | iptables -A OUTPUT -o eth0 -p udp --dport 53 -j ACCEPT |
timmy / iptables 高效設定技巧
Last active 1 week ago
快速配置 iptables,確保流量安全,簡單搞定網絡管理。
| 1 | iptables -A INPUT -i lo -d 127.0.0.0/8 -j ACCEPT |
| 2 | iptables -A OUTPUT -o lo -s 127.0.0.0/8 -j ACCEPT |
| 3 |
| 1 | # 允許特定 IP 繞過 DNS 轉發 |
| 2 | iptables -t nat -A PREROUTING -i eth1 -s 192.168.6.10 -p udp --dport 53 -j ACCEPT |
| 3 | |
| 4 | # 其他設備的 DNS 流量轉發到指定伺服器 |
| 5 | iptables -t nat -A PREROUTING -i eth1 -p udp --dport 53 -j DNAT --to-destination 192.168.88.1 |
| 6 |
| 1 | # 檢查網卡是否存在 |
| 2 | if ! ip link show $LAN_IFACE > /dev/null 2>&1; then |
| 3 | echo "錯誤:內網網卡 $LAN_IFACE 不存在!" |
| 4 | exit 1 |
| 5 | fi |
| 6 | |
| 7 | # 檢查 iptables 命令是否成功 |
| 8 | iptables -P INPUT DROP || { echo "設置 INPUT 鏈策略失敗!"; exit 1; } |
| 9 |
| 1 | # 限制 SYN 封包速率,防止 SYN Flood 攻擊 |
| 2 | iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT |
| 3 | iptables -A DDoS_PROTECTION -i eth0 -p tcp --syn -j DROP |
| 4 | |
| 5 | # 使用 recent 模組限制單一 IP 的 SSH 連線速率 |
| 6 | iptables -A DDoS_PROTECTION -i eth0 -p tcp --dport 22 -m recent --set --name SSH_LIMIT --mask 255.255.255.255 |
| 7 | iptables -A DDoS_PROTECTION -i eth0 -p tcp --dport 22 -m recent --update --seconds 60 --hitcount 4 --name SSH_LIMIT --mask 255.255.255.255 -j DROP |
| 8 |
| 1 | # 限制 SSH 訪問僅允許特定子網 |
| 2 | iptables -A INPUT -p tcp --dport 22 -s 192.168.6.0/24 -j ACCEPT |
| 3 | iptables -A INPUT -p tcp --dport 22 -j DROP |
| 4 |
| 1 | # 定義基本規則 |
| 2 | setup_basic_rules() { |
| 3 | iptables -P INPUT DROP |
| 4 | iptables -P OUTPUT DROP |
| 5 | iptables -P FORWARD DROP |
| 6 | # ... 其他基本規則 |
| 7 | } |
| 8 | |
| 9 | # 定義 DDoS 防護規則 |
| 10 | setup_ddos_protection() { |
| 11 | iptables -N DDoS_PROTECTION |
| 12 | iptables -A INPUT -j DDoS_PROTECTION |
| 13 | # ... 其他 DDoS 防護規則 |
| 14 | } |
| 15 | |
| 16 | # 調用各個函數 |
| 17 | setup_basic_rules |
| 18 | setup_ddos_protection |
| 19 |
Comments 0
Log in to leave a comment