How to forward a port from guest to host in qemu (kvm)
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:
- minimal QEMU + Buildroot setup
- Ubuntu amd64 cloud image: https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171
Tested on an Ubuntu 18.10 host, QEMU 2.12.
Related videos on Youtube
vorburger
blogs on http://www.vorburger.ch ... Mifos OSS Microfinance oldie. http://Eclipse.org Modeling / Xtext OSS (ESON). Various other OSS on https://github.com/vorburger/. Geek Father, LEGO We Do & Mindstorms videos on https://www.youtube.com/user/mvorburger. EPFL alumni. Co-author of "Core Java Data Objects" (JDO) book. Speaks Esperanto, Java, no Klingon, but a few other langs. #SOreadytohelp
Updated on September 18, 2022Comments
-
vorburger almost 2 years
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 Путлер Капут 六四事 over 6 yearsThis is too high level for me, I need actual commands. QEMU CLI, guest config / actions/ host config / actions :-)
-
pawan1491 over 5 years#Run this command on guest machine:
$ ssh -L 6633:localhost:6622 user@Host_Machine
S above command is forwarding your guest machine 6633 port to host machine 6622 port.