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


相关推荐
Lw老王要学习23 分钟前
CentOS 7.9达梦数据库安装全流程解析
linux·运维·数据库·centos·达梦
CRUD酱1 小时前
CentOS的yum仓库失效问题解决(换镜像源)
linux·运维·服务器·centos
zly35001 小时前
VMware vCenter Converter Standalone 转换Linux系统,出现两个磁盘的处理
linux·运维·服务器
Albert Edison1 小时前
【Python】函数
java·linux·python·pip
General_G2 小时前
Linux中的信号
linux·运维·服务器
诸神缄默不语2 小时前
当无法直接用apt instll时,Linux如何离线安装软件包(以make为例)
linux·运维·服务器
Sivan_Xin2 小时前
拒绝 If-Else 屎山:利用适配器模式(Adapter)构建第三方登录的“防腐层”实战
linux·python·适配器模式
learning-striving2 小时前
kali默认桌面Xfce切换为GNOME桌面
linux·运维·服务器·kali
源远流长jerry2 小时前
dpdk之kni处理dns案例
linux·网络·网络协议·ubuntu·ip
黄昏晓x2 小时前
Linux----进程控制
android·linux·运维