在 Linux 系统中,防火墙通常由 iptablesfirewalldufw 管理。以下是查看防火墙状态的常用方法:


1. 检查防火墙是否启用

1.1 使用 systemctl 检查防火墙服务状态

  • 对于 firewalld

    sudo systemctl status firewalld
    

    如果显示 active (running),表示 firewalld 正在运行。如果显示inactive (dead),表示firewalld未运行。

  • 对于 ufw(Ubuntu/Debian):

    sudo systemctl status ufw
    

    如果显示 active (running),表示 ufw 正在运行。

  • 对于 iptables

    sudo systemctl status iptables
    

    如果显示 active (running),表示 iptables 正在运行。


1.2 查看防火墙规则

1.2.1 使用 firewalld

  • 查看当前区域和规则:

    sudo firewall-cmd --list-all
    

    输出示例:

    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: eth0
      sources:
      services: ssh dhcpv6-client
      ports:
      protocols:
      masquerade: no
      forward-ports:
      source-ports:
      icmp-blocks:
      rich rules:
    
  • 查看开放的端口:

    sudo firewall-cmd --list-ports
    

1.2.2 使用 ufw

  • 查看防火墙状态和规则:
    sudo ufw status verbose
    
    输出示例:
    Status: active
    To                         Action      From
    --                         ------      ----
    22/tcp                     ALLOW       Anywhere
    80/tcp                     ALLOW       Anywhere
    

1.2.3 使用 iptables

  • 查看所有规则:
    sudo iptables -L -n -v
    
    输出示例:
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination
       0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination
    
    Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination
    

3. 检查防火墙是否阻止了特定端口

3.1 使用 nmap

安装 nmap

sudo yum install nmap  # CentOS/RHEL
sudo apt install nmap  # Ubuntu/Debian

扫描本地端口:

nmap -sT -p 22,80,3306 localhost

输出示例:

Starting Nmap 7.80 ( https://nmap.org ) at 2023-10-01 12:00 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00010s latency).
PORT     STATE  SERVICE
22/tcp   open   ssh
80/tcp   closed http
3306/tcp closed mysql

4. 临时关闭防火墙

4.1 关闭 firewalld

sudo systemctl stop firewalld

4.2 关闭 ufw

sudo ufw disable

4.3 清空 iptables 规则

sudo iptables -F

5. 永久关闭防火墙

5.1 禁用 firewalld

sudo systemctl disable firewalld

5.2 禁用 ufw

sudo ufw disable

5.3 清空并保存 iptables 规则

sudo iptables -F
sudo service iptables save

6. 总结

  • 使用 systemctl 检查防火墙服务状态。
  • 使用 firewall-cmdufwiptables 查看防火墙规则。
  • 使用 nmap 检查端口是否开放。
  • 如果需要,可以临时或永久关闭防火墙。

根据你的需求选择合适的方法查看或管理防火墙。如果问题仍然存在,请提供更多上下文信息(如操作系统版本、防火墙工具等),以便进一步分析。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐