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

How to forward a port from guest to host in qemu (kvm)

· 52CE 我爱测

Is it possible, and how, to forward a port from guest to host in qemu/kvm? Note do I mean (out of) from guest to host, and not from host into guest (that's widely documented). I think I'm looking for the "opposite" of hostfwd/red…

问题原文

原帖链接 · vorburger · CC BY-SA 3.0

Is it possible, and how, to forward a port from guest to host in qemu/kvm?

Note do I mean (out of) from guest to host, and not from host into guest (that's widely documented).

I think I'm looking for the "opposite" of hostfwd/redir.. Like I use & understand -net user,hostfwd=tcp::8022-:22 which connects port 8022 on the HOST to the GUEST - great. Now I also want the "opposite", and need to get connections made to port 6633 in the GUEST out to the HOST, where some process is running waiting for connections on that.

Is that easily possible with some --option I cannot find, or is that a stupid request betraying my lack of networking understanding - like that's not possible like that?

社区高分解答

原帖链接 · Ciro Santilli OurBigBook.com · CC BY-SA 4.0

Minimal working setup

I can access both the host and external network with this QEMU command:

qemu-system-x86_64 \
  -append 'root=/dev/vda console=ttyS0' \
  -device rtl8139,netdev=net0 \
  -drive file=./out/buildroot/build/default/x86_64/images/rootfs.ext2.qcow2,format=qcow2,if=virtio,snapshot \
  -enable-kvm \
  -kernel ./out/linux/default/x86_64/arch/x86/boot/bzImage \
  -machine pc \
  -netdev user,id=net0 \
  -nographic \
  -serial mon:stdio \
;

Then in the host, setup a file server on port 8000:

python -m SimpleHTTPServer 8000

nc -kl 0.0.0.0 8000 also works, but on one of my host systems it only worked with 0.0.0.0 but not localhost.

Now in the guest:

# Done by init in most non-minimal systems.
ifup -a
ip route

This gives:

default via 10.0.2.2 dev eth0
10.0.2.0/24 dev eth0 scope link  src 10.0.2.15

which tells us that the port we need is 10.0.2.2:

cd /tmp
wget 10.0.2.2:8000
cat index.html

Tested with the following images:

Tested on an Ubuntu 18.10 host, QEMU 2.12.

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

资料来源与适用范围

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

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

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