大网数据广告活动
服务器运维问答

Port forwarding from external network to localhost

· 52CE 我爱测

I have a service listen on 127.0.0.1:8080, and I only want to export port 80 to outside. I tried port forwarding but no success. So how can I forward port 80 to localhost:8080? This is what i tried: firewall-cmd --add-port-forwar…

问题原文

原帖链接 · yoru · CC BY-SA 4.0

I have a service listen on 127.0.0.1:8080, and I only want to export port 80 to outside. I tried port forwarding but no success. So how can I forward port 80 to localhost:8080?

This is what i tried:

firewall-cmd --add-port-forward=port=8080:proto=tcp:toport=80:toaddr=127.0.0.1

OS: fedora 36, firewalld


2022-07-02 update:

I tried again with this but failed:

firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=127.0.0.1
# firewall-cmd --list-all
FedoraServer (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp1s0
  sources:
  services: cockpit dhcpv6-client http https ssh
  ports: 80/tcp 80/udp 443/tcp 443/udp
  protocols:
  forward: no
  masquerade: yes
  forward-ports:
    port=443:proto=tcp:toport=8443:toaddr=127.0.0.1
    port=443:proto=udp:toport=8443:toaddr=127.0.0.1
    port=443:proto=tcp:toport=8443:toaddr=
    port=443:proto=udp:toport=8443:toaddr=
    port=80:proto=tcp:toport=8080:toaddr=127.0.0.1
  source-ports:
  icmp-blocks:
  rich rules:
nc -l 127.0.0.1 8080
nc x.x.x.x 80
Ncat: TIMEOUT.

Edit 2022-07-03: fix wrong port number.

已采纳解答

原帖链接 · Dylan · CC BY-SA 4.0

Forwarding to the loopback address is disabled by default. Assuming the incoming inteface name is eth0, it can be temporarily enabled with:

sudo sysctl -w net.ipv4.conf.eth0.route_localnet=1

Additionally, the forwarding rules must contain toaddr=127.0.0.1

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-07-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。