4

Looking around I have found out the following about /etc/resolv.conf valid formatting:

However, I can't find anywhere whether the resolv.conf keywords are case insensitive or case sensitive. They seem to be lowercase usually, but do they have to be? Is it an error if I find a server where they are in uppercase?

A google search turns up this forum thread, where a code example seems to indicate that the keywords are case insensitive. However, there is no link to any authoritative documentation.

Are /etc/resolv.conf keywords (such as nameserver) case sensitive?

8

They are certainly case sensitive in the glibc resolver libraries. Note the use of strncmp (case sensitive compare) rather than strncasecmp (case insensitive compare) in the MATCH function within glibc res_init.c.

This code is responsible for reading + parsing the /etc/resolv.conf file.

#define MATCH(line, name) \
    (!strncmp(line, name, sizeof(name) - 1) && \
    (line[sizeof(name) - 1] == ' ' || \
     line[sizeof(name) - 1] == '\t'))

    if ((fp = fopen(_PATH_RESCONF, "rce")) != NULL) {
        /* No threads use this stream.  */
        __fsetlocking (fp, FSETLOCKING_BYCALLER);
        /* read the config file */
        while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
        /* skip comments */
        if (*buf == ';' || *buf == '#')
            continue;
        /* read default domain name */
        if (MATCH(buf, "domain")) {
            if (haveenv)    /* skip if have from environ */
                continue;
            cp = buf + sizeof("domain") - 1;

Further, quick example showing how lookup breaks with NAMESERVER rather than nameserver.

# cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /sbin/dhclient-script
search eu-west-1.compute.internal
nameserver 172.31.0.2
# getent hosts www.google.com
2a00:1450:400b:802::2004 www.google.com
# sed -i 's/nameserver/NAMESERVER/' /etc/resolv.conf
# getent hosts www.google.com
#
4

Looking at the source code for glibc, the macro that tries to match strings like domain and nameserver is—

#define MATCH(line, name) \
  (!strncmp(line, name, sizeof(name) - 1) && \
  (line[sizeof(name) - 1] == ' ' || \
   line[sizeof(name) - 1] == '\t'))

and the line that matches the search keyword looks like—

if (MATCH(buf, "search")) {

strncmp() is case-sensitive, so that line will only match a lower-case search (followed by either a space or a tab). The same is true for the other keywords.

The parsing code ignores non-matching lines, so you won't see an error if you accidentally use SEARCH instead of search: the line will just be ignored.

  • 1
    Thanks! I accepted the other answer due to the direct link to the code, and the demonstration, but I particularly appreciate the last line of this answer. – Wildcard Nov 10 '16 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.