• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

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

September 27, 2014 by Sharad Chhetri

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 ~]#

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. How to change hostname or FQDN in CentOS and Redhat
  2. Set hostname and FQDN in Ubuntu without reboot
  3. How to change hostname in Ubuntu 12.04 and 12.10 without system restart
  4. How to start / stop / restart / reload iptables on CentOS 7 / RHEL 7
  5. Install MariaDB Server 10 on CentOS 7 and RHEL 7 by using yum
  6. How to backup and restore iptables on CentOS 7 / RHEL 7
  7. How to install Nagios on CentOS 7 / RHEL 7
  8. How to find CentOS Linux release version on CentOS 7 series
  9. How to install Octopress in CentOS 6 and RHEL 6
  10. How to install redis server on CentOS 7 / RHEL 7

Filed Under: Linux Tagged With: centos 7, hostname, Red Hat, RHEL 7

Reader Interactions

Comments

  1. sop says

    February 23, 2021 at 11:42 am

    And what about # hostname -d ?

    This won’t work.

  2. Michael McNally says

    March 23, 2017 at 6:31 pm

    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.

    • Sharad Chhetri says

      March 26, 2017 at 3:39 pm

      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

      • Michael McNally says

        March 27, 2017 at 2:47 pm

        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.

        • Gilbert Arias says

          March 5, 2019 at 10:42 pm

          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 says

    March 1, 2017 at 3:00 pm

    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.

    • Sharad Chhetri says

      March 1, 2017 at 4:18 pm

      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

  4. azhar says

    October 29, 2015 at 12:57 pm

    i am hopping it will work properly on rhel7

  5. Kate S. says

    August 19, 2015 at 4:55 pm

    Thanks, very helpful.

    • sharad chhetri says

      August 20, 2015 at 1:55 pm

      You are welcome Kate,

      Best Regards
      Sharad

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

Replace keyword with its filename without extension : bash script

Nagios HTTP WARNING: HTTP/1.1 403 Forbidden

Protect from w00tw00t.at.blackhats.romanian.anti-sec

Virtual Machine inaccessible status on VirtualBox 4.3

Convert new line to space by using sed command

Learn Linux Date Command With Examples

Do not show line haveing particular keyword by grep command

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy