V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
samohyes
V2EX  ›  程序员

ubuntu 端口转发,总显示 Connection refused

  •  
  •   samohyes · Sep 28, 2023 · 1523 views
    This topic created in 942 days ago, the information mentioned may be changed or developed.

    各位大佬,请教个端口转发的问题。 我这边有服务运行在本地的 127.0.0.1: 8545 ,机子内网 ip 是 192.168.0.5 。我需要将所有到 192.168.0.5: 8549 的请求转发到 127.0.0.1:8545. 于是我使用 iptables 添加了如下规则并 save 。

    iptables -t nat -A PREROUTING -d 192.168.0.5 -p tcp --dport 8549 -j DNAT --to-destination 127.0.0.1:8545
    iptables-save
    

    同时我确保 ufw 8549 打开了。

    ufw allow 8549
    ~# ufw status
    Status: active
    
    To                         Action      From
    --                         ------      ----
    8549                       ALLOW       Anywhere                  
    8549 (v6)                  ALLOW       Anywhere (v6)             
    
    

    但是我 curl 依旧显示 connection refused

    curl http://192.168.0.5:8549/ 
    curl: (7) Failed to connect to 192.168.0.5 port 8549: Connection refused
    
    

    求大佬们指点一下!

    Supplement 1  ·  Sep 29, 2023
    https://serverfault.com/questions/977498/iptables-port-forwarding-to-localhost

    这个转发到本地端口还需要设置下那个 sys 配置,现在可以了
    3 replies    2023-10-01 22:23:18 +08:00
    pagxir
        1
    pagxir  
       Sep 28, 2023
    不能用 127 开头,那个是回环地址
    sduoduo233
        2
    sduoduo233  
       Sep 29, 2023 via Android
    是不是 forwarding 没开
    看看/etc/sysctl.conf 有没有 net.ipv4.ip_forward=1
    secssion
        3
    secssion  
       Oct 1, 2023
    配置了 dnat , 也要配置回包的 snat
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5650 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 06:51 · PVG 14:51 · LAX 23:51 · JFK 02:51
    ♥ Do have faith in what you're doing.