How to set the fully qualified hostname on CentOS 7.0?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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:

  1. There is a line <machine's primary, non-loopback IP address> <hostname>.<domainname> <hostname> there
  2. There are NO other lines with <some IP> <hostname>, and this includes lines with 127.0.0.1 and ::1 (IPv6) addresses.

Note that unless you’re using NIS, (none) is the correct output when running the domainname command.

To check if your DNS domainname is set correctly use dnsdomainname command and check output of hostname vs 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. (nisdomainname and 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.

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):

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 /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 and hostname reports that Name 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 ran hostname 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 file

    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

Excerpt from hostname man page

You cannot change the FQDN with hostname or dnsdomainname.

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
in /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
/etc/hosts.

Method 2

On Centos7 using nmtui to set the hostname and providing nodename.domainname / FQDN format is giving consistent results. hostname [d/f/s] all provide correct expected results. It is not stored in /etc/resolv.conf OR /etc/hosts. But the FQDN is written to /etc/hostname as a single line entry.

Method 3

For me, the /etc/hostname file contained the FQDN instead of the hostname (e.g., hostname instead of hostname.example.com). Once I removed the domain from the hostname in /etc/hostname the ‘hostname -f’ command worked normally.

I think this was introduced as I installed the CentOS 7 VM. I should have entered just the hostname, not the fully qualified domain name.

Method 4

Similar issue as OP (RHEL7), and solved it by removing a trailing space from short hostname listed in the /etc/hostname file. Followed by systemctl restart systemd-hostnamed. Now showing FQDN with hostname -f command.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x