3

I have a situation where I am able to launch CentOS 6.6 images on a subnet such that the VM instances get their IP addresses from the virtual gateway of the subnet. Now this gateway has gone wonky and I don't have the access to fix it, so I have set up my own DHCP server on this subnet.

So now there are 2 DHCP servers on this subnet and my VMs are getting random IP addresses, sometimes from one DHCP server and sometimes from the other. My question is that how I can configure the dhcp client on my VMs so that they make DHCP requests to only my DHCP server rather than the faulty one? man dhcp.conf has not been very helpful.

xkcd
  • 444
  • 3
  • 7
  • 16

2 Answers2

8

On CentOS 7 this can be achieved by creating the file /etc/dhcp/dhclient.conf and adding a line like

reject 192.168.56.0/24;

to reject DHCP offers from one server or the other (or in this example, from all DHCP servers on a specific subnet).

To filter DHCP offers only on a specific interface, place the reject directive inside an interface block:

interface "eth0" {
    reject 192.168.56.0/24;
}

Note the interface name (here, eth0) must be placed in quotation marks, unlike the example shown in the man page for dhclient.conf.

In all cases, run systemctl restart network.service as the superuser for changes to take effect.


On CentOS 6.7 the same instructions work, however

  • Each interface uses a separate configuration file for dhclient. In the above example, the file to which the reject rule would be added would be /etc/dhcp/dhclient-eth0.conf (and there would clearly be no need to use an interface block).
  • The command to reinitialize the system's network interfaces is service network restart, which again must be invoked as the superuser.
  • Can I `reject` an IP address instead of a whole subnet? So for instance, will `reject 10.1.1.1;` work if 10.1.1.1 is the IP address of the faulty DHCP server? – xkcd Dec 21 '15 at 09:54
  • Yes, you can specify an individual IP address like you show, as well as multiple IP addresses and ranges separated by commas. `man dhclient.conf` has more details and examples. –  Dec 21 '15 at 10:27
  • I tried the `reject IP_Address` option in the dhclient.conf file but it still allocated the IP from the rejected DHCP server. Any ideas what is wrong? – xkcd Dec 21 '15 at 15:12
  • Not off the top of my head. Are there any clues in the system log? Note I've tested this only on CentOS 7. –  Dec 21 '15 at 15:33
  • I am also using CentOS 7. Do you know which file has the relevant DHCP logs? – xkcd Dec 21 '15 at 18:54
  • Ah. In that case, try running either `journalctl -e` (or `journalctl -xe`, for more detail) or `systemctl status network` and check for error messages about DHCP. –  Dec 21 '15 at 18:59
  • Sorry I gave you the wrong CentOS version, its 6.7, not 7. I couldn't find any of the tools you mentioned in that. – xkcd Dec 23 '15 at 12:40
  • I've tested this on CentOS 6.7 and updated my answer. Check `/var/log/messages` for information from dhclient. –  Dec 23 '15 at 15:26
3

DHCP works via a multicast request for an IP address. Which gets an IP address from the first DHCP server to respond. There is no way that I know of to force the client to get a DHCP address from a specific server.

In fact, running two DHCP servers that are not aware of each other is asking for the type of trouble you are running into. You need to get the faulty one shut down or fixed.

Zypher
  • 36,995
  • 5
  • 52
  • 95
  • 1
    I cannot get the faulty one to be shutdown or fixed, its controlled by another team which are ... lets say busy. But there is an option in the dhclient command (-s), that lets you specify a specific dhcp server manually. Usually in Linux, if you have a command-line option, there is a reciprocal configuration file option as well. I just cannot find what to put in the `dhcp.conf` file to get the same result as running the `dhclient -s` command manually. – xkcd Jul 29 '15 at 11:34
  • 3
    `DHCP works via a multicast request for an IP address` - DHCP actually uses UDP broadcasts to the Layer 2 broadcast address 255.255.255.255. It doesn't use multicast. – joeqwerty Dec 23 '15 at 15:39
  • -1: Does not answer the OP question and there is a way to ignore specific dhcp servers. It is common for gateway servers to run a dhcpd service on downstream interfaces, and get their upstream configurations from a dhcp service running on an upstream server. In this case you need to have a way to tell the upstream interface to ignore the dhcp server that it itself is running. Furthermore, it is common in development environments to have multiple differently purposed dhcp servers running on a local network and the clients decide which one they listen to depending on the developer needs needs. – Jonathan Ben-Avraham Apr 30 '20 at 13:52