V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
hutoer
V2EX  ›  Linux

Linux 服务器开启 firewalld 后,无法访问公网网站

  •  
  •   hutoer · 263 天前 · 1259 次点击
    这是一个创建于 263 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Linux 服务器开启 firewalld 后,无法访问公网网站

    Linux OS:CentOS 7

    没有启动 iptables 服务,仅仅启动 firewalld 服务,启动后一开始能访问公网网站,大约 5 分钟后,无法访问公网网站。

    能 ping 通公网网站域名

    # firewall-cmd --list-all
    public (default, active)
      interfaces: eno1
      sources: 
      services: 
      ports: 58426/tcp 80/tcp
      masquerade: no
      forward-ports: 
      icmp-blocks: 
      rich rules: 
    
    # iptables -L -n
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
    INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
    INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
    INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
    FORWARD_direct  all  --  0.0.0.0/0            0.0.0.0/0           
    FORWARD_IN_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
    FORWARD_IN_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
    FORWARD_OUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
    FORWARD_OUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    OUTPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
    
    Chain FORWARD_IN_ZONES (1 references)
    target     prot opt source               destination         
    FWDI_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
    FWDI_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
    
    Chain FORWARD_IN_ZONES_SOURCE (1 references)
    target     prot opt source               destination         
    
    Chain FORWARD_OUT_ZONES (1 references)
    target     prot opt source               destination         
    FWDO_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
    FWDO_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
    
    Chain FORWARD_OUT_ZONES_SOURCE (1 references)
    target     prot opt source               destination         
    
    Chain FORWARD_direct (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_public (2 references)
    target     prot opt source               destination         
    FWDI_public_log  all  --  0.0.0.0/0            0.0.0.0/0           
    FWDI_public_deny  all  --  0.0.0.0/0            0.0.0.0/0           
    FWDI_public_allow  all  --  0.0.0.0/0            0.0.0.0/0           
    
    Chain FWDI_public_allow (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_deny (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_log (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_public (2 references)
    target     prot opt source               destination         
    FWDO_public_log  all  --  0.0.0.0/0            0.0.0.0/0           
    FWDO_public_deny  all  --  0.0.0.0/0            0.0.0.0/0           
    FWDO_public_allow  all  --  0.0.0.0/0            0.0.0.0/0           
    
    Chain FWDO_public_allow (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_deny (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_log (1 references)
    target     prot opt source               destination         
    
    Chain INPUT_ZONES (1 references)
    target     prot opt source               destination         
    IN_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
    IN_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto] 
    
    Chain INPUT_ZONES_SOURCE (1 references)
    target     prot opt source               destination         
    
    Chain INPUT_direct (1 references)
    target     prot opt source               destination         
    
    Chain IN_public (2 references)
    target     prot opt source               destination         
    IN_public_log  all  --  0.0.0.0/0            0.0.0.0/0           
    IN_public_deny  all  --  0.0.0.0/0            0.0.0.0/0           
    IN_public_allow  all  --  0.0.0.0/0            0.0.0.0/0           
    
    Chain IN_public_allow (1 references)
    target     prot opt source               destination         
    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:58426 ctstate NEW
    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 ctstate NEW
    
    Chain IN_public_deny (1 references)
    target     prot opt source               destination         
    
    Chain IN_public_log (1 references)
    target     prot opt source               destination         
    
    Chain OUTPUT_direct (1 references)
    target     prot opt source               destination         
    

    firewalld 的配置非常简单,仅仅打开 ports: 58426/tcp 80/tcp

    大家给看看要如何排查

    13 条回复    2023-08-23 11:35:41 +08:00
    dode
        1
    dode  
       263 天前
    防火墙启动后只会阻止新建连接,所以开始可能能打开,你关掉浏览器程序,重新试试,
    你的电脑有几个网口?
    把对外服务的网口更改为 public 区域

    firewall-cmd --zone=public --change-interface=xxx --permanent
    LPLaser
        2
    LPLaser  
       263 天前
    可能是 outpu 指向的 chain 里没有规则然后就默认 drop 掉了?
    可以试试先把 OUPUT 的策略清空试试
    hutoer
        3
    hutoer  
    OP
       263 天前
    @dode 没用浏览器,使用的是 curl 命令行
    服务器 2 个网口
    访问公网的网口是在 public 区域的

    firewall-cmd --get-active-zones
    public
    interfaces: eno1
    hutoer
        4
    hutoer  
    OP
       263 天前
    @LPLaser 规则是 firewalld 服务自动产生的
    我看过能访问网站时和不能访问时的规则是一样的
    dode
        5
    dode  
       263 天前
    还有就是网站 80 等端口备案问题,你换 8000 ,81 比较一下
    ufok
        6
    ufok  
       263 天前
    发网址出来,让大家搜一下
    gvdlmjwje
        7
    gvdlmjwje  
       263 天前   ❤️ 1
    我就吐槽下 firewalld 的命令真是繁琐的一逼
    GoodRui
        8
    GoodRui  
       263 天前 via Android
    @gvdlmjwje 是和 ufw 比吗?目前 Linux 中好用成熟的防火墙都有哪些啊?
    ysc3839
        9
    ysc3839  
       263 天前 via Android
    @gvdlmjwje @GoodRui
    个人觉得 firewalld 不用高级功能的话还是挺好用的,ufw 不支持端口转发,要用的时候更麻烦。
    ShallowAi
        10
    ShallowAi  
       262 天前
    是无法入网(被访问)还是无法出网(向外 ping )?如果是无法入网需要检查是否备案,tcpping 指定 80 端口,不行再换个其他端口试试看。

    一些其他说明:其实 firewalld 是个防火墙前端,底层调用 iptables (现在的实现是 nftables )。
    hutoer
        11
    hutoer  
    OP
       262 天前
    @ShallowAi 是无法出网
    busier
        12
    busier  
       248 天前 via Android
    @hutoer 能 ping 通公网网站域名说的不够明确!是一直在 ping 同一个,还是网不通后,再 ping 其它也能通。毕竟 dns 有缓存,而且一直 ping 的话,也不会重复 dns 解析。

    既然始终能 ping 通,说明到达目标路由和 icmp 协议放行没问题

    建议在不通的时候 用 nslookup 或 dig 诊断下 DNS
    hutoer
        13
    hutoer  
    OP
       248 天前
    @busier 始终能 ping 通,与 DNS 无关。将域名改为公网 IP ,故障依旧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2826 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:14 · PVG 20:14 · LAX 05:14 · JFK 08:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.