KVM - The Linux Kernel-Based Virtual Machine
News, Blogs and Resources on the Linux (KVM) Kernel-Based Virtual Machine

Exploring Host to Guest Communication using redirects

By default kvm provides mechanisms to communicate with your host from within your guest machine when running your guest machine with userspace networking ie. –net user. You would do this by directing your guest to 10.0.2.2. As an example, see previous post on sharing host files with guest. However, it is not immediately obvious how you would access your guest from your host in this mode of networking. The solution as you might have guessed is using redirects.

 

Using Redirects

Redirects is a built in feature of kvm. It is simply a port forwarding mechanism on your host and requires some extra arguments when starting your virtual machine. Redirects would forward incoming tcp/udp communication on your host to specified ports on your guest. The syntax for using redirects are as follows:

qemu-system-x86_64 –hda fedora.img –m 400 –redir tcp:[host-port]:[guest-host]:[guest-port]

In the next section you’ll see a concrete example of accessing ssh on a guest linux machine from your host.

 

An Example

Let’s say you wanted to ssh to a linux guest machine with ssh running on default port 22, you would start your guest linux machine with the following parameters.

qemu-systems-x86_64 –m 400 –hda fedora.img –redir tcp:2222::22 

I’ve chosen port 2222 on my host for redirection as it is likely that my host already has ssh server listening on port 22. You can choose any random high port number for redirection on your host. Now on your host, execute the the following command assuming you have ssh root access on your guest.

ssh –l root –p 2222 localhost

Your localhost machine will forward the request to your guest on port 22 providing you with access to your guest using ssh.

 

 

 

 

Summary

Port redirection for host to guest communication when running in user networking mode is one of those not so obvious features in kvm and you might come across a situation when you need to use it. It simply involves using the built in port forwarding feature in kvm using the –redir option.