Back to Home

Relationships of dhcpclient and resolv.conf on Linux

Ubuntu · Debian · Fedora · RHEL · Centos · DHCP · DNS · resolv.conf

Relationships of dhcpclient and resolv.conf on Linux

    Abstract: a description of how the /etc/resolv.conf file is updated under the conditions of a working dhcp client, the specifics of various OSs and implementation options.

    Coverage: Debian, Ubuntu, Centos / Fedora / RHEL; dhclient with resolvconf and without. NetworkManager does not count.

    Lyrics: I just spent a few days (details in English [ 1 ], [ 2 ]) figuring out how to properly save the 'options rotate' in /etc/resolv.conf on different distributions with DHCP working. It turned out that there was no clear documentation on this issue, and information had to be collected from various sources, source texts and experimental data. Then it will be dry and to the point.

    What are you talking about?

    On a computer, the network interface can basically be configured in three ways: manually / by specialized software, statically configured settings, and through a DHCP client. (There are still some exotic, but these three are the main methods). The first method is not interesting to us, with a static configuration everything is simple - as it is written, it will be so. DHCP is interesting because the computer asks for settings over the network "from someone." DCHP protocol has many options (settings) that can change completely unexpected computer settings - time zone, server address with exact time, routing table, server name or domain, etc. Of all this, we are interested in the ability to set DNS settings.

    Traditionally, the DNS resolver settings are stored in the /etc/resolv.conf file, and after updating the dhcp lease, this file is updated. This article explains how exactly this file is.

    DHCP client device


    There are several implementations of the dhcp client, we are interested in ISC DHCP, as the most common.
    The client itself is called / sbin / dhclient, however, by default, to update the settings, it is not he that is called, but / sbin / dhclient-script. dhclient-script calls dhclient and uses its response to change different parts of the system. In the dhclient-script itself there is a function make_resolv_conf, which, in fact, creates the resolv.conf file.

    For the convenience of modifying (and confusing system administrators) the dhclient-script has hooks. Their position is different (in Ubuntu Xenial and Debian Stretch it is /etc/dhcp/dhclient-exit-hooks.d, for some version of Centos it is / etc / dhclient-enter-hooks / etc.). There are two kinds of hooks - entry and exit. Entry are called before the main when dhclient-script, exit at the end. In hooks, you can register your version of the make_resolv_conf () function, and then dhclient-script will call it, not the built-in one. What exactly happens with a lease is determined by the reason variable (examples of values: PREINIT, BOUND, RENEW, REBIND, REBOOT, EXPIRE, FAIL, RELEASE, etc). Thanks to maxzhurkin for pointing out (corrected) inaccuracies in this section.

    resolvconf


    The authors of the resolvconf package are actively using this, which allows you to generate the /etc/resolv.conf file according to a given template (and not fixedly, as in the case of the native dhclient-script implementation). They put the file (in Debian / Ubuntu) /etc/dhcp/dhclient-enter-hooks.d/resolvconf, which calls resolvconf -u (update) to create a new version of resolvconf.

    In order not to interfere with the dhcpclient-script, resolvconf manages the /run/resolvconf/resolv.conf file, and the resolvconf package (not to be confused with the program it provides) during installation replaces / etc / resolvconf with a symlink ../run/resolvconf/resolv .conf.

    A typical problem when using resolvconf is the lack of a symlink. If it is not there, then dhclient-script will simply overwrite /etc/resolv.conf with the settings from the DHCP server, and resolvconf will update its file in the corner, only giving a warning that /etc/resolv.conf is not symlink.

    The resolvconf templates are pretty simple:
    • /etc/resolvconf/resolv.conf.d/head
    • /etc/resolvconf/resolv.conf.d/base
    • /etc/resolvconf/resolv.conf.d/tail

    head and tail are simply appended where necessary, but base allows all sorts of strange things, which are described in man resolvconf in the section "CONSUMERS OF NAMESERVER INFORMATION").

    RHEL / Centos / Fedora Specifics


    Red Hat uses its version of the dhclient-script, which is very extensive and complex, taking into account many settings from ifcfg-ethXXX, in particular, the RES_OPTIONS option that interested me. Debian and Ubuntu mostly use the upstream version, which lacks such delights. In Centos 7, at one time in this script there was a bug that caused the presence of the 'options' line in /etc/resolv.conf upon reboot, all other lines were deleted from it, except the line with options, and new DNS servers in the file was not added.

    Read Next