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 配置完成


相关推荐
戴为沐11 小时前
Linux内存扩容指南
linux
zylyehuo1 天前
Linux 彻底且安全地删除文件
linux
用户805533698031 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297911 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux
Web3探索者3 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo3 天前
Linux系统中网线与USB网络共享冲突
linux
Sokach10154 天前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
AlfredZhao5 天前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3106 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode6 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏