V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
samohyes
V2EX  ›  程序员

ubuntu 端口转发,总显示 Connection refused

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

    各位大佬,请教个端口转发的问题。 我这边有服务运行在本地的 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
    
    

    求大佬们指点一下!

    第 1 条附言  ·  221 天前
    https://serverfault.com/questions/977498/iptables-port-forwarding-to-localhost

    这个转发到本地端口还需要设置下那个 sys 配置,现在可以了
    3 条回复    2023-10-01 22:23:18 +08:00
    pagxir
        1
    pagxir  
       222 天前
    不能用 127 开头,那个是回环地址
    sduoduo233
        2
    sduoduo233  
       221 天前 via Android
    是不是 forwarding 没开
    看看/etc/sysctl.conf 有没有 net.ipv4.ip_forward=1
    secssion
        3
    secssion  
       219 天前
    配置了 dnat , 也要配置回包的 snat
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1424 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 17:31 · PVG 01:31 · LAX 10:31 · JFK 13:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.