0

Before launching a VM from my script, I need to figure out which IP address it will get.

So I did:

dhclient <interface>

And this works, because dhclient uses the MAC address from the macvtap interface specified, and returns me the IP address from the DHCP server.

This is not a foolproof solution, because there may be some people who have a router at home that does not always return the same IP for the same MAC. But every router I ever owned did, so if it works for 99 percent of the cases it's good enough for me.

But the problem is that dhclient also makes changes to the local configuration because it thinks I want to actually use that address on the host. There is a -n flag that should prevent this, but it is not supported by Debian or most other distributions.

So what is the best way to just ask a DHCP server which IP it is planning to serve to a certain MAC address, without actually modifying any settings on the host?

11
  • You can tell the dhcp server to only assign a certain ip address to a certain mac in its configuration files. EDIT: you do not want to change any settings on the dhcp server? that would change things Commented May 4, 2023 at 21:15
  • @Cheetaiean I use this in a script that anyone can download. If the users need to modify their router settings only to run my script, that will be too much to ask. So no, I dont know anything about the DHCP servers people will have. I just assumed 99 percent of them will always serve the same IP to the same MAC, because every router I ever owned worked that way. Commented May 4, 2023 at 23:18
  • I'm going to suggest you send raw DHCP packets then. Simply broadcast a DHCPDISCOVER, and then wait for the DHCPOFFER from the server, which will tell you exactly what you need to know. Commented May 4, 2023 at 23:54
  • 1
    XY problem? Why do you need the address before the VM gets it? The hypervisor may provide means to see the current address; or to communicate with the VM itself in a way that doesn't depend on networking. Either way learning the actual IP address after the VM gets it may be possible. Commented May 5, 2023 at 0:55
  • 1
    If you want the VM to announce its IP address without any guest module requirement you can use LLDP, which is built into systemd. See EmitLLDP= Commented May 5, 2023 at 13:32

2 Answers 2

1

dhcp does not change the local configuration directly, it calls a script once it gets the lease (by default /sbin/dhclient-script in Debian).

You can specify your own script with -sf and use the $new_ip_address to find out the leased IP. There is a dedicated manpage for this type of script.

dhcp will keep on running once it gets the lease, so you need to stop it. By default the pid is stored in /var/run/dhclient.pid but you can change it with -pf.

An example script:

#!/bin/sh

case $reason in
BOUND|RENEW|REBIND|REBOOT)
    echo "MY IP IS " $new_ip_address
    kill $(cat /var/run/dhclient.pid)
    ;;
*)
    ;;
esac

Then, if you run:

dhclient -sf /path/to/your_script -d  interface 2>&1 | grep "MY IP"

You'll get the value.

Be sure to avoid interaction with other DHCP client processes (dhclient, NetworkManager, ...) since in that case the results could be different.

0

You can't ask a DHCP Server what IP address it's going to issue, and then rely on that going forward. At least, not unless the Server can update its configuration database so that it "remembers" what address it gave you.

Either it's issued you the address, in which case it has to record the fact, or it hasn't issued you the address and it's available for another device to claim.

3
  • Thanks! I don't have a problem with the server issueing the address. But I just want to query that address only, without actually configuring it for the interface afterwards. Im sure there are existing tools which can do that, its just that dhclient doesnt provide that feature on Debian. So Im looking for the name of an alternative dhcp client that does. Commented May 4, 2023 at 23:35
  • @Maestro if you don't have a problem leasing the address then please remove that constraint from the question Commented May 5, 2023 at 5:19
  • No problem I just did.. Commented May 5, 2023 at 13:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.