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


相关推荐
武大打工仔1 小时前
如何使用 Alacritty 让你的 macOS 终端更加出色
linux
深思慎考1 小时前
LinuxC++——etcd分布式键值存储系统入门
linux·c++·etcd
爱倒腾的老唐2 小时前
02、命令行的介绍
linux·bash
mahuifa2 小时前
C++(Qt)软件调试---Linux动态库链接异常排查(38)
linux·c++·动态库·ldd·异常排查
SouthBay4932 小时前
eNSP、HCIA学习笔记
hcia·ensp·防火墙·vrp
深思慎考2 小时前
LinuxC++——etcd分布式键值存储系统API(libetcd-cpp-api3)下载与二次封装
linux·c++·etcd
前方一片光明3 小时前
Linux—升级openssh常见的问题与解决方案
linux·运维·服务器
siriuuus4 小时前
Linux rsyslog 日志服务及日志转发实践
linux·rsyslog
邂逅星河浪漫4 小时前
【DockerFile+Nginx+DockerCompose】前后端分离式项目部署(docker容器化方式)
nginx·docker·centos·部署·docker-compose·dockerfile·容器化部署
dawnsky.liu4 小时前
RHEL - 在离线的 RHEL 10 中部署 Lightspeed 命令行助手
linux·人工智能·ai