L4Linux
V2EX  ›  问与答

这样的 Port Forwarding 规则该怎么用 iptables 创建?

  •  
  •   L4Linux · Dec 25, 2022 · 1123 views
    This topic created in 1263 days ago, the information mentioned may be changed or developed.

    我目前用 TUN/TAP 给虚拟机分配了 network interface ,通过设置一下三条规则,虚拟机能上网:

    iptables -A FORWARD -i tap0 -o wlan0 -j ACCEPT
    iptables -A FORWARD -i wlan0 -o tap0 -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
    

    现在,我想让虚拟机能访问主机上的某些服务,这些服务只监听了 127.0.0.1 。假设 tap0 的网段是 10.0.0/24 ,iptables 命令该怎么写呢?

    4 replies    2022-12-26 09:20:53 +08:00
    wonderblank
        1
    wonderblank  
       Dec 25, 2022   ❤️ 1
    1. 直接监听 0.0.0.0 ,可以直接访问
    2. iptables -t nat -I PREROUTING -i tap0 -p tcp --dport 80 -j REDIRECT --to-port 80 # DNAT
    L4Linux
        2
    L4Linux  
    OP
       Dec 25, 2022
    @wonderblank 好像不行,这样设置虚拟机里访问不了 10.0.0.0:80 (网关),而且 iptables -L 也显示不出这条规则。
    L4Linux
        3
    L4Linux  
    OP
       Dec 25, 2022
    @wonderblank Samba 成功了 V2Ray 没成功。这条规则是没问题的。
    wonderblank
        4
    wonderblank  
       Dec 26, 2022
    可以加上-d tap0_ip_on_host ,这样就不会影响其他网站访问。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3287 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:12 · PVG 19:12 · LAX 04:12 · JFK 07:12
    ♥ Do have faith in what you're doing.