ssh port forwarding on qemu and virt-manager

I had a QEMU + virt-manager configuration with some virtual machines i need to access. preferrably from external addresses. So need to be able to port-forward ssh from host to guest with QEMU running the VM instances. No way to configure this in virt-manager.

There is a good set of instructions here:

Which basically says to edit vmname.xml in /etc/libvirt/qemu directory. But you are not allowed to edit it directly you must use some tool called “virsh”. The command should be “virsh edit vmname” where the vmname is your VM name, same as the file name in /etc/libvirt/qemu directory, but without the .xml extension.

First off, no permissions so need to sudo it. Secondly and most baffling is when I edited the file, saved and exited, all my changes were gone all the time. Why is that? Who knows, but some (or a lot of) internet searching later I got this working:

>sudo EDITOR=emacs;export EDITOR

>sudo virsh list

1 myvm

>sudo virsh shutdown 1

>sudo virsh edit vmname

do some edits

>sudo virsh start vmname

And here is the important bit (or so I think, whatever as long as it works..): Do all the edits on the file before saving it. Otherwise it seems that all the changes are reverted by the VM manager as it does not see the namespace used if you save it before adding the parts that use it (even if you in same session add the parts that use it). So DO NOT SAVE BEFORE ALL CHANGES DONE. Not sure if this was the issue but in the end got it working after this.

Finally, with this configuration in place, I can now set up port-forwarding on Putty and connect through several of these from external location to guest on the host. whooppeee..

Just for records, the changes to the vmname.xml done using virsh from the site quoted above:

<domain type='kvm' xmlns:qemu=''>

That is a change on the first line. And add this to the end:

   <qemu:arg value='-redir'/>
   <qemu:arg value='tcp:3333::22'/>

To fordward connections to the host port 3333 to the guest port 22. Finally change

<interface type='network'>


<interface type='user'>

somewhere in the middle of the file. And you are done? I wish..

3 thoughts on “ssh port forwarding on qemu and virt-manager

  1. Note to self.. seems like this can be done much easier with just setting up some iptables forwarding rules on the host. now just need to learn how to do that someday..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s