9

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?

3 Answers 3

4

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.

0

This should be very simple. If you are on NAT, then you simply need a forward rule that would send the packets to 192.168.122.1, that's the host's IP on the libvirt default network. If you are using a bridged setup, make sure the host bridge has an IP, and redirect the packets there. All the configuration needs to be done on the guest.

1
  • 6
    This is too high level for me, I need actual commands. QEMU CLI, guest config / actions/ host config / actions :-) Mar 14, 2018 at 18:10
0
**Try the below command for forwarding port from guest to host or vice-versa**
 # forwarding guest 8055 port to host 8055 port
    user@host_machine:~$ ssh -L 8055:localhost:8055 guest@IP_ADDRESS
    user@host_machine:~$ telnet localhost 8055  # verify by telnet command

    # forwarding host 8055 port to guest 8055 port
    user@host_Machine:~$ ssh -R 8055:localhost:8055 guest@IP_ADDRESS

    user@Guest_Machine:~$ telnet localhost 8055 
1
  • #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.
    – pawan1491
    Jan 10, 2019 at 7:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .