How do I set the fully qualified hostname on CentOS 7.0?
I have seen a few posts online for example using:
$ sudo hostnamectl set-hostname nodename.domainname
However, running domainname returns nothing:
$ domainname
(none)
Also:
$ hostname
nodename.domainname
However,
$ hostname -f
hostname: Name or service not known
$ hostname -d
hostname: Name or service not known
Some debug output:
$ cat /etc/hostname
nodename.domainname
$ grep ^hosts /etc/nsswitch.conf
hosts: files dns
Best Answer
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:<machine's primary, non-loopback IP address> <hostname>.<domainname> <hostname>
there<some IP> <hostname>
, and this includes lines with127.0.0.1
and::1
(IPv6) addresses.Note that unless you’re using NIS,
(none)
is the correct output when running thedomainname
command.To check if your DNS domainname is set correctly use
dnsdomainname
command and check output ofhostname
vshostname -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 thegetdomainname(2)
standard library function. (nisdomainname
andypdomainname
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 orhostname
with the-d, --domain
options. (Note that thednsdomainname
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.Name Resolution
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):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/etc/resolv.conf
.When running
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 andhostname
reports thatName or service not known
.When
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
1. 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 ranhostname
with the-d
or-f
option, the local DNS services were able to fully resolve the FQDN for my new hostname.2. Use the
/etc/hosts
fileIf 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.,Excerpt from
hostname
man page