Linux防火牆終極對決:iptables與firewalld完整配置教程

iptables與firewalld防火牆配置完全指南

1. 防火牆基礎概念

1.1 什麼是防火牆

防火牆是一種網路安全裝置,用於監控和控制網路流量,根據預定義的安全規則來允許或阻止資料包透過。Linux系統中主要有兩種防火牆解決方案:iptables和firewalld。

1.2 iptables vs firewalld

  • • iptables:傳統的Linux防火牆工具,直接操作核心的netfilter框架
  • • firewalld:動態防火牆管理器,提供更高階的抽象和動態配置能力

2. iptables詳解

2.1 iptables基本概念

2.1.1 表(Tables)

  • • filter表:預設表,用於過濾資料包
  • • nat表:用於網路地址轉換
  • • mangle表:用於修改資料包頭部資訊
  • • raw表:用於配置連線跟蹤

2.1.2 鏈(Chains)

  • • INPUT:處理入站資料包
  • • OUTPUT:處理出站資料包
  • • FORWARD:處理轉發資料包
  • • PREROUTING:在路由決策前處理資料包
  • • POSTROUTING:在路由決策後處理資料包

2.1.3 目標(Targets)

  • • ACCEPT:接受資料包
  • • DROP:丟棄資料包
  • • REJECT:拒絕資料包並返回錯誤資訊
  • • LOG:記錄日誌
  • • MASQUERADE:IP偽裝

2.2 iptables基本語法

iptables [-t table] -[ADI] chain rule-specification
iptables [-t table] -[FLZ] [chain]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target

2.3 iptables常用命令

2.3.1 檢視規則

# 檢視所有規則
iptables -L -n -v

# 檢視特定表的規則
iptables -t nat -L -n -v

# 檢視規則編號
iptables -L INPUT --line-numbers

2.3.2 新增規則

# 允許SSH連線
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允許HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允許特定IP訪問
iptables -A INPUT -s 192.168.1.100 -j ACCEPT

# 允許本地迴環
iptables -A INPUT -i lo -j ACCEPT

# 允許已建立的連線
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

2.3.3 刪除規則

# 刪除特定規則
iptables -D INPUT -p tcp --dport 80 -j ACCEPT

# 按行號刪除
iptables -D INPUT 3

# 清空所有規則
iptables -F
iptables -X
iptables -Z

2.3.4 設定預設策略

# 設定預設拒絕策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

2.4 iptables高階配置

2.4.1 埠範圍和多埠

# 埠範圍
iptables -A INPUT -p tcp --dport 3000:3010 -j ACCEPT

# 多埠
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT

2.4.2 時間限制

# 只在工作時間允許訪問
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 18:00 -j ACCEPT

2.4.3 連線限制

# 限制併發連線數
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT

# 限制連線速率
iptables -A INPUT -p tcp --dport 22 -m limit --limit 5/min --limit-burst 10 -j ACCEPT

2.4.4 NAT配置

# SNAT(源地址轉換)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

# DNAT(目標地址轉換)
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:8080

2.5 iptables規則持久化

2.5.1 CentOS/RHEL系統

# 儲存規則
service iptables save

# 或者手動儲存
iptables-save > /etc/sysconfig/iptables

# 恢復規則
iptables-restore < /etc/sysconfig/iptables

2.5.2 Ubuntu/Debian系統

# 安裝iptables-persistent
apt-get install iptables-persistent

# 儲存規則
netfilter-persistent save

# 恢復規則
netfilter-persistent reload

3. firewalld詳解

3.1 firewalld基本概念

3.1.1 區域(Zones)

  • • drop:丟棄所有傳入連線
  • • block:拒絕所有傳入連線
  • • public:公共區域,預設區域
  • • external:外部區域,用於NAT
  • • dmz:DMZ區域
  • • work:工作區域
  • • home:家庭區域
  • • internal:內部區域
  • • trusted:信任區域,允許所有連線

3.1.2 服務(Services)

預定義的服務配置,包含埠、協議等資訊。

3.1.3 富規則(Rich Rules)

提供更復雜的規則配置語法。

3.2 firewalld基本命令

3.2.1 服務管理

# 啟動firewalld
systemctl start firewalld

# 停止firewalld
systemctl stop firewalld

# 重啟firewalld
systemctl restart firewalld

# 檢視狀態
systemctl status firewalld
firewall-cmd --state

3.2.2 區域管理

# 檢視預設區域
firewall-cmd --get-default-zone

# 設定預設區域
firewall-cmd --set-default-zone=public

# 檢視活動區域
firewall-cmd --get-active-zones

# 檢視所有區域
firewall-cmd --get-zones

# 檢視區域資訊
firewall-cmd --zone=public --list-all

3.2.3 服務管理

# 檢視可用服務
firewall-cmd --get-services

# 檢視已開放的服務
firewall-cmd --list-services

# 新增服務
firewall-cmd --add-service=http
firewall-cmd --add-service=https

# 刪除服務
firewall-cmd --remove-service=http

# 永久新增服務
firewall-cmd --permanent --add-service=http

3.2.4 埠管理

# 新增埠
firewall-cmd --add-port=8080/tcp

# 刪除埠
firewall-cmd --remove-port=8080/tcp

# 檢視開放埠
firewall-cmd --list-ports

# 永久新增埠
firewall-cmd --permanent --add-port=8080/tcp

3.3 firewalld高階配置

3.3.1 自定義服務

# 建立自定義服務配置檔案
cat > /etc/firewalld/services/myapp.xml << EOF
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MyApp</short>
  <description>My Application Service</description>
  <port protocol="tcp" port="8080"/>
  <port protocol="tcp" port="8443"/>
</service>
EOF

# 重新載入配置
firewall-cmd --reload

# 新增自定義服務
firewall-cmd --add-service=myapp

3.3.2 富規則配置

# 允許特定IP訪問特定埠
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'

# 限制連線速率
firewall-cmd --add-rich-rule='rule service name="ssh" limit value="10/m" accept'

# 阻止特定IP
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.200" drop'

# 埠轉發
firewall-cmd --add-rich-rule='rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'

3.3.3 介面繫結

# 將介面繫結到區域
firewall-cmd --zone=internal --add-interface=eth1

# 檢視介面繫結
firewall-cmd --get-zone-of-interface=eth1

# 更改介面區域
firewall-cmd --zone=public --change-interface=eth1

3.3.4 IP偽裝和埠轉發

# 啟用IP偽裝
firewall-cmd --add-masquerade

# 埠轉發
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100

# 檢視轉發規則
firewall-cmd --list-forward-ports

4. 實戰配置案例

4.1 Web伺服器防火牆配置

4.1.1 iptables配置

#!/bin/bash
# Web伺服器防火牆配置指令碼

# 清空現有規則
iptables -F
iptables -X
iptables -Z

# 設定預設策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允許本地迴環
iptables -A INPUT -i lo -j ACCEPT

# 允許已建立的連線
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允許SSH(限制連線數)
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允許HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允許FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT

# 允許DNS
iptables -A INPUT -p udp --dport 53 -j ACCEPT

# 儲存規則
service iptables save

4.1.2 firewalld配置

#!/bin/bash
# Web伺服器防火牆配置指令碼

# 設定預設區域
firewall-cmd --set-default-zone=public

# 新增服務
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=ftp

# 限制SSH連線
firewall-cmd --permanent --add-rich-rule='rule service name="ssh" limit value="3/m" accept'

# 重新載入配置
firewall-cmd --reload

4.2 資料庫伺服器防火牆配置

4.2.1 iptables配置

#!/bin/bash
# 資料庫伺服器防火牆配置

# 清空現有規則
iptables -F
iptables -X
iptables -Z

# 設定預設策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允許本地迴環
iptables -A INPUT -i lo -j ACCEPT

# 允許已建立的連線
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允許SSH(僅限管理網段)
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

# 允許MySQL(僅限應用伺服器)
iptables -A INPUT -s 192.168.1.100 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -s 192.168.1.101 -p tcp --dport 3306 -j ACCEPT

# 儲存規則
service iptables save

4.2.2 firewalld配置

#!/bin/bash
# 資料庫伺服器防火牆配置

# 建立資料庫區域
firewall-cmd --permanent --new-zone=database

# 設定預設區域
firewall-cmd --set-default-zone=database

# 新增SSH服務(限制源IP)
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'

# 新增MySQL服務(限制源IP)
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="mysql" accept'
firewall-cmd --permanent --zone=database --add-rich-rule='rule family="ipv4" source address="192.168.1.101" service name="mysql" accept'

# 重新載入配置
firewall-cmd --reload

5. 故障排查和最佳化

5.1 常見問題排查

5.1.1 規則不生效

# 檢查規則是否正確新增
iptables -L -n -v
firewall-cmd --list-all

# 檢查服務狀態
systemctl status iptables
systemctl status firewalld

# 檢查日誌
tail -f /var/log/messages
journalctl -u firewalld -f

5.1.2 連線被拒絕

# 啟用日誌記錄
iptables -A INPUT -j LOG --log-prefix "INPUT DROP: "

# 檢視日誌
tail -f /var/log/messages

# firewalld日誌
firewall-cmd --set-log-denied=all

5.2 效能最佳化

5.2.1 規則最佳化

# 將常用規則放在前面
iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

# 使用狀態匹配減少規則數量
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 使用multiport匹配多個埠
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT

5.2.2 監控和統計

# 檢視規則統計
iptables -L -n -v --line-numbers

# 重置計數器
iptables -Z

# 即時監控
watch -n 1 'iptables -L -n -v'

6. 安全最佳實踐

6.1 基本安全原則

  1. 1. 最小許可權原則:只開放必要的埠和服務
  2. 2. 白名單策略:預設拒絕所有連線,只允許必要的連線
  3. 3. 定期審計:定期檢查和更新防火牆規則
  4. 4. 日誌監控:啟用日誌記錄並定期分析

6.2 配置建議

# 設定合理的預設策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 啟用連線跟蹤
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 限制連線速率
iptables -A INPUT -p tcp --dport 22 -m limit --limit 5/min --limit-burst 10 -j ACCEPT

# 防止掃描
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

6.3 備份和恢復

# 備份iptables規則
iptables-save > /root/iptables.backup.$(date +%Y%m%d)

# 恢復iptables規則
iptables-restore < /root/iptables.backup.20231201

# 備份firewalld配置
cp -r /etc/firewalld /root/firewalld.backup.$(date +%Y%m%d)

7. 總結

iptables和firewalld都是Linux系統中強大的防火牆工具。iptables提供了更底層的控制能力,適合對防火牆規則有詳細要求的場景;firewalld則提供了更友好的管理介面和動態配置能力,更適合日常運維管理。
選擇使用哪種工具主要取決於具體的應用場景和個人偏好。在實際部署中,建議根據系統的具體需求制定合適的防火牆策略,並定期進行安全審計和規則最佳化。
記住防火牆只是網路安全的一個組成部分,還需要結合其他安全措施(如入侵檢測、日誌監控、定期更新等)來構建完整的安全防護體系。
文末福利
就目前來說,傳統運維衝擊年薪30W+的轉型方向就是SRE&DevOps崗位。
為了幫助大家早日擺脫繁瑣的基層運維工作,給大家整理了一套高階運維工程師必備技能資料包,內容有多詳實豐富看下圖!
共有 20 個模組
1.38張最全工程師技能圖譜
2.面試大禮包
3.Linux書籍
4.go書籍
······
6.自動化運維工具
18.訊息佇列合集
 以上所有資料獲取請掃碼
備註:最新運維資料
100%免費領取
(後臺不再回復,掃碼一鍵領取)


相關文章