V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
salamanderMH
V2EX  ›  问与答

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

  •  
  •   salamanderMH · Apr 28, 2020 · 1135 views
    This topic created in 2203 days ago, the information mentioned may be changed or developed.

    问题

    按照了文章,我也给一个容器手动配置了网络,最后给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 replies    2020-04-29 13:37:58 +08:00
    salamanderMH
        1
    salamanderMH  
    OP
       Apr 29, 2020
    看了下 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 的规则来的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5741 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 06:49 · PVG 14:49 · LAX 23:49 · JFK 02:49
    ♥ Do have faith in what you're doing.