How To Set Static or Permanent IP Addresses for Virtual Machines in KVM

You are here:
< All Topics

Default KVM behaviour is for KVM to issue DHCP temporary IP addresses for its virtual machines. You can suppress this behaviour for newly defined subnets by simply unticking the “Enable DHCP” option for the defined subnet in the Virtual Networks section in the KVM dashboard.
  
However, the NAT bridged network interface is set to automatically issue DHCP IPs. This can be inconvenient when you want to login to the machine from a shell terminal on your PC or laptop rather than accessing the machine via the KVM console terminal.

  
To change these IPs from DHCP to Static, you need to carry out the following steps, using my current environment as an example:
  
Let’s say I want to change the IP of a machine called suse1 from DHCP to Static IP.
    
1. On the KVM host machine, display the list of current KVM networks:
  
virsh net-list
  
root@yoga:/etc# virsh net-list
Name State Autostart Persistent
—————————————————–
default active yes yes
network-10.0.7.0 active yes yes
network-10.0.8.0 active yes yes
  
The interface of the machine I want to set is located on network “default”.
  
2. Find the MAC address or addresses of the virtual machine whose IP address you want to set:
  
Note the machine name is the name used to define the machine in KVM. It need not be the same as the OS hostname of the machine.
  
virsh dumpxml <machine name> | grep -i ‘<mac’
  
root@yoga:/home/kevin# virsh dumpxml suse1 | grep -i ‘<mac’
<mac address=’52:54:00:b4:0c:8d’/>
<mac address=’52:54:00:e9:97:91’/>
  
So the machine has two network interfaces.
  
I know from ifconfig (or ip a) that the interface I want to set is the first one, eth0, with mac address: 52:54:00:b4:0c:8d.
  
This is the one that is using the network called “default”.
  
suse1:~ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:b4:0c:8d brd ff:ff:ff:ff:ff:ff
inet 192.168.122.179/24 brd 192.168.122.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:feb4:c8d/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:e9:97:91 brd ff:ff:ff:ff:ff:ff
inet 10.0.7.11/24 brd 10.0.7.255 scope global eth1
valid_lft forever preferred_lft forever
inet 10.0.7.100/24 brd 10.0.7.255 scope global secondary eth1
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fee9:9791/64 scope link
valid_lft forever pre
  
3. Edit the network configuration file:
  
virsh net-edit <network name>
  
So in this example I do:
  
virsh net-edit default
  

Add the following entry between <dhcp> and </dhcp> as follows:
  
<host mac=’xx:xx:xx:xx:xx:xx’ name=’virtual_machine’ ip=’xxx.xxx.xxx.xxx’/>
  
whereby
  
mac = mac address of the virtual machine
  
name = KVM virtual machine name
  
IP = IP address you want to set for this interface
  
So for this example I add:
  
<host mac=’52:54:00:b4:0c:8d’ name=’suse1′ ip=’192.168.122.11’/>
  
then save and close the file.
  
4. Then restart the KVM DHCP service:
  
virsh net-destroy <network name>
  
virsh net-destroy default
  
virsh net-start <network name>
  
virsh net-start default
  
5. Shutdown the virtual machine:
  
virsh shutdown <machine name>
  
virsh shutdown suse1
  
6. Stop the network service:
  
virsh net-destroy default
  
7. Restart the libvertd system:
  
systemctl restart virtlogd.socket
systemctl restart libvirtd
  
8. Restart the network:
  
virsh net-start <network name>
  
virsh net-start default
  
9. Then restart the KVM desktop virt-manager
  
virt-manager
  
10. Then restart the virtual machine again, either on the KVM desktop or else using the command:
  
virsh start <virtual machine>
  
virsh start suse1
  
If the steps have all been performed correctly, the network interface on the machine should now have the static IP address you defined instead of a DHCP address from KVM.
  
Verify on the guest machine with ifconfig or ip a

 

Table of Contents