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 -mtime--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 -mlimit--limit5/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 # 重新加載配置 firewall-cmd --reload # 添加自定義服務 firewall-cmd --add-service=myapp MyApp My Application Service
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.最小權限原則:只開放必要的端口和服務
2.白名單策略:默認拒絕所有連接,只允許必要的連接
3.定期審計:定期檢查和更新防火墻規則
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 -mlimit--limit5/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則提供了更友好的管理界面和動態配置能力,更適合日常運維管理。
選擇使用哪種工具主要取決于具體的應用場景和個人偏好。在實際部署中,建議根據系統的具體需求制定合適的防火墻策略,并定期進行安全審計和規則優化。
記住防火墻只是網絡安全的一個組成部分,還需要結合其他安全措施(如入侵檢測、日志監控、定期更新等)來構建完整的安全防護體系。
-
Linux
+關注
關注
87文章
11509瀏覽量
213714 -
防火墻
+關注
關注
0文章
435瀏覽量
36186 -
網絡安全
+關注
關注
11文章
3339瀏覽量
61446
原文標題:Linux防火墻終極對決:iptables與firewalld完整配置教程
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
Linux系統iptables和firewall防火墻的配置方法

Linux中使用Iptables實現簡單的網站防火墻

Linux系統firewalld防火墻實戰指南

評論