Why is the output of the domainname command "(none)"?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux 6.4

Issue

  • The domainname command will normally return "(none)"; however, if you call the function, then domainname will return the empty string. The su command looks to see if domainname is non-null, and will attempt to use it to match domain entries in netgroups.

Resolution

This is the correct behavior of Red Hat Enterprise Linux. If the setdomainname() call is never made, the domainname will be "(none)". To change this behavior, you should set the value manually using the setdomainname() function, or set the NIS Domain Name.

Root Cause

On most Linux architectures (including x86), there is no getdomainname() system call; instead, glibc implements getdomainname() as a library function that returns a copy of the domainname field returned from a call to uname().

From the uname(2) man page:

int uname(struct utsname *buf);

uname() returns system information in the structure pointed to by buf. 
The utsname struct is defined in <sys/utsname.h>: 

    struct utsname {
        char sysname[];    /* Operating system name (e.g., "Linux") */
        char nodename[];   /* Name within "some implementation-defined

        ...

    #ifdef _GNU_SOURCE
        char domainname[]; /* NIS or YP domain name */
    #endif

"(none)" is the value set in the kernel if there has been no call to setdomainname(), as shown by the following bit of code in include/linux/uts.h:

#ifndef UTS_DOMAINNAME
#efine UTS_DOMAINNAME "(none)" /* set by setdomainname() */
#endif

Diagnostic Steps

This can be demonstrated with the following test program:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>

main() {
        char *domain;
        int retval;

        retval = setdomainname(NULL, 0);
        printf("retval = %d\n", retval);


        domain = (char *) malloc((size_t) MAXHOSTNAMELEN);
        getdomainname(domain, MAXHOSTNAMELEN);
        printf("Domain = %s\n", domain);

        exit(0);
}

After building an executable, the following sequence of commands demonstrates the behavior:

# domainname
(none)

# ./testprogram
retval = 0
Domain =

# domainname

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments