How to Configure Hostname in CENTOS 7/RHEL 7


Host Name


Hostname alternatively referred as Computer name or site name, a host name is the name of the computer you’re currently logged into or visiting. Hostname  is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication such as the World Wide Web. Hostnames may be simple names consisting of a single word or phrase, or they may be structured.

On the Internet, hostnames may have appended the name of a Domain Name System (DNS) domain, separated from the host-specific label by a period (“dot”). In the latter form, a hostname is also called a domain name. If the domain name is completely specified, including a top-level domain of the Internet, then the hostname is said to be a fully qualified domain name (FQDN). Hostnames that include DNS domains are often stored in the Domain Name System together with the IP addresses of the host they represent for the purpose of mapping the hostname to an address, or the reverse process.

Configuring hostname by using hostname command

The hostname command shows or sets the system hostname.

[root@localhost ~]# hostname
 [root@localhost ~]#

we can set the hostname by using the hostname command in the following way

[root@localhost ~]# hostname
[root@localhost ~]# hostname

Configuring hostname by using hostnamectl command


 In recent versions of Linux a new command hostnamectl is introduced, by using this command we can change the hostname in an easyway like hostname command

To view all the current host names, enter the following command:

[root@localhost ~]#hostnamectl status

 To set all the host names on a system, enter the following command as root

[root@localhost ~]hostnamectl set-hostname

 This will alter the pretty, static, and transient host names alike. The static and transient host names will be simplified forms of the pretty host name. Spaces will be replaced with “-” and special characters will be removed.

 To Set a Particular Host Name specify pretty, static or transient.

 [root@localhost ~]# hostnamectl set-hostname "techbuddies personal" --pretty

 Changing Host Names Remotely

To execute a hostnamectl command on a remote system, use the -H, --host option as follows:

  [root@localhost ~]#hostnamectl set-hostname -H [username]@hostname

here the username is the root and is optional and the hostname is the new hostname of remote host.

Configuring hostname by using /etc/hosts file

You can change the hostname by simply edit and add the required information in /etc/hosts file.

The default content of /etc/hosts file is as follows

[root@localhost ~]# cat /etc/hosts   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]#

The leftmost column is the IP address to be resolved. The next column is that host’s name. Any subsequent columns are alias for that host.

add the hostname as like follows    techbuddies        

After you are finished configuring your networking files, don’t forget to restart your network for the changes to take effect.

Related articals 

How to configure networking in linux for beginners – I

How to configure networking in linux for beginners – III

How to configure Firewall service (iptables) in Linux