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

相关推荐
江畔柳前堤4 分钟前
github实战指南01-账号配置与 SSH 密钥
运维·人工智能·深度学习·ssh·github·pyqt·信号处理
Moshow郑锴1 小时前
Ubuntu 26.04 中文输入法 : fcitx5+Rime中州韵引擎
linux·运维·ubuntu
qq_163135753 小时前
Linux 【04-more命令超详细教程】
linux
sevencheng7984 小时前
【ADB】adb命令行常用按键模拟代码
linux·adb·模拟按键,返回键,音量键
暗影天帝4 小时前
BPI-R3 Mini 刷 Yuzhii DHCPD U-Boot 教程
linux
小赖同学啊4 小时前
智能连接器集群化高可用生产方案
linux·运维·人工智能
wanghao6664554 小时前
DevOps 从入门到实践:构建高效交付流水线
运维·devops
qq_546937274 小时前
从“能用”到“超神”,DeepSeek++给网页版装上“大脑”和“手脚”,支持长期记忆、MCP工具与自动化任务!
运维·自动化
ZStack开发者社区4 小时前
基于AI Agent的ZCF API文档全链路自动化
运维·人工智能·自动化
Cinema KI4 小时前
Linux第一个系统程序-进度条
linux·服务器