CentOS的防火墙工具(firewalld和iptables)的使用

CentOS的防火墙工具因版本不同而异,以下是具体操作步骤:

一、firewalld(CentOS 7及以上默认工具)‌

‌1、安装与启动‌:

安装:sudo yum install firewalld

启动服务:sudo systemctl start firewalld

开机自启:sudo systemctl enable firewalld‌

2‌、开放端口‌:

永久开放单个端口(如8080):
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent

开放多个端口:
sudo firewall-cmd --zone=public --add-port=9001/tcp --add-port=9002/tcp --permanent‌

立即生效:sudo firewall-cmd --reload‌


3、验证与管理‌:

查看已开放端口:sudo firewall-cmd --list-ports

4、删除端口规则:
sudo firewall-cmd --zone=public --remove-port=80/tcp --permanent‌

二、iptables(CentOS 6或手动切换的工具)‌
1‌、安装与启动‌:

安装:sudo yum install iptables-services

启动服务:sudo systemctl start iptables

开机自启:sudo systemctl enable iptables‌

2‌、开放端口‌:

添加规则(如8080):
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

3、保存规则CentOS 7及以下):sudo service iptables save‌


4、切换工具CentOS 8)‌:

需先禁用firewalld
sudo systemctl stop firewalld && sudo systemctl disable firewalld‌

三、注意事项‌
‌版本差异‌CentOS 6默认使用iptables,需直接编辑/etc/sysconfig/iptables文件并重启服务生效‌。
‌区域管理‌firewalld支持多区域配置,可通过--zone参数指定(如public)‌。
‌临时规则‌ :省略--permanent参数则规则重启后失效‌。

相关推荐
chlk12320 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑20 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件21 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒21 小时前
OpenClaw 多 Agent 配置实战指南
运维
xy1230621 小时前
OpenStack Train 部署实战(三)控制节点--keystone服务
centos·openstack
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux