What does dhclient do?

15.9k views Asked by At

I had a server unable to connect to any network and while wrangling with it I found a temporary resolution on running this command: /etc/init.d/network restart && dhclient. However, I don't quite understand what exactly did the dhclient command do to fix the issue? Any help is appreciated. Thanks!

1

There are 1 answers

0
f9c69e9781fa194211448473495534 On BEST ANSWER

The dhclient requests an IP address from a DHCP server (reference 1, reference 2).

From what you are describing, it seems the following is happening:

  • Initially, none of the network interfaces on your machine have been assigned an IP address. Therefore, you are unable to connect to other servers with IP-based protocols. You can verify that no IP address has been assigned using ip address or ifconfig -a.

  • You then start dhclient, which will use DHCP to request an IP address. It does so by sending a DHCP discovery message to the broadcast address of your network.

  • There is a DHCP server running somewhere in your local network (e.g. on your router/gateway). The DHCP server receives the DHCP discovery message and reserves an IP address for your machine. The DHCP server sends back a DHCP offer message back to your machine (based on the link-layer address of your machine contained in the DHCP discovery message, i.e. the MAC address of your network interface). The DHCP offer message tells your machine which IP is being offered to it.

  • Your machine receives the DHCP offer message and replies with a DHCP request message for the offered IP address. The DHCP server replies with a DHCP acknowledgement message, telling you that the IP can be used by your machine now. Your network interface is configured for the assigned IP address. You can now connect to other servers with IP-based protocols.

The dhclient is similar in functionality to dhcpd, they only have some small differences in behavior, e.g. dhclient will exit immediately if no broadcast interfaces are found, whereas dhcpd will continue running in the background until an interface becomes available (reference).

The alternative to using DHCP would be to assign a static IP to your machine.