一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Linux系統(tǒng)中iptables與firewalld防火墻的區(qū)別

馬哥Linux運(yùn)維 ? 來源:馬哥Linux運(yùn)維 ? 2025-07-14 11:24 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

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

1. 防火墻基礎(chǔ)概念

1.1 什么是防火墻

防火墻是一種網(wǎng)絡(luò)安全設(shè)備,用于監(jiān)控和控制網(wǎng)絡(luò)流量,根據(jù)預(yù)定義的安全規(guī)則來允許或阻止數(shù)據(jù)包通過。Linux系統(tǒng)中主要有兩種防火墻解決方案:iptables和firewalld。

1.2 iptables vs firewalld

?iptables:傳統(tǒng)的Linux防火墻工具,直接操作內(nèi)核的netfilter框架

?firewalld:動態(tài)防火墻管理器,提供更高級的抽象和動態(tài)配置能力

2. iptables詳解

2.1 iptables基本概念

2.1.1 表(Tables)

?filter表:默認(rèn)表,用于過濾數(shù)據(jù)包

?nat表:用于網(wǎng)絡(luò)地址轉(zhuǎn)換

?mangle表:用于修改數(shù)據(jù)包頭部信息

?raw表:用于配置連接跟蹤

2.1.2 鏈(Chains)

?INPUT:處理入站數(shù)據(jù)包

?OUTPUT:處理出站數(shù)據(jù)包

?FORWARD:處理轉(zhuǎn)發(fā)數(shù)據(jù)包

?PREROUTING:在路由決策前處理數(shù)據(jù)包

?POSTROUTING:在路由決策后處理數(shù)據(jù)包

2.1.3 目標(biāo)(Targets)

?ACCEPT:接受數(shù)據(jù)包

?DROP:丟棄數(shù)據(jù)包

?REJECT:拒絕數(shù)據(jù)包并返回錯(cuò)誤信息

?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 查看規(guī)則

# 查看所有規(guī)則
iptables -L -n -v

# 查看特定表的規(guī)則
iptables -t nat -L -n -v

# 查看規(guī)則編號
iptables -L INPUT --line-numbers

2.3.2 添加規(guī)則

# 允許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

# 允許本地回環(huán)
iptables -A INPUT -i lo -j ACCEPT

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

2.3.3 刪除規(guī)則

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

# 按行號刪除
iptables -D INPUT 3

# 清空所有規(guī)則
iptables -F
iptables -X
iptables -Z

2.3.4 設(shè)置默認(rèn)策略

# 設(shè)置默認(rèn)拒絕策略
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 時(shí)間限制

# 只在工作時(shí)間允許訪問
iptables -A INPUT -p tcp --dport 22 -mtime--timestart 09:00 --timestop 18:00 -j ACCEPT

2.4.3 連接限制

# 限制并發(fā)連接數(shù)
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(源地址轉(zhuǎn)換)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

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

2.5 iptables規(guī)則持久化

2.5.1 CentOS/RHEL系統(tǒng)

# 保存規(guī)則
service iptables save

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

# 恢復(fù)規(guī)則
iptables-restore < /etc/sysconfig/iptables

2.5.2 Ubuntu/Debian系統(tǒng)

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

# 保存規(guī)則
netfilter-persistent save

# 恢復(fù)規(guī)則
netfilter-persistent reload

3. firewalld詳解

3.1 firewalld基本概念

3.1.1 區(qū)域(Zones)

?drop:丟棄所有傳入連接

?block:拒絕所有傳入連接

?public:公共區(qū)域,默認(rèn)區(qū)域

?external:外部區(qū)域,用于NAT

?dmz:DMZ區(qū)域

?work:工作區(qū)域

?home:家庭區(qū)域

?internal:內(nèi)部區(qū)域

?trusted:信任區(qū)域,允許所有連接

3.1.2 服務(wù)(Services)

預(yù)定義的服務(wù)配置,包含端口、協(xié)議等信息。

3.1.3 富規(guī)則(Rich Rules)

提供更復(fù)雜的規(guī)則配置語法。

3.2 firewalld基本命令

3.2.1 服務(wù)管理

# 啟動firewalld
systemctl start firewalld

# 停止firewalld
systemctl stop firewalld

# 重啟firewalld
systemctl restart firewalld

# 查看狀態(tài)
systemctl status firewalld
firewall-cmd --state

3.2.2 區(qū)域管理

# 查看默認(rèn)區(qū)域
firewall-cmd --get-default-zone

# 設(shè)置默認(rèn)區(qū)域
firewall-cmd --set-default-zone=public

# 查看活動區(qū)域
firewall-cmd --get-active-zones

# 查看所有區(qū)域
firewall-cmd --get-zones

# 查看區(qū)域信息
firewall-cmd --zone=public --list-all

3.2.3 服務(wù)管理

# 查看可用服務(wù)
firewall-cmd --get-services

# 查看已開放的服務(wù)
firewall-cmd --list-services

# 添加服務(wù)
firewall-cmd --add-service=http
firewall-cmd --add-service=https

# 刪除服務(wù)
firewall-cmd --remove-service=http

# 永久添加服務(wù)
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 自定義服務(wù)

# 創(chuàng)建自定義服務(wù)配置文件
cat> /etc/firewalld/services/myapp.xml <

 MyApp
 My Application Service
 
 

EOF

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

# 添加自定義服務(wù)
firewall-cmd --add-service=myapp

3.3.2 富規(guī)則配置

# 允許特定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'

# 端口轉(zhuǎn)發(fā)
firewall-cmd --add-rich-rule='rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'

3.3.3 接口綁定

# 將接口綁定到區(qū)域
firewall-cmd --zone=internal --add-interface=eth1

# 查看接口綁定
firewall-cmd --get-zone-of-interface=eth1

# 更改接口區(qū)域
firewall-cmd --zone=public --change-interface=eth1

3.3.4 IP偽裝和端口轉(zhuǎn)發(fā)

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

# 端口轉(zhuǎn)發(fā)
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100

# 查看轉(zhuǎn)發(fā)規(guī)則
firewall-cmd --list-forward-ports

4. 實(shí)戰(zhàn)配置案例

4.1 Web服務(wù)器防火墻配置

4.1.1 iptables配置

#!/bin/bash
# Web服務(wù)器防火墻配置腳本

# 清空現(xiàn)有規(guī)則
iptables -F
iptables -X
iptables -Z

# 設(shè)置默認(rèn)策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允許本地回環(huán)
iptables -A INPUT -i lo -j ACCEPT

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

# 允許SSH(限制連接數(shù))
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

# 保存規(guī)則
service iptables save

4.1.2 firewalld配置

#!/bin/bash
# Web服務(wù)器防火墻配置腳本

# 設(shè)置默認(rèn)區(qū)域
firewall-cmd --set-default-zone=public

# 添加服務(wù)
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 數(shù)據(jù)庫服務(wù)器防火墻配置

4.2.1 iptables配置

#!/bin/bash
# 數(shù)據(jù)庫服務(wù)器防火墻配置

# 清空現(xiàn)有規(guī)則
iptables -F
iptables -X
iptables -Z

# 設(shè)置默認(rèn)策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允許本地回環(huán)
iptables -A INPUT -i lo -j ACCEPT

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

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

# 允許MySQL(僅限應(yīng)用服務(wù)器)
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

# 保存規(guī)則
service iptables save

4.2.2 firewalld配置

#!/bin/bash
# 數(shù)據(jù)庫服務(wù)器防火墻配置

# 創(chuàng)建數(shù)據(jù)庫區(qū)域
firewall-cmd --permanent --new-zone=database

# 設(shè)置默認(rèn)區(qū)域
firewall-cmd --set-default-zone=database

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

# 添加MySQL服務(wù)(限制源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. 故障排查和優(yōu)化

5.1 常見問題排查

5.1.1 規(guī)則不生效

# 檢查規(guī)則是否正確添加
iptables -L -n -v
firewall-cmd --list-all

# 檢查服務(wù)狀態(tài)
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 性能優(yōu)化

5.2.1 規(guī)則優(yōu)化

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

# 使用狀態(tài)匹配減少規(guī)則數(shù)量
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 使用multiport匹配多個(gè)端口
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT

5.2.2 監(jiān)控和統(tǒng)計(jì)

# 查看規(guī)則統(tǒng)計(jì)
iptables -L -n -v --line-numbers

# 重置計(jì)數(shù)器
iptables -Z

# 實(shí)時(shí)監(jiān)控
watch -n 1'iptables -L -n -v'

6. 安全最佳實(shí)踐

6.1 基本安全原則

1.最小權(quán)限原則:只開放必要的端口和服務(wù)

2.白名單策略:默認(rèn)拒絕所有連接,只允許必要的連接

3.定期審計(jì):定期檢查和更新防火墻規(guī)則

4.日志監(jiān)控:啟用日志記錄并定期分析

6.2 配置建議

# 設(shè)置合理的默認(rèn)策略
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 備份和恢復(fù)

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

# 恢復(fù)iptables規(guī)則
iptables-restore < /root/iptables.backup.20231201

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

7. 總結(jié)

iptables和firewalld都是Linux系統(tǒng)中強(qiáng)大的防火墻工具。iptables提供了更底層的控制能力,適合對防火墻規(guī)則有詳細(xì)要求的場景;firewalld則提供了更友好的管理界面和動態(tài)配置能力,更適合日常運(yùn)維管理。

選擇使用哪種工具主要取決于具體的應(yīng)用場景和個(gè)人偏好。在實(shí)際部署中,建議根據(jù)系統(tǒng)的具體需求制定合適的防火墻策略,并定期進(jìn)行安全審計(jì)和規(guī)則優(yōu)化。

記住防火墻只是網(wǎng)絡(luò)安全的一個(gè)組成部分,還需要結(jié)合其他安全措施(如入侵檢測、日志監(jiān)控、定期更新等)來構(gòu)建完整的安全防護(hù)體系。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11509

    瀏覽量

    213742
  • 防火墻
    +關(guān)注

    關(guān)注

    0

    文章

    435

    瀏覽量

    36186
  • 網(wǎng)絡(luò)安全
    +關(guān)注

    關(guān)注

    11

    文章

    3339

    瀏覽量

    61454

原文標(biāo)題:Linux防火墻終極對決:iptables與firewalld完整配置教程

文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    Linux系統(tǒng)iptables和firewall防火墻的配置方法

    防火墻就是根據(jù)系統(tǒng)管理員設(shè)定的規(guī)則來控制數(shù)據(jù)包的進(jìn)出,主要是保護(hù)內(nèi)網(wǎng)的安全,目前 Linux 系統(tǒng)防火墻類型主要有兩種:分別是 [
    發(fā)表于 07-17 10:34 ?1671次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>系統(tǒng)</b><b class='flag-5'>iptables</b>和firewall<b class='flag-5'>防火墻</b>的配置方法

    Linux中使用Iptables實(shí)現(xiàn)簡單的網(wǎng)站防火墻

    Linux中使用Iptables實(shí)現(xiàn)一個(gè)簡單的網(wǎng)站防火墻可以幫助我們保護(hù)服務(wù)器不受到惡意請求的攻擊。一個(gè)現(xiàn)實(shí)的場景是我們想要阻止某個(gè)IP地址訪問我們的網(wǎng)站。
    <b class='flag-5'>Linux</b>中使用<b class='flag-5'>Iptables</b>實(shí)現(xiàn)簡單的網(wǎng)站<b class='flag-5'>防火墻</b>

    Linux系統(tǒng)firewalld防火墻實(shí)戰(zhàn)指南

    本文浩道跟大家分享Linux系統(tǒng)firewalld防火墻硬核干貨,通過對其介紹、相關(guān)概念回顧,數(shù)據(jù)包處理流程、其安裝過程、其常用命令用法、其添加規(guī)則相關(guān)用法去開展,讓大家在****
    發(fā)表于 10-16 16:47 ?1315次閱讀
    <b class='flag-5'>Linux</b><b class='flag-5'>系統(tǒng)</b><b class='flag-5'>firewalld</b><b class='flag-5'>防火墻</b>實(shí)戰(zhàn)指南

    Linux內(nèi)核防火墻netfilter的原理和應(yīng)用

    操作系統(tǒng)得到了迅速發(fā)展,而Linux內(nèi)核中所內(nèi)嵌的防火墻起著重要作用。在Linux1.1版中使用的防火墻是從BSD UNIX
    發(fā)表于 09-19 09:22

    Linux防火墻的配置方法(firewalld服務(wù))

    紅帽RHEL7系統(tǒng)已經(jīng)用firewalld服務(wù)替代了iptables服務(wù),新的防火墻管理命令firewall-cmd與圖形化工具firewall-config。執(zhí)行firewall-c
    發(fā)表于 10-30 12:27

    Linux下關(guān)閉防火墻的關(guān)閉及開放端口

    :service iptables stop3.查看防火墻狀態(tài)serviceiptablesstatus重啟服務(wù)service iptables restart實(shí)現(xiàn)配置文件及時(shí)生效source文件路徑
    發(fā)表于 07-09 06:23

    Linux如何關(guān)閉防火墻

    1、查看防火墻規(guī)則vim /etc/sysconfig/iptables放開某個(gè)端口號不被防火墻攔截,適用于部署tomcat,nginx等之類的軟件
    發(fā)表于 07-12 06:31

    請問如何自動加載iptables防火墻規(guī)則?

    系統(tǒng)啟動后,如何自動加載iptables防火墻規(guī)則?
    發(fā)表于 01-07 08:46

    基于Linux防火墻的可視化管理系統(tǒng)的研究與實(shí)現(xiàn)

    針對Iptables 命令配置防火墻規(guī)則的復(fù)雜性,本系統(tǒng)研究和實(shí)現(xiàn)了基于linux 防火墻的可視化管理
    發(fā)表于 07-16 08:47 ?16次下載

    Linux新型內(nèi)核防火墻研究和應(yīng)用

    Linux新型防火墻netfilter框架原理及工作機(jī)制基礎(chǔ)上,研究了該防火墻的應(yīng)用設(shè)計(jì), 提出了用netfilter/iptables構(gòu)建門戶服務(wù)器
    發(fā)表于 08-25 11:35 ?14次下載

    Linux防火墻配置(iptablesfirewalld)

    防火墻就是根據(jù)系統(tǒng)管理員設(shè)定的規(guī)則來控制數(shù)據(jù)包的進(jìn)出,主要是保護(hù)內(nèi)網(wǎng)的安全,目前 Linux 系統(tǒng)防火墻類型主要有兩種:分別是 [
    的頭像 發(fā)表于 03-31 10:09 ?1463次閱讀

    請問Centos7如何配置firewalld防火墻規(guī)則?

    Firewalld是CentOS系統(tǒng)自帶的一種動態(tài)防火墻管理工具。是一個(gè)前端工具,用于管理Linux系統(tǒng)上的netfilter
    的頭像 發(fā)表于 10-09 09:33 ?1909次閱讀
    請問Centos7如何配置<b class='flag-5'>firewalld</b><b class='flag-5'>防火墻</b>規(guī)則?

    Linux軟件防火墻iptables詳解

    Linux提供的軟件防火墻,名為iptables,它可以理解為是一個(gè)客戶端代理,通過iptables的代理,將用戶配置的安全策略執(zhí)行到對應(yīng)的安全框架
    的頭像 發(fā)表于 03-01 14:50 ?639次閱讀
    <b class='flag-5'>Linux</b>軟件<b class='flag-5'>防火墻</b><b class='flag-5'>iptables</b>詳解

    如何在CentOS系統(tǒng)配置防火墻

    作為一名系統(tǒng)管理員或開發(fā)者,你是否曾經(jīng)被 Linux 防火墻配置搞得頭大?在生產(chǎn)環(huán)境,我們經(jīng)常需要配置防火墻來保護(hù)服務(wù)器安全,但面對
    的頭像 發(fā)表于 05-08 11:52 ?396次閱讀
    如何在CentOS<b class='flag-5'>系統(tǒng)</b><b class='flag-5'>中</b>配置<b class='flag-5'>防火墻</b>

    Linux系統(tǒng)iptables防火墻配置詳解

    iptablesLinux內(nèi)核中用于配置防火墻規(guī)則的工具。它基于Netfilter框架,可以對通過網(wǎng)絡(luò)接口的數(shù)據(jù)包進(jìn)行過濾、修改等操作。通過設(shè)置一系列規(guī)則,iptables能夠控制哪
    的頭像 發(fā)表于 06-18 15:25 ?259次閱讀