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 :
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 .Therefore FQDN is

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


The below given is reference from our system.

[root@localhost ~]# cat /etc/hostname
[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 –


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
[root@nix ~]# 
[root@nix ~]# hostname -f
[root@nix ~]# 
[root@nix ~]#





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

  1. sop Avatar

    And what about # hostname -d ?

    This won’t work.

  2. Michael McNally Avatar
    Michael McNally

    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.

    1. Sharad Chhetri Avatar

      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.


      1. Michael McNally Avatar
        Michael McNally

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

        1. Gilbert Arias Avatar
          Gilbert Arias

          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”

  3. fred Avatar

    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.

    1. Sharad Chhetri Avatar

      Hi Fred,

      FQDN is combination of Hostname+DNS-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,
      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.


  4. azhar Avatar

    i am hopping it will work properly on rhel7

  5. Kate S. Avatar
    Kate S.

    Thanks, very helpful.

    1. sharad chhetri Avatar

      You are welcome Kate,

      Best Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: