HostOnNet Blog

KVM Guest forwarding port to Host

Looking for Linux Server Admin or WordPress Expert? We can help.

To forward port from KVM Guest to KVM Host (Node), for example, you want Port 22 of KVN Guest accessible as Port 2222 on KVM Hosts public IP address.

First, shutdown VM

virsh shutdown VM-NAME

Now edit VM config.

virsh edit VM-NAME

Add following before before closing

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


<domain type='kvm'>

Replace With

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


<interface type='network'>

Replace With

<interface type='user'>

Save and exit editor. Now, start VM.

virsh start VM-NAME

Sample config for VM with name ubuntu14

[root@cl-148 ~]# virsh shutdown ubuntu14
Domain ubuntu14 is being shutdown

[root@cl-148 ~]# virsh edit ubuntu14
Domain ubuntu14 XML configuration edited.

[root@cl-148 ~]# virsh start ubuntu14
Domain ubuntu14 started

[root@cl-148 ~]# netstat -an | grep 2222
tcp        0      0      *                   LISTEN      
[root@cl-148 ~]# virsh dumpxml ubuntu14
<domain type='kvm' id='7' xmlns:qemu=''>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/ubuntu14.img'/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    <controller type='usb' index='0'>
      <alias name='usb0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    <interface type='user'>
      <mac address='52:54:00:a6:bd:a3'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    <serial type='pty'>
      <source path='/dev/pts/3'/>
      <target port='0'/>
      <alias name='serial0'/>
    <console type='pty' tty='/dev/pts/3'>
      <source path='/dev/pts/3'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen=''>
      <listen type='address' address=''/>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    <qemu:arg value='-redir'/>
    <qemu:arg value='tcp:2222::22'/>

[root@cl-148 ~]# 

Posted in Virtualization

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.