samohyes
V2EX  ›  程序员

ubuntu 端口转发,总显示 Connection refused

By samohyes at 2023 年 9 月 28 日 · 1946 次点击

各位大佬,请教个端口转发的问题。 我这边有服务运行在本地的 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 条附言  ·  2023 年 9 月 29 日
https://serverfault.com/questions/977498/iptables-port-forwarding-to-localhost

这个转发到本地端口还需要设置下那个 sys 配置,现在可以了
3 条回复  •  2023-10-01 22:23:18 +08:00
pagxir
   1
pagxir  
   2023 年 9 月 28 日
不能用 127 开头,那个是回环地址
sduoduo233
   2
sduoduo233  
   2023 年 9 月 29 日 via Android
是不是 forwarding 没开
看看/etc/sysctl.conf 有没有 net.ipv4.ip_forward=1
secssion
   3
secssion  
   2023 年 10 月 1 日
配置了 dnat , 也要配置回包的 snat
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
© 2026 V2EX · 26ms · 3.9.8.5