在Linux系统中,防火墙是保障服务器安全的重要屏障,通过合理配置防火墙规则可以有效控制网络访问,防止未授权访问和恶意攻击,不同Linux发行版使用的防火墙工具有所不同,常见的有iptables(CentOS 7之前)、firewalld(CentOS 7及以后版本、RHEL、Fedora等)以及nftables(较新的Linux发行版),以下将详细介绍这些防火墙的开启命令及相关配置方法,帮助用户根据实际需求选择合适的工具进行操作。

基于iptables的防火墙开启命令
iptables是Linux系统中传统的防火墙工具,通过内核模块实现包过滤功能,功能强大且灵活,在CentOS 6、Ubuntu 14.04等较老版本中,默认使用iptables作为防火墙解决方案。
安装iptables
如果系统中未安装iptables,可通过以下命令安装:
# CentOS/RHEL系统 yum install iptables -y # Ubuntu/Debian系统 apt-get update apt-get install iptables -y
开启iptables服务
安装完成后,需启动iptables服务并设置为开机自启:
# 启动iptables服务 service iptables start # CentOS 6系统设置开机自启 chkconfig iptables on # Ubuntu系统设置开机自启 update-rc.d iptables enable
添加基本规则
iptables的规则由表(table)和链(chain)组成,默认使用filter表,包含INPUT(入站)、OUTPUT(出站)、FORWARD(转发)三条链,开启防火墙后,需添加允许必要服务的规则,避免因拦截所有流量导致服务不可用,以下为常用规则示例:

# 允许本地回环地址(localhost) iptables -A INPUT -i lo -j ACCEPT # 允许已建立的连接和相关连接 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 允许SSH连接(默认端口22) iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许HTTP(80端口)和HTTPS(443端口)访问 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 拦截所有其他入站流量(默认策略设置为DROP) iptables -P INPUT DROP
保存规则
不同系统中保存iptables规则的命令不同:
# CentOS/RHEL系统 service iptables save # Ubuntu/Debian系统 iptables-save > /etc/iptables/rules.v4
查看和删除规则
# 查看当前规则 iptables -L -n -v # 删除某条规则(例如删除SSH允许规则) iptables -D INPUT -p tcp --dport 22 -j ACCEPT
基于firewalld的防火墙开启命令
firewalld是CentOS 7、RHEL 7、Fedora等系统默认的防火墙管理工具,支持动态规则更新,无需重启服务即可生效,适合需要频繁修改规则的场景。
检查firewalld状态
# 查看firewalld运行状态 systemctl status firewalld # 如果未启动,执行以下命令开启并设置开机自启 systemctl start firewalld systemctl enable firewalld
使用firewall-cmd管理规则
firewall-cmd是firewalld的命令行工具,以下为常用操作:
# 查看已开放的端口 firewall-cmd --list-ports # 开放永久端口(例如SSH 22端口) firewall-cmd --permanent --add-port=22/tcp # 开放临时端口(重启后失效) firewall-cmd --add-port=80/tcp # 重新加载防火墙规则使永久规则生效 firewall-cmd --reload # 添加服务(例如http服务,自动开放80端口) firewall-cmd --permanent --add-service=http # 查看已启用的服务 firewall-cmd --list-services # 设置默认区域(public为默认区域) firewall-cmd --get-default-zone firewall-cmd --set-default-zone=public # 添加富规则(允许特定IP访问22端口) firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
区域(Zone)管理
firewalld将网络流量分为不同的区域(如public、trusted、block等),每个区域有不同的默认规则,可通过以下命令管理区域:

# 查看所有区域 firewall-cmd --get-active-zones # 将网络接口添加到指定区域(例如eth0加入trusted区域) firewall-cmd --permanent --zone=trusted --add-interface=eth0
基于nftables的防火墙开启命令
nftables是iptables的替代方案,在Linux内核3.13版本后引入,提供了更简洁的语法和更好的性能,逐渐成为主流防火墙工具(如Ubuntu 20.04、CentOS 8默认使用nftables)。
安装nftables
# CentOS/RHEL 8系统 dnf install nftables -y # Ubuntu 20.04系统 apt-get install nftables -y
启动nftables服务
systemctl start nftables systemctl enable nftables
基本规则配置
nftables规则通过nft
命令管理,配置文件通常位于/etc/nftables/
目录下,以下为基本规则示例:
# 创建一个表名为firewall的表 nft add table inet firewall # 在表中创建链 nft add chain inet firewall input { type filter hook input priority 0 \; } # 添加规则 nft add rule inet firewall input iif lo accept nft add rule inet firewall input tcp dport 22 accept nft add rule inet firewall input tcp dport 80 accept nft add rule inet firewall input tcp dport 443 accept nft add rule inet firewall input counter drop # 保存规则(CentOS 8系统) nft list ruleset > /etc/sysconfig/nftables.conf # Ubuntu系统规则保存 nft list ruleset > /etc/nftables/ruleset.nft
加载和查看规则
# 加载保存的规则 nft -f /etc/sysconfig/nftables.conf # 查看当前规则 nft list ruleset
防火墙开启后的注意事项
- 规则优先级:防火墙规则按顺序匹配,一旦匹配某条规则即停止向下检查,因此需将允许规则置于拦截规则之前。
- 默认策略:iptables和nftables可设置默认策略(如DROP或ACCEPT),建议在测试环境验证规则无误后再设置为DROP,避免合法流量被误拦截。
- 日志记录:可通过
iptables -A INPUT -j LOG
或nftables的log
选项记录被拦截的流量,便于分析安全事件。 - 远程管理安全:开启防火墙后,确保SSH等管理端口允许访问,建议限制特定IP访问,避免暴露在公网中。
不同工具对比
特性 | iptables | firewalld | nftables |
---|---|---|---|
默认支持系统 | CentOS 6及以前 | CentOS 7+ | CentOS 8+、Ubuntu 20.04+ |
规则更新方式 | 需重启服务 | 动态更新 | 动态更新 |
语法复杂度 | 复杂 | 中等 | 简洁 |
性能 | 较低 | 中等 | 较高 |
适用场景 | 老系统维护 | 日常管理 | 新系统部署 |
相关问答FAQs
问题1:如何查看当前系统使用的防火墙类型?
解答:可通过以下命令判断系统使用的防火墙工具:
# 检查iptables是否运行 service iptables status 2>/dev/null || systemctl status iptables 2>/dev/null # 检查firewalld是否运行 systemctl status firewalld # 检查nftables规则是否存在 nft list ruleset 2>/dev/null || ls /etc/nftables/ 2>/dev/null
若iptables
服务运行,则使用iptables;若firewalld
服务运行,则使用firewalld;若存在nftables规则文件或nft
命令可执行,则使用nftables。
问题2:开启防火墙后无法访问服务器怎么办?
解答:可按以下步骤排查:
- 检查防火墙状态:确认防火墙已启动(如
systemctl status firewalld
或service iptables status
)。 - 查看规则日志:通过
journalctl -u firewalld
或iptables -L -v
查看是否有流量被拦截。 - 临时开放端口:若因规则拦截导致无法访问,可临时开放端口(如
firewall-cmd --add-port=22/tcp
或iptables -I INPUT -p tcp --dport 22 -j ACCEPT
),然后重新加载规则。 - 检查默认策略:确保默认策略未设置为
DROP
(如iptables -P INPUT ACCEPT
临时放行,再调整具体规则)。 - 安全组/云平台规则:若使用云服务器(如AWS、阿里云),需检查云平台的安全组是否开放了对应端口。