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


$ hostname


$ hostname -f
hostname: Name or service not known
$ hostname -d
hostname: Name or service not known

Some debug output:

$ cat /etc/hostname

$ grep ^hosts /etc/nsswitch.conf
hosts:      files dns

4 Answers 4


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 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.,  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:    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.

  • Thanks for your answer. I've updated the question - unfortunately the hostname -f command doesn't work on my CentOS vm.
    – Chris Snow
    Oct 31, 2015 at 18:37
  • @SHC Can you edit your question to also include the contents of /etc/hostname and the output of grep ^hosts: /etc/nsswitch.conf? I don't know much about Network Manager but I also noticed that my /etc/NetworkManager/NetworkManager.conf contains a line in its [keyfile] section: hostname=<nodename>. Oct 31, 2015 at 21:23
  • 1
    @SHC I've since been researching the issue, and have radically revised my answer; it should now address your question more comprehensively. Nov 2, 2015 at 11:01
  • 1
    The lede to this answer is inconsistent with the body. hostnamectl set-hostname nodename.domainname does not set the DNS domain name (and it's not that it sets the NIS domain name either).
    – sourcejedi
    Nov 5, 2016 at 15:19
  • @sourcejedi I don't have time at the moment but I'll try to clarify my answer when I get the chance. Nov 7, 2016 at 16:39

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.


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.


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.

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.