To set the hostname do use
hostnamectl, but only with the hostname, like this:
hostnamectl set-hostname nodename
To set the (DNS) domainname edit
/etc/hosts file and ensure that:
- There is a line
<machine's primary, non-loopback IP address> <hostname>.<domainname> <hostname> there
- There are NO other lines with
<some IP> <hostname>, and this includes lines with
::1 (IPv6) addresses.
Note that unless you’re using NIS,
(none) is the correct output when running the
To check if your DNS domainname is set correctly use
dnsdomainname command and check output of
hostname -f (FQDN).
NIS vs. DNS domain
This issue confused me when I first came across it. It seems that the
domainname command predates the popularity of the Internet. Instead of the DNS domain name, it shows or sets the system’s NIS (Network Information Service) aka YP (Yellow Pages) domain name (a group of computers which have services provided by a master NIS server). This command simply displays the name returned by the
getdomainname(2) standard library function. (
ypdomainname are alternative names for this command.)
Display the FQDN or DNS domain name
To check the DNS (Internet) domain name, you should run the
dnsdomainname command or
hostname with the
-d, --domain options. (Note that the
dnsdomainname command can’t be used to set the DNS domain name – it’s only used to display it.)
To display the FQDN (Fully Qualified Domain Name) of the system, run
hostname with the
-f, --fqdn, --long options (likewise, this command can’t be used to set the domain name part).
The above commands use the system’s resolver (implemented by the
gethostbyname(3) function from the standard library, as specified by POSIX) to determine the DNS domain name and the FQDN.
In modern operating systems such as RHEL 7, the
hosts entry in
/etc/nsswitch.conf is used for resolving host names. In your CentOS 7 machine, this line is configured as (default for CentOS 7):
hosts: files dns
This means that when when the resolver functions look up hostnames or IP address, they first check for an entry in the
/etc/hosts file and next try the DNS server(s) which are listed in
hostname -f to obtain the FQDN of a host, the resolver functions try to get the FQDN for the system’s hostname. If the host is not listed in the
/etc/hosts file or by the relevant DNS server, the attempt fails and
hostname reports that
Name or service not known.
hostname -d is run to obtain the domain name, the same operations are carried out, and the domain name part is determined by stripping the hostname part and the first dot from the FQDN.
Configure the domain name
Update the relevant DNS name server
In my case, I had already added an entry for my new CentOS 7 machine in the DNS server for my local LAN so when the FQDN wasn’t found in the
/etc/hosts file when I ran
hostname with the
-f option, the local DNS services were able to fully resolve the FQDN for my new hostname.
If the DNS server haven’t been configured, the fully qualified domain name can be specified in the
/etc/hosts file. The most common way to do this is to specify the primary IP address of the system followed by its FQDN and its short hostname. E.g.,
172.22.0.9 nodename.domainname nodename
You cannot change the FQDN with
The recommended method of setting the FQDN is to make the hostname be
an alias for the fully qualified name using
/etc/hosts, DNS, or
NIS. For example, if the hostname was "ursula", one might have a line
/etc/hosts which reads:
127.0.1.1 ursula.example.com ursula
Technically: The FQDN is the name getaddrinfo(3) returns for the host
name returned by gethostname(2). The DNS domain name is the part
after the first dot.
Therefore it depends on the configuration of the resolver (usually in
/etc/host.conf) how you can change it. Usually the hosts file is
parsed before DNS or NIS, so it is most common to change the FQDN in