KVM Virtual Machine Guest With Static Dedicated IP Setup

First of all, this walk through pertains to Ubuntu 12.04 and assumes that your hosting company or ISP is able and willing to assign an extra static IP to an arbitrary hwaddress/mac on your host machine.

1.) First of all, let’s install the software:

apt-get install qemu-kvm libvirt-bin bridge-utils vm-install

2.) If you have apparmor running and are planning to put the disk images of your virtual machines in a non-default place you might want to turn off apparmor completely to save yourself some headaches:

/etc/init.d/apparmor stop
/etc/init.d/apparmor teardown
update-rc.d -f apparmor remove

The error you get if apparmor interferes with your stuff is error: internal error Timed out while reading console log output.

4.) It’s time to setup the virtual machine:

virt-install --connect qemu:///system -n ubuntu1004 -r 8192 --vcpus=2 --disk path=/mnt/data/kvm/images/u1004.img,size=240 -c /mnt/data/kvm/ubuntu-10.04.4-server-amd64.iso --vnc --noautoconsole --os-type linux --accelerate --network=bridge:virbr0 --hvm

My VM will use Lucid, have 8GB of RAM, two cores and 240GB HD space.

This line will give you: Domain installation still in progress. You can reconnect to the console to complete the installation process..

5.) Install virt manager on your desktop/work machine: sudo apt-get install virt-manager

6.) Go to Dash Home and start writing Virtual Machine Manager to get the launcher, launch it. Go File -> Add Connection. Check Connect to remote host via SSH with Hypervisor QEMU/KVM. The connection should now show in the list, right click it and Connect to complete the installation.

7.) Possibly pointless but won’t hurt (the eth0:0 virtual interface part), your /etc/network/interfaces:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address host_ip
        netmask netmask_ip
        gateway gateway_ip

auto eth0:0
iface eth0:0 inet static
    address guest_ip
    netmask netmask_ip

Also possibly pointless: Uncomment net.ipv4.ip_forward=1 in /etc/sysctl.conf and do sysctl -p /etc/sysctl.conf

8.) Do virsh dumpxml ubuntu1004 > /etc/ubuntu1004.xml followed by nano /etc/ubuntu1004.xml. Check out the network interface part:

<interface type='direct'>
  <mac address='guest:mac:address'/>
  <source dev='eth0' mode='bridge'/>
  <model type='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x1'/>

Make sure that it looks like above (with a proper MAC address of course, not guest:mac:address). Note the mac address and notify your hosting provider or ISP of the mac address and the guest ip that should be attached to it.

Do virsh define /etc/ubuntu1004.xml and /etc/init.d/libvirt-bin restart.

9.) Login to your virtual machine by way of virt-manager again. Edit /etc/network/interfaces on the guest:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address guest_ip
        netmask netmask_ip
        gateway gateway_ip

Restart networking: /etc/init.d/networking restart.

10.) Fix /etc/resolv.conf on the guest, make it look like on the host.

Voila! You should now be able to SSH in to your guest via the static guest ip and the guest should be able to access the internet.

Related Posts

Tags: , , , , ,