CentOS 7 防火墙配置相关指令 + 防火墙开放端口

CentOS 7 防火墙配置相关指令 + 防火墙开放端口


一、防火墙相关指令

  1. 检查防火墙状态

    shell 复制代码
    systemctl status firewalld
    shell 复制代码
    firewall-cmd --state

    若输出显示 active (running),说明防火墙正在运行;若显示 inactive (dead),则表示防火墙当前未运行。`

  2. 开启防火墙

    shell 复制代码
    systemctl start firewalld
  3. 关闭防火墙

    临时关闭防火墙,运行此命令后,防火墙会立即停止运行,但是系统关机重启后,防火墙会自动重启。

    shell 复制代码
    systemctl stop firewalld

    永久关闭防火墙,运行此命令后,防火墙不会立即停止运行,系统关机重启后,防火墙已经关闭。

    shell 复制代码
    systemctl disable firewalld
  4. 重新启动防火墙

    shell 复制代码
    systemctl restart firewalld
  5. 设置防火墙开机自启

    shell 复制代码
    systemctl enable firewalld
  6. 检查防火墙是否开机自启

    shell 复制代码
    systemctl is-enabled firewalld
    • 若输出为 enabled:这表明防火墙服务已经被设置为开机自启,系统启动时会自动启动防火墙。
    • 若输出为 disabled:意味着防火墙服务没有设置为开机自启,系统启动时不会自动启动防火墙。
    • 若输出为 static:表示该服务是由系统核心服务启动的,无法通过systemctl enable或systemctl disable来设置其开机自启状态。不过firewalld一般不会出现这种情况。
  7. 开放端口

    ① 开放 TCP 协议 8080 端口(TCP)

    shell 复制代码
    firewall-cmd --zone=public --add-port=8080/tcp --permanent

    此命令会在公共区域(public)永久开放 8080 端口的 TCP 连接。

    ② 开放 UDP 协议 8080 端口(UDP)

    shell 复制代码
    firewall-cmd --zone=public --add-port=8080/udp --permanent
  8. 关闭端口

    ① 关闭 TCP 协议 8080 端口(TCP)

    shell 复制代码
    firewall-cmd --zone=public --remove-port=8080/tcp --permanent

    ② 关闭 UDP 协议 8080 端口(UDP)

    shell 复制代码
    firewall-cmd --zone=public --remove-port=8080/udp --permanent
  9. 重新加载防火墙规则

    shell 复制代码
    firewall-cmd --reload
  10. 检查已开放的防火墙端口

    shell 复制代码
    firewall-cmd --zone=public --list-ports
  11. 查看当前防火墙详细配置信息

    shell 复制代码
    firewall-cmd --list-all

二、开放端口

  1. 检查防火墙状态,如果未开启请先开启

    shell 复制代码
    systemctl status firewalld
  2. 开启防火墙

    shell 复制代码
    systemctl start firewalld
  3. 建议加入防火墙开机自启

    shell 复制代码
    systemctl enable firewalld
  4. 开放端口

    shell 复制代码
    firewall-cmd --zone=public --add-port=端口号/协议(tcp/udp) --permanent

    如开放 tcp 协议的 8080 端口

    shell 复制代码
    firewall-cmd --zone=public --add-port=8080/tcp --permanent
  5. 重新载入防火墙配置,让配置生效

    shell 复制代码
    firewall-cmd --reload
  6. 检查开放的端口

    shell 复制代码
    firewall-cmd --zone=public --list-ports

END 配置完成


相关推荐
weixin_4284984926 分钟前
Using Huge Pages in Linux for Big Data Processing
linux·服务器
java-王森1 小时前
List中多个数据相同,保留最新日期数据
linux·windows·list
钡铼技术物联网关1 小时前
模块化革命:树莓派CM5嵌入式工业计算机如何重构嵌入式系统开发边界
linux·arm开发·边缘计算
m0_519523102 小时前
Linux——线程互斥和同步
linux·运维
东城绝神2 小时前
《Linux运维总结:基于银河麒麟V10+ARM64架构CPU源码编译部署单实例redis7.2.6》
linux·运维·架构
诚丞成3 小时前
进程状态:Linux的幕后指挥管理,穿越操作系统进程的静与动
linux
pingxiaozhao3 小时前
gogs私服详细配置
linux
rainFFrain3 小时前
对锁进行封装
linux·运维·服务器·vscode
liuweidong08024 小时前
【Pandas】pandas Series to_xarray
linux·运维·pandas
一只栖枝5 小时前
RHCA核心课程技术解析4:红帽服务管理与自动化深度实践
linux·运维·自动化·rhca·红帽认证