Centos – How to configure dhclient on CentOS to get IP from a specific DHCP server

centosdhclientdhcp

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.

Best Answer

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.