How to setup your VPS on a dedicated server at Hetzner

How to setup your VPS on a dedicated server at Hetzner

If you need or want to manage your own VPS on a dedicated server and you are in Europe, Hetzner has some very good prices on dedicated servers. To access the VPS from the World you need to purchase a supplemental IP address that it’s going to be connected via a MAC address to your VPS. Here are the steps to setup KVM on the Ubuntu host machine then create the Virtual Machines and customize the network connection to be able to access the internet.

Before you start, you have to purchase a dedicated machine and an supplemental IP that you can manage via Hetzener’s “Robot”. So you have the dedicated IP lets say ddd.ddd.d.d and the secondary ip for the VPS sss.sss.s.s that relates to the MAC address  00:50:56:00:AB:33.

Of course, before anything you should first secure the server.

Setting up the Ubuntu host server

Things are quite easy here. From the Hetzner Robot you should get Ubuntu pre-installed, but let’s make sure its updated:

$ apt-get update

 

$ apt-get upgrade

Let’s install KVM and the tools we are going to use:

$ apt-get install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker

Just to be sure everything works, reboot the host machine.

Installing the first virtual machine on the host server

We are going to install the first machine using the virt-install command. There are a lot of setting you can choose with virt-install, but we are going to make the minimal settings to have an Ubuntu based VPS.

Let’s do a folder where the virtual machines images will reside:

$ mkdir -p /var/kvm/images

Lets run the virt-install command and create an 100GB VPS, with 4GB of RAM and 2cpu. The machine is “named” Ubuntu and the image on the disk is ubuntu.img (but you can call both the way you want).

virt-install \
--name ubuntu \
--ram 4096 \
--disk path=/var/kvm/images/ubuntu.img,size=100 \
--vcpus 2 \
--os-type linux \
--os-variant ubuntu16.04 \
--graphics none \
--console pty,target_type=serial \
--location 'http://jp.archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial' 

If everything goes well the VM console should appear and you should install Ubuntu with the desired settings. One thing you should make sure during the installation: install OpenSSH to be able to access the virtual machine via SSH.

Open another console on the host machine and check if the VM is running using the following command:

virsh list

The ubuntu machine should appear.

Making the network setting so you can access the virtual machine

There are a few things to do. The host machine network should be set to bridge (this means the network for the virtual machine will go directly to the VPS based on the  VPS MAC. You should change the virtual machine MAC to match the MAC indicated in the Hetzner Robot and also you should change the VPS networking to be able to access the internet.

Let’s first edit the virtual machine settings and change it’s MAC. When creating a VM with virt-install a MAC is pre-alocated to the VM. However, Hetzner only allow connections on the MAC indicated in the Robot interface. So let’s change the VM MAC to the one indicated by Hetzner.

Shutting down the VM (destroy actually means pressing the power button on the machine so it will not send the VM in “fire”):

$ virsh destroy ubuntu

 

Then you should edit the VM configuration:

virsh edit ubuntu

This will actually open the configuration XML file for editing. You should look for a chunk of code that starts with
This interface should be changed to (make sure you put the MAC address from the Hetzner’s Robot and note the replacements with bridge and br0)

We are almost done.

Changing the host network connection
Open the network connection file on the host machine:

vim /etc/network/interfaces 

You should do several changes: replace eth0 with br0

auto br0
iface br0 inet static

Add the following lines:

 bridge_ports eth0
 bridge_stp off
 bridge_fd 1
 bridge_hello 2
 bridge_maxage 12

Reboot the host machine or restart the networking machine.

After getting back online, you should start the VM

virsh start ubuntu

Login to the Virtual machine using SSH, for example (you should use the username and password you created when installing ubuntu on the VPS and the second IP you got from Hetzner:

ssh @root@sss.sss.s.s

Change the networking details on the VM machine (you should use the details for the secondary IP from the Hetzner Robot and the google DNS to find domains)

vim /etc/network/interfaces
address sss.sss.s.s
netmask xxx.xxx.xxx.xxx
broadcast xxx.xxx.xxx.xx
gateway xxx.xxx.xxx.x
dns-nameservers 8.8.8.8 8.8.4.4

Reboot the VM. Should be all good now.