Centos7关闭防火墙或添加端口

本文介绍如何在CentOS7中管理和配置firewall及iptables防火墙,包括安装、启动、关闭、查看状态等操作命令,并指导如何设置iptables以开放特定端口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下

1
2
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#systemctl disable firewalld.service
1
2
3
4
5
6
7
8
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled

Centos 7 firewall 命令:

查看已经开放的端口:

firewall-cmd --list-ports

开启端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

命令含义:

–zone #作用域

–add-port=80/tcp #添加端口,格式为:端口/通讯协议

–permanent #永久生效,没有此参数重启后失效

重启防火墙

firewall-cmd --reload #重启firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)


2、设置 iptables service

yum -y install iptables-services

如果要修改防火墙配置,如增加防火墙端口3306

vi /etc/sysconfig/iptables 

增加规则

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

保存退出后

systemctl restart iptables.service #重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

最后重启系统使设置生效即可。

<think>好的,我现在需要帮用户解决在CentOS 7上修改防火墙端口规则的问题。首先,用户之前问过如何保存iptables规则,现在转向CentOS 7防火墙配置,可能意识到CentOS 7默认使用firewalld而不是iptables,所以需要调整方法。 首先,用户可能不太清楚CentOS 7中firewalld和iptables的关系,者他们可能更习惯使用iptables,想切换回去。我需要先确认默认情况下CentOS 7使用firewalld,但用户也可能想继续用iptables-services。所以,我需要给出两种方法:使用firewalld和切换到iptables。 接下来,用户需要修改端口规则,比如开放关闭某个端口。使用firewalld的话,需要介绍firewall-cmd命令,包括添加端口、移除端口、重载配置等步骤。同时,要提醒用户永久生效需要--permanent参数,并记得reload。 如果用户更习惯iptables,可能需要指导他们如何禁用firewalld,安装iptables-services,然后使用传统的iptables命令来修改规则,并保存配置。这里需要注意systemctl命令来启用服务,以及保存规则的方法,比如使用service iptables save者直接iptables-save到配置文件。 还要考虑用户可能的错误操作,比如同时运行firewalld和iptables导致冲突,需要提醒他们禁用firewalld后再使用iptables。另外,对于开放端口的具体例子,比如SSH的2222端口,需要给出具体的命令示例,帮助用户理解。 另外,用户可能需要验证配置是否生效,所以需要包括检查防火墙状态的步骤,比如firewall-cmd --list-alliptables -L -n -v。最后,提醒用户测试连接,确保端口确实开放,避免配置错误导致无法访问。 总结下来,回答需要结构清晰,分步骤说明两种方法,给出具体命令示例,注意事项以及验证方法,确保用户能安全有效地修改端口规则。</think>--- ### **CentOS 7 修改防火墙端口规则指南** CentOS 7 默认使用 `firewalld` 管理防火墙,但也可以通过切换到 `iptables` 服务操作。以下是两种方式的详细步骤: --- ### **一、使用 firewalld(推荐方式)** #### **1. 查看当前开放的端口** ```bash sudo firewall-cmd --list-ports ``` #### **2. 开放新端口(以 TCP 2222 端口为例)** ```bash # 临时开放(立即生效,重启失效) sudo firewall-cmd --add-port=2222/tcp # 永久开放(需重载配置) sudo firewall-cmd --permanent --add-port=2222/tcp sudo firewall-cmd --reload ``` #### **3. 关闭端口** ```bash # 临时关闭 sudo firewall-cmd --remove-port=2222/tcp # 永久关闭 sudo firewall-cmd --permanent --remove-port=2222/tcp sudo firewall-cmd --reload ``` #### **4. 验证配置** ```bash # 检查生效规则 sudo firewall-cmd --list-all ``` --- ### **二、切换回 iptables 服务** 如果习惯使用传统 iptables,需先禁用 `firewalld`: ```bash # 停止并禁用 firewalld sudo systemctl stop firewalld sudo systemctl disable firewalld # 安装 iptables 服务 sudo yum install iptables-services # 启动并启用服务 sudo systemctl start iptables sudo systemctl enable iptables ``` #### **1. 开放端口(以 TCP 2222 为例)** ```bash # 添加规则 sudo iptables -A INPUT -p tcp --dport 2222 -j ACCEPT # 保存规则(自动写入 /etc/sysconfig/iptables) sudo service iptables save # 重启服务 sudo systemctl restart iptables ``` #### **2. 关闭端口** ```bash # 删除对应规则(先查看规则编号) sudo iptables -L -n --line-numbers # 根据编号删除(假设规则编号为 3) sudo iptables -D INPUT 3 # 保存并重启 sudo service iptables save sudo systemctl restart iptables ``` --- ### **三、关键操作示例** #### **场景:开放 HTTP/HTTPS 端口** ```bash # 使用 firewalld sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload # 使用 iptables sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT sudo service iptables save ``` --- ### **四、注意事项** 1. **优先级问题**: - 如果同时使用 `firewalld` 和 `iptables`,可能导致规则冲突,建议只选择一种工具。 2. **端口范围操作**: ```bash # 开放 5000-6000 端口范围 sudo firewall-cmd --permanent --add-port=5000-6000/tcp ``` 3. **富规则(高级过滤)**: ```bash # 仅允许 192.168.1.0/24 访问 2222 端口 sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="2222" protocol="tcp" accept' ``` 4. **应急恢复**: - 如果误操作导致无法连接,可通过服务器控制台(如VNC)登录,清空规则: ```bash sudo iptables -F sudo service iptables save ``` --- ### **五、验证端口是否生效** ```bash # 使用 netstat 检查监听 sudo netstat -tuln | grep 2222 # 使用 telnet 测试(从其他机器) telnet 服务器IP 2222 # 查看防火墙日志 sudo tail -f /var/log/messages | grep firewall ``` --- 通过以上步骤,可安全地修改 CentOS 7 防火墙端口规则。**推荐优先使用 `firewalld`**,其动态管理特性更适合现代网络环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值