Score:2

How to set kvm to use router's dhcp in linux?

ve flag

I'm new to kvm and my question may be obvious to some. How to use the local router dhcp instead the kvm local dhcp. The local dhcp has a range starting from 192.168.122.1 and i need it to use the local router with a range starting from 192.168.100.1. How to configure it? Thanks in advance!

My /etc/libvirt/qemu/networks/default.xml

    <network>
      <name>default</name>
      <uuid>21f9eef3-c1ac-49a1-b413-574011a2c4d5</uuid>
      <forward mode='nat'/>
      <bridge name='virbr0' stp='on' delay='0'/>
      <mac address='52:54:00:38:20:3e'/>
      <ip address='192.168.122.1' netmask='255.255.255.0'>
        <dhcp>
          <range start='192.168.122.45' end='192.168.122.55'/>
          <host mac='52:54:00:05:d8:ba' name='pluto' ip='192.168.122.50'/>
          <host mac='52:54:00:84:bb:56' name='kali' ip='192.168.122.45'/>
        </dhcp>
      </ip>
    </network>

I've seen a suggestion in this post KVM Networking - assign IP to each VM by outside DHCP to edit this file with this snippet, but I'm not sure where to put it in the xml file:

        <interface type='bridge'>
          <mac address='52:54:00:1c:00:3a'/>
          <source bridge='br0'/>
          <model type='e1000e'/>
          <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
        </interface>
Score:1
cn flag

What you'd need to do first is to put your hypervisor's network on a bridged interface that the VMs can connect to.

Let's say this is going to be br0 and the current physical interface is eth0.

Any IP configuration you have on eth0 should be moved to br0.

The way to do this is persistently depends on how you configure your network and that differs even on the same distro (NetworkManager, netplan, systemd-networkd, classic distro scripts - all have different configuration).

If you wanted to do this once on the command line to try it out the below may work, and if you mess it up, a reboot would put your configuration back:

brctl addbr br0
ip a flush eth0
ip a add a.b.c.d/xx dev br0
brctl addif br0 eth0
ip link set br0 up

Then you can either attach a new network to your VM which is on the bridge (this can be done while the VM is running), or change the current interface (this would require a shutdown of the VM).

In general, all this is much easier if you use virt-manager - there is a GUI that makes the whole experience very easy.

Otherwise, you can add a new network to your VM with:

virsh attach-interface --domain your-vm-name \
  --type bridge \
  --source br0 \
  --model virtio \
  --config \
  --live

Here, --live will add the configuration to the running machine, and --config would persist it to its XML file so it is there after your shutdown the VM.

Alternatively, you can virsh edit your-vm-name and edit the network configuration you have now to mimic the one you posted (replace the current <interface>...</interface> so it is type bridge, source br0. You can keep the model and mac, and pci information as it is.

Joao Henrique da Silva avatar
ve flag
Thanks for the attention, I've tried the first approach and I couldn't figure out what info to insert in this line ip a add a.b.c.d/xx dev br0, is the ip of me vm or the ip range of the dhcp server, or else? I've created a second interface in my server but, probably because my mistake, It didn't worked. Later today I'll try the second approach, if the first is not fit for this case.
cn flag
If your server currently has single interface eth0 with IP address 192.168.99.9/24, then this IP address should be assigned to the bridge (br0), and eth0 should be attached to the bridge. If you put the output of `ip a` to your question and indicate the DHCP interface and network, it will be easier to update the answer.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.