3

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?

BluesSolo
  • 133
  • 1
  • 6
  • 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"? – Tommiie Oct 19 '18 at 11:31
  • Well, but the question still remains: How can I set the default gateway, does not matter if isolated or not. And in my case I block everything to the internet so it is still isolated. But the type of network was not the point of the question.. – BluesSolo Oct 19 '18 at 11:37
  • 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. – Tommiie Oct 19 '18 at 11:37
  • Ok, I get your point. I updated the question and removed the part about isolation as I am interested in a solution in general to set the gateway specifically. – BluesSolo Oct 19 '18 at 11:39
  • 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. – Tommiie Oct 19 '18 at 11:41
  • Thank you for your hints. It might really be the best choice to change the type of network. However, I am still wondering if setting an explicit gateway is possible via DHCP in libvirt. – BluesSolo Oct 19 '18 at 12:00
  • 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 '18 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?](https://meta.stackexchange.com/q/66377/189912) – Michael Hampton Oct 19 '18 at 14:42

2 Answers2

4

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.

zymhan
  • 1,351
  • 1
  • 14
  • 30
  • 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 '18 at 14:40
1

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?

Mbo42
  • 176
  • 6
  • 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 '18 at 15:29