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

相关推荐
像风一样自由20206 小时前
52.Docker部署大模型应用从本地开发到服务化运行
运维·人工智能·docker·容器·大模型·智能体
sxgzzn6 小时前
如何利用电缆故障定位在线监测系统提升供电安全?
运维·重构·无人机·开关柜局放在线监测·gis局放在线监测
曦夜日长7 小时前
Git系统篇,Git本地使用(一):从“究极进化版“到三个区域的底层原理
大数据·linux·git·elasticsearch
mengchanmian7 小时前
VSFTP 服务器搭建(Centos)
运维·服务器·centos
shimly1234567 小时前
给断网无 sudo ubuntu22.04 机器安装 nodejs
linux
别动我齐刘海9 小时前
Linux 系统调试学习1
linux·运维·服务器·c++·学习·机器学习·机器人
Lsetea9 小时前
OpenSSL报certificate chain too long:error 22与verify_depth深度排查
linux·https·ssl证书·openssl·证书链
不像程序员的程序媛10 小时前
linux常见文本操作命令
linux·运维·服务器
wangbing112510 小时前
开发指南149-IotDB-常用命令
linux·运维·iotdb
Dovis(誓平步青云)10 小时前
常用命令藏得太深?怎么做一个快捷命令坞
运维·前端·自动化·音视频·prometheus