×



The hostname will be set at the time of installation of CentOS operating system. If you are creating virtual machine then it will be assigned dynamically at the instance creation time. Basically, a computer hostname represents a unique name that gets assigned to a computer in a network in order to uniquely identify that computer in that specific network.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux System Hostname queries.

In this context, we shall look into how to change a hostname on a CentOS 7 system.


How to Change Hostname on CentOS ?

Before proceeding with this procedure, ensure that you use a user with sudo privileges on your Server.

By assigning a hostname you can differentiates a system on a network. It is important that you shouldn't have same hostname on a same network.

There are three types of hostnames as below:

  • static – This is the most important type of hostname. This is used to identify a particular server between machines on network. It's stored at /etc/hostname file. For example, LinuxAPT's Server.
  • pretty – This is more user-friendly and can use more characters and punctuation. It is used to represent the user. This is stored at /etc/machine-info.
  • transient – A transient hostname is changed in the Linux kernel. It is the same as the static hostname.


It is best practice to use a fully-qualified domain name for both static and transient hostnames like server.linuxapt.com.


How to Check Existing Hostname ?

Run below command to check the existing hostname for your system:

$ hostnamectl
Static hostname: local.linuxapt
 Pretty hostname: CentOS7 Server
      Icon name: computer-vm
        Chassis: vm
     Machine ID: 44dddc11e56e403d8053ecdf0b92d462
        Boot ID: c299757e3c8145e09eb2d55172189a24
 Virtualization: kvm
 Operating System: CentOS Linux 7 (Core)
    CPE OS Name: cpe:/o:centos:centos:7
         Kernel: Linux 3.10.0-957.5.1.el7.x86_64
   Architecture: x86-64

Here, the current hostname name is local.linuxapt.


How to Change the Hostname using hostnamectl ?

In CentOS 7 system, we can use hostnamectl command to change the hostname. Following is the basic syntax to change hostname:

$ sudo hostnamectl set-hostname NEW_HOST_NAME
$ sudo hostnamectl set-hostname server.yourdomain.com
$ sudo hostnamectl set-hostname server.yourdomain.com --static
$ sudo hostnamectl set-hostname "Pretty HostName" --pretty
$ sudo hostnamectl set-hostname server.yourdomain.com --transient

You can run command as below to change hostname:

$ sudo hostnamectl set-hostname server.linuxapt.com

Here we changed hostname from local.linuxapt to server.linuxapt.com using hostnamectl command. 

Now the hostname is server.linuxapt.com for server, you can check it again using hostnamectl command.

Same as you can set pretty name for server using below command:

$ sudo hostnamectl set-hostname "LinuxAPT's Server" --pretty


How to Change the Hostname by Edit /etc/hosts file ?

You also can change hostname by changing in /etc/hosts file. Open /etc/hosts file using your favorite text editor:

$ sudo nano /etc/hosts

In file, change the hostname as per you choice at line that begins with 127.0.0.1

Save and close file. 

For this method you need to restart your CentOS system.


How to Verify Hostname ?

After changed using second method, you can again check hostname using hostnamectl command and now it should output with new hostname as below:

Static hostname: server.linuxapt.com
 Pretty hostname: LinuxAPT's Server
      Icon name: computer-vm
        Chassis: vm
     Machine ID: 44dddc11e56e403d8053ecdf0b92d462
        Boot ID: c299757e3c8145e09eb2d55172189a24
 Virtualization: kvm
 Operating System: CentOS Linux 7 (Core)
    CPE OS Name: cpe:/o:centos:centos:7
         Kernel: Linux 3.10.0-957.5.1.el7.x86_64
   Architecture: x86-64

As you can see above the hostname is successfully changed from local.linuxapt to server.linuxapt.com and also Pretty hostname changed from CentOS7 Server to LinuxAPT's Server.


[Need assistance in fixing Ubuntu Linux System errors? We can help you. ]


Conclusion


More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com