Linux服务器防火墙开启需根据系统版本选择工具:CentOS 7+及RHEL 7+默认使用firewalld,传统系统可用iptables,操作步骤:首先检查状态(systemctl status firewalld或iptables -L);开启服务并设置开机自启(systemctl start firewalld && systemctl enable firewalld);添加规则(如开放80端口:firewall-cmd --add-port=80/tcp --permanent);最后重载配置(firewall-cmd --reload),若使用iptables,需先安装iptables-services,通过iptables -A INPUT -p tcp --dport 80 -j ACCEPT添加规则,并执行service iptables save保存,操作前建议备份现有规则,确保安全策略符合业务需求。
在Linux服务器管理中,防火墙是保障系统安全的第一道防线,能有效阻止未授权访问、恶意攻击等风险,本文将详细介绍通过命令行开启Linux服务器防火墙的常用方法,涵盖主流发行版(如CentOS/RHEL、Ubuntu/Debian等)的操作步骤,帮助管理员快速配置并启用防火墙。
防火墙的重要性及准备工作
防火墙通过控制网络流量(允许/阻止特定端口、IP或协议),为服务器提供访问控制,在开启防火墙前,需确认以下事项:
- 明确服务器用途:根据服务(如Web、SSH、数据库等)确定需要开放的端口,避免因全开放导致安全风险。
- 确认系统发行版:不同Linux发行版使用的防火墙工具不同(如CentOS/RHEL常用
firewalld或iptables,Ubuntu/Debian常用ufw或iptables)。 - 备份当前配置:若服务器已有防火墙规则,建议先备份配置,避免误操作导致服务中断。
检查防火墙当前状态
在开启防火墙前,需先确认其当前运行状态,避免重复开启或冲突,以下是主流发行版的检查命令:

CentOS/RHEL(使用firewalld)
# 检查firewalld服务状态 systemctl status firewalld # 或查看防火墙规则(若已启动) firewall-cmd --list-all
CentOS/RHEL(使用iptables)
# 检查iptables服务状态 systemctl status iptables # 或查看当前规则链 iptables -L -n
Ubuntu/Debian(使用ufw)
# 检查ufw状态 ufw status # 或查看详细规则(若已启用) ufw status verbose
通用方法(检查端口监听)
# 查看已开放的端口(需配合防火墙规则) ss -tulnp | grep LISTEN
开启防火墙的命令行操作
CentOS/RHEL 7及以上(firewalld)
firewalld是CentOS 7/RHEL 7及更高版本的默认防火墙工具,支持动态管理规则。
(1)开启firewalld服务
# 启动firewalld sudo systemctl start firewalld # 设置开机自启(避免重启后失效) sudo systemctl enable firewalld
(2)验证状态
systemctl status firewalld # 应显示"active (running)" firewall-cmd --state # 输出"running"表示已启动
CentOS/RHEL 6及以下(iptables)
旧版系统(如CentOS 6)默认使用iptables,需通过service或systemctl管理。
(1)开启iptables服务
# 启动iptables(CentOS 6用service,CentOS 7+用systemctl) sudo service iptables start # CentOS 6 sudo systemctl start iptables # CentOS 7+(若未切换到firewalld) # 设置开机自启 sudo chkconfig iptables on # CentOS 6 sudo systemctl enable iptables # CentOS 7+
(2)保存规则(关键步骤)
iptables规则默认不保存,重启后会丢失,需手动保存:
# CentOS 6 sudo service iptables save # CentOS 7+ sudo iptables-save > /etc/sysconfig/iptables
Ubuntu/Debian(ufw)
ufw(Uncomplicated Firewall)是Ubuntu/Debian的简化防火墙工具,命令直观易用。
(1)开启ufw
# 启用ufw(首次启用会提示确认,输入y) sudo ufw enable # 设置开机自启(ufw默认开机自启,可手动确认) sudo systemctl enable ufw
(2)验证状态
sudo ufw status # 输出"Status: active"表示已启用
通用方法(iptables手动配置)
若系统未预装firewalld或ufw,可通过iptables手动配置(适用于所有发行版)。
(1)清空现有规则(避免冲突)
sudo iptables -F # 清空过滤规则 sudo iptables -X # 清空自定义链 sudo iptables -Z # 计数器清零
(2)设置默认策略(允许已连接,阻止新连接)
sudo iptables -P INPUT DROP # 默认阻止所有输入 sudo iptables -P FORWARD DROP # 默认阻止所有转发 sudo iptables -P OUTPUT ACCEPT # 默认允许所有输出
(3)开放必要端口(以SSH 22端口为例)
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许SSH访问
(4)保存规则(不同系统保存方式不同)
- CentOS/RHEL:
sudo service iptables save # 或 sudo iptables-save > /etc/sysconfig/iptables
- Ubuntu/Debian:
sudo apt-get install iptables-persistent # 安装保存工具 sudo netfilter-persistent save # 保存规则
配置常用规则(以开放端口为例)
开启防火墙后,需根据服务需求开放端口,以下是主流工具的端口配置示例:
firewalld开放端口
# 开放HTTP端口(80)--permanent表示永久生效(重启后仍有效) sudo firewall-cmd --permanent --add
文章版权声明:除非注明,否则均为xmsdn原创文章,转载或复制请以超链接形式并注明出处。

