libvirt - Static IP and local dns

libvirt - Static IP and local dns

Setting Static IP for kvm guests

When using libvirt (linux kvm), many timnes it is required to assign static ip. One approach is to setup static ip inside guest but just like I use my router’s dhcp server to assign static IPs, I prefer to use same approach for VMs.

We can create IP to MAC address bindings in network setup but adding similar line(s) inside dhcp section:

      <host mac="nn:nn:nn:nn:nn:nn" name="<hostname>" ip="<IP Address>"/>

Edit kvm network (default)

sudo virsh net-edit default

<network connections="10">
  <name>default</name>
  <uuid>4e136f8b-287e-4253-97ec-2e899b954bf2</uuid>
  <forward mode="nat">
    <nat>
      <port start="1024" end="65535"/>
    </nat>
  </forward>
  <bridge name="virbr0" stp="on" delay="0"/>
  <mac address="52:54:00:a9:a2:50"/>
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.2" end="192.168.122.254"/>
      <host mac="nn:nn:nn:nn:nn:nn" name="<hostname>" ip="<IP Address>"/>
    </dhcp>
  </ip>
</network>

Note: replace nn:nn:nn:nn:nn:nn with MAC address, with real hostname and with static ip you want to assign.

Local dns for kvm guests

When using libvirt (linux kvm), many timnes it is required to setup local dns.

We can add local dns but adding similar section before ip section:

      <host mac="nn:nn:nn:nn:nn:nn" name="<hostname>" ip="<IP Address>"/>

Edit kvm network (default)

sudo virsh net-edit default

<network connections="10">
  <name>default</name>
  <uuid>4e136f8b-287e-4253-97ec-2e899b954bf2</uuid>
  <forward mode="nat">
    <nat>
      <port start="1024" end="65535"/>
    </nat>
  </forward>
  <bridge name="virbr0" stp="on" delay="0"/>
  <mac address="52:54:00:a9:a2:50"/>
  <dns>
    <host ip="192.168.122.1">
      <hostname>myhost.example.com</hostname>
    </host>
  </dns>
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.2" end="192.168.122.254"/>
      <host mac="nn:nn:nn:nn:nn:nn" name="<hostname>" ip="<IP Address>"/>
    </dhcp>
  </ip>
</network>

 Share!