How to set hostname and FQDN on CentOS 7 and RHEL 7

The tutorial will explain about how to set hostname and FQDN on CentOS 7 and RHEL 7 (Red Hat Enterprise Linux 7). In our previous post “How to change hostname on CentOS and Red Hat“, we have already define the difference between hostname and FQDN. Hence suggest to read this post.

In Red Hat Enterprise Linux 7 or CentOS 7 , we have found slight changes.

You must have the ip address of system , which you can get by using the command ip addr list.

The given below is our system information, replace these values as per your system information.

IP Address : 192.168.56.101
Hostname : nix (We have selected this hostname, use hostname as per your interest)
FQDN (Fully Qualified Domain Name) : FQDN is combination of hostname+domain-name . Hence , here we are taking domain name as example.com .Therefore FQDN is nix.example.com

To change the hostname and FQDN on RHEL 7 and CentOS follow the given below steps.

Step 1: Edit /etc/hosts file

Now edit the /etc/hosts file in system. And add new line as given below

vi /etc/hosts

192.168.56.101 nix.example.com nix

This is the screenshot from our system.
Hostname CentOS 7

Step 2: Edit /etc/hostname

Edit the /etc/hostname file and give the hostname you have selected.

vi /etc/hostname

nix

The below given is reference from our system.

[root@localhost ~]# cat /etc/hostname
nix
[root@localhost ~]#

Alternatively,you can also use hostnamectl command to set the hostname. It also update the /etc/hostname file which we have edit manually in above steps.

hostname nix is used in below given command.

hostnamectl --static set-hostname nix

Step 3: Print the hostname and FQDN

Now you do not require to restart or logout-login from RHEL 7 and CentOS 7 which is the best thing we have found. Hostname and FQDN are set.
Now print the hostname and FQDN of system.

To print the hostname ,use the command –

hostname

To print the FQDN ,use the command –

hostname -f

Whereas, in case hostname and FQDN is not taking effect. Then run the below command

systemctl restart systemd-hostnamed

Below given is reference from our system

[root@nix ~]# hostname
nix
[root@nix ~]# 
[root@nix ~]# hostname -f
nix.example.com
[root@nix ~]# 
[root@nix ~]#

10 thoughts on “How to set hostname and FQDN on CentOS 7 and RHEL 7”

  1. Not working for me on RHEL 7. Hostname was already set, edited hosts file, ran “systemctl restart systemd-hostnamed”.
    “hostname” and “hostname -f” both return the same value, hostname only without domain suffix.

    • Hi Michael,

      Thanks for the feedback. I am still practising these steps in OS installed in Bare metal servers, VPS, Virtual Machines (VMWare, Xen, VirtualBox) and AWS Cloud EC2 instance.

      Would you mind to share some more details about OS version, hosting provider and how OS is installed.

      Regards
      Sharad

      • Sure. I’m running RHEL 7 under VMWare. It’s the default install, with a few addon apps installed (Oracle JDK, glassfish) and a few ports opened on the firewall.

        I ended up following the advice from http://unix.stackexchange.com/questions/239920/how-to-set-the-fully-qualified-hostname-on-centos-7-0, running:

        “hostnamectl set-hostname nodename.domainname”

        I did not undo the host file changes adding the fqdn so it’s hard to say what that command actually does. The explanation given in that stackexchange post seems to agree with your reasoning, so I’m curious why your method was not sufficient. Most likely it is something to do with how systemd operates, which I’ll admit I don’t know much about.

        • the problem you may have is in your /etc/nsswitch.conf file stating this in the host part: “hosts: myhostname files dns” and you need to have “hosts: files dns”

  2. I am not sure if this is 100% correct. There is a school of thought that says hostname should return the FQDN as well. Not just hostname -f. In that case /etc/hostname should contain the fqdn and not just the first part.

    If you read the linux literature on the hostname command it eludes to that. I use one software application that requires this.

    • Hi Fred,

      FQDN is combination of Hostname+DNS-Name .
      Reference: https://en.wikipedia.org/wiki/Fully_qualified_domain_name

      Run the command man hostname and find this line.

      THE FQDN
      The FQDN (Fully Qualified Domain Name) of the system is the name that
      the resolver(3) returns for the host name, such as, ursula.example.com.
      It is usually the hostname followed by the DNS domain name (the part
      after the first dot). You can check the FQDN using hostname –fqdn or
      the domain name using dnsdomainname.

      Regards
      Sharad

Comments are closed.