DHCP host entries¶
It is often useful to always assign a specific IPv4 or IPv6 address to a
particular VM. Open the network XML configuration in a text editor. For example,
to edit the default
network:
# virsh net-edit default
For IPv4, use the VM’s MAC address to add a DHCPv4 <host>
element. Add as
many <host>
elements as required.
<network>
<name>default</name>
<bridge name="virbr0"/>
<forward mode="nat"/>
<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="52:54:00:6f:78:f3" ip="192.168.122.222"/>
<host mac="52:54:00:91:ed:23" ip="192.168.122.233"/>
</dhcp>
</ip>
</network>
For IPv6, MAC addresses are deprecated so use the VM’s DHCP Unique Identifier
(DUID) instead. To find the DUID for a running VM, use virsh
.
# virsh net-dhcp-leases routed
MAC address Client ID or DUID
------------------------------------
52:54:00:8a:11:4c 00:01:00:01:1a:ff:2b:ff:52:54:00:8a:11:4c
52:54:00:9b:ae:41 00:01:00:01:1a:ff:2b:ff:52:54:00:9b:ae:41
Use the VM’s DUID to add a DHCPv6 <host>
element. Add as many <host>
elements as required.
<network>
<name>default</name>
<bridge name="virbr0"/>
<forward mode="nat"/>
<ip address="192.168.122.1" netmask="255.255.255.0">
<dhcp>
<range start="192.168.122.2" end="192.168.122.254"/>
</dhcp>
</ip>
<ip family="ipv6" address="2001:db8::1" prefix="64">
<dhcp>
<range start="2001:db8::1000" end="2001:db8::1fff"/>
<host id="00:01:00:01:1a:ff:2b:ff:52:54:00:8a:11:4c" ip="2001:db8::1222"/>
<host id="00:01:00:01:1a:ff:2b:ff:52:54:00:9b:ae:41" ip="2001:db8::1333"/>
</dhcp>
</ip>
</network>
Restart the virtual network.
# virsh net-destroy default
# virsh net-start default
Without restarting the virtual network
It is a bit tiresome to restart the virtual network after every change. A much
better approach is to use virsh net-update
, which makes live changes to an
active virtual network. (--parent-index
specifies which element to select if
there is more than one <ip>
element.)
# virsh net-update default add-last ip-dhcp-host \
'<host mac="52:54:00:6f:78:f3" ip="192.168.122.222"/>' \
--live --config --parent-index 0
# virsh net-update default add-last ip-dhcp-host \
'<host id="00:01:00:01:1a:ff:2b:ff:52:54:00:8a:11:4c" ip="2001:db8::1222"/>' \
--live --config --parent-index 1
To modify a DHCP host entry on an active network:
# virsh net-update default modify ip-dhcp-host \
'<host mac="52:54:00:6f:78:f3" ip="192.168.122.222"/>' \
--live --config --parent-index 0
To delete a DHCP host entry on an active network:
# virsh net-update default delete ip-dhcp-host \
'<host mac="52:54:00:6f:78:f3" ip="192.168.122.222"/>' \
--live --config --parent-index 0