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参数则规则重启后失效‌。

相关推荐
未济1 天前
linux 配置环境变量
linux
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.1 天前
进程间通信
linux
AI职业加油站1 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-051 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏1 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz1 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅1 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK1 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid