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


相关推荐
提笔惊蚂蚁6 分钟前
终端VS命令解释器(Linux & Windows)
linux·运维·windows
Nimsolax13 分钟前
Linux文件fd
linux
·云扬·26 分钟前
【Bug】CentOS 7 使用vim命令报错vim: command not found
centos·vim·bug
tb_first1 小时前
k8sday13数据存储(1.5/2)
linux·运维·服务器·云原生·容器·kubernetes
逢生博客3 小时前
Ubuntu 安装 gvm 管理 Go 语言开发环境
linux·ubuntu·golang·gvm
多吃蔬菜!!!4 小时前
vscode 搭建C/C++开发环境搭建(linux)
linux·c语言·c++
李李李li4 小时前
Ubuntu 22.04 安装tensorrt
linux·tensorrt
phoenix09816 小时前
Linux入门DAY29
linux·运维
入秋6 小时前
Linux服务器安装部署 Nginx、Redis、PostgreSQL、Docker
linux·前端
Mr. Cao code7 小时前
使用Tomcat Clustering和Redis Session Manager实现Session共享
java·linux·运维·redis·缓存·tomcat