Is it possible to set a default gateway for the guest VMs via DHCP in libvirt?

I have the following config

<ip address='192.168.123.1' netmask='255.255.255.0'>
  <dhcp>
    <range start='192.168.123.10' end='192.168.123.254'/>
    <host mac="00:16:3e:45:e2:ed" ip="192.168.123.10"/>
    <host mac="00:16:3e:53:a9:1a" ip="192.168.123.11"/>
    ...
  </dhcp>
</ip>

and now it would be nice to somehow add a **gateway** to the guest VMs like that:

<ip address='192.168.123.1' netmask='255.255.255.0'>
  <dhcp>
    <range start='192.168.123.10' end='192.168.123.254'/>
    <host mac="00:16:3e:45:e2:ed" ip="192.168.123.10" **gateway=192.168.123.1**/>
    <host mac="00:16:3e:53:a9:1a" ip="192.168.123.11" **gateway=192.168.123.1**/>
    ...
  </dhcp>
</ip>

How can I do that? I have only found this source, saying that it is not possible. However, this is from 2016. Has anything changed yet or is there another solution that I could use?

  • If you add a default gateway the network wouldn't be isolated anymore... perhaps you should choose a different network type than "isolated", e.g. "forward"? – Tom Oct 19 at 11:31
  • 2
    It was the point of the question as you specifically mention that the network type is isolated. Have you tried setting it to "type=routed"? I would expect you then automatically get a default gateway from DHCP. – Tom Oct 19 at 11:37
  • 2
    I've never used libvirt so I can't help you there, but when I use Docker, virtualbox, vmware workstation... they all provide DHCP services to give your VM an IP address and if the type of network is not host-only or isolated, you automatically get a default gateway from the DHCP server with the IP address of your host. – Tom Oct 19 at 11:41
  • 1
    @BluesSolo you've stated what you immediate goal is a few times. Why don't you tell us what your overall goal is? Why do you want to specify a gateway for the libvirt NAT guests? I get the impression you don't understand the inner workings of the libvirt NAT, and therefore don't understand what is and is not possible. – zymhan Oct 19 at 13:00
  • 1
    Don't ignore questions about why you are doing something. Answer these in as much detail as possible. The reason is that, by knowing what you actually intend to accomplish, someone may be able to find an alternate solution for you. See also What is the XY problem? – Michael Hampton Oct 19 at 14:42
up vote 2 down vote accepted

By default, the libvirt DHCP "server" is the gateway for the libvirt guests. If you want to specify a different setup, then you cannot use libvirt's built-in NAT. You need to create a custom virtual network, probably using brctl, and run a DHCP service on that interface that hands out whatever custom DHCP lease you want.

  • 1
    This is correct. Libvirt does not allow changing the gateway from the default. If you want to use something else as a gateway, you have to disable that DHCP server and run your own. – Michael Hampton Oct 19 at 14:40

I don't have the points to comment, but when you ask "is there another solution that I could use?" I'd ask why can't you spin up a basic DHCP server on the Vhost and let it hand out your default route?

  • This is pretty much the route to take here. Running your own DHCP service instead. The libvirt one is great for getting started but not great if you have a complex network – zymhan Oct 19 at 15:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.