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:commandline>
    <qemu:arg value='-redir'/>
    <qemu:arg value='tcp:2222::22'/>
  </qemu:commandline>

Find

<domain type='kvm'>

Replace With

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>

Find

<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 0.0.0.0:2222                0.0.0.0:*                   LISTEN      
[root@cl-148 ~]# virsh dumpxml ubuntu14
<domain type='kvm' id='7' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  <name>ubuntu14</name>
  <uuid>9d2851db-1194-7051-834a-ca16fc372e49</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <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>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0'>
      <alias name='usb0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <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'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/3'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/3'>
      <source path='/dev/pts/3'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>
      <listen type='address' address='127.0.0.1'/>
    </graphics>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
  <qemu:commandline>
    <qemu:arg value='-redir'/>
    <qemu:arg value='tcp:2222::22'/>
  </qemu:commandline>
</domain>

[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.