CentOS查看iptable:詳解防火墻規(guī)則查詢方法
iptables的基本概念
iptables是Linux系統(tǒng)中用于管理網(wǎng)絡(luò)流量的核心防火墻工具,通過預(yù)定義規(guī)則控制數(shù)據(jù)包的傳輸與攔截。在CentOS系統(tǒng)中,通過命令行操作可快速查看當前防火墻配置。
查看iptables規(guī)則的命令
1. 列出所有規(guī)則
sudo iptables -L -n -v
-L
顯示規(guī)則列表,-n
禁止域名解析(加快顯示速度),-v
輸出詳細信息。
2. 查看NAT表規(guī)則
sudo iptables -t nat -L -v
通過-t nat
指定查看網(wǎng)絡(luò)地址轉(zhuǎn)換表,適用于端口轉(zhuǎn)發(fā)或IP偽裝場景。
3. 導(dǎo)出完整規(guī)則配置
sudo iptables-save
輸出當前所有規(guī)則的原始配置格式,可用于備份或遷移防火墻策略。
規(guī)則表字段解析
字段 | 說明 |
---|---|
Chain | 規(guī)則鏈名稱(INPUT/OUTPUT/FORWARD) |
target | 數(shù)據(jù)包處理動作(ACCEPT/DROP/REJECT) |
prot | 協(xié)議類型(tcp/udp/icmp) |
source | 源IP地址或網(wǎng)段 |
destination | 目標IP地址或網(wǎng)段 |
常見問題與解決方案
Q: 執(zhí)行命令提示權(quán)限不足
使用sudo
或以root用戶身份運行命令,例如:sudo iptables -L
Q: 規(guī)則顯示為空
檢查iptables服務(wù)是否啟動:systemctl status iptables
(CentOS 6)或確認firewalld是否已禁用(CentOS 7+)。
注意事項
- CentOS 7及以上版本默認使用firewalld,如需使用iptables需先禁用firewalld:
sudo systemctl stop firewalld && sudo systemctl disable firewalld
- 修改規(guī)則前建議執(zhí)行
iptables-save > /path/to/backup.txt
備份配置