V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
salamanderMH
V2EX  ›  问与答

容器配置了 Bridge 和 NAT 后还是没法访问外网?

  •  
  •   salamanderMH · 2020-04-28 16:09:25 +08:00 · 775 次点击
    这是一个创建于 1457 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题

    按照了文章,我也给一个容器手动配置了网络,最后给br666这个 Bridge 配置 NAT 后

    sudo iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o enp7s0 -j MASQUERADE
    

    在 Ubuntu 容器里 ping 还是不通的

    root@6414d7278905:/# ping 8.8.8.8
    PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
    
    

    我在宿主机上 tcpdump 还是能看到 ICMP 包的

    sudo tcpdump icmp -i br666
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on br666, link-type EN10MB (Ethernet), capture size 262144 bytes
    16:07:58.270987 IP 10.0.0.2 > dns.google: ICMP echo request, id 78, seq 73, length 64
    16:07:59.295299 IP 10.0.0.2 > dns.google: ICMP echo request, id 78, seq 74, length 64
    16:08:00.319332 IP 10.0.0.2 > dns.google: ICMP echo request, id 78, seq 75, length 64
    

    echo requestecho reply,应该是 iptables 配置错了吧

    1 条回复    2020-04-29 13:37:58 +08:00
    salamanderMH
        1
    salamanderMH  
    OP
       2020-04-29 13:37:58 +08:00
    看了下 https://askubuntu.com/questions/1052789/correct-way-to-route-between-2-interfaces-with-netplan-in-ubuntu-18-04 这里
    是需要添加 FORWARD 链
    我这里的情况的话,就是要
    ```
    sudo iptables -A FORWARD -i br666 -o br666 -j ACCEPT
    sudo iptables -A FORWARD -i br666 ! -o br666 -j ACCEPT
    sudo iptables -A FORWARD ! -i br666 -o br666 -j ACCEPT
    ```
    按 Docker0 的那个网桥的 FORWARD 的规则来的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1044 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 106ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.