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.
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:
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.
This article covers how to change the hostname on CentOS 7 using different methods. By default, your server is started with the server's given name as the hostname. Some software such as cPanel requires a valid fully qualified domain name (FQDN) for the hostname to be used during their licensing verification system.
To Change a server's hostname:
1. Using a text editor, open the server's /etc/sysconfig/network file. The following example shows how to open this file in the GNU nano text editor:
# sudo nano /etc/sysconfig/network
2. Modify the HOSTNAME= value to match your FQDN hostname, as shown in the following example:
HOSTNAME=myserver.domain.com
3. Open the file at /etc/hosts. To update the information for internal networking, change the host that is associated with the main IP address for your server, as shown in the following example:
127.0.0.1 localhost localhost.localdomain
123.45.67.89 hostname.domain.com hostname
4. Run the hostname command. This command enables you to change the hostname on the server that the command line remembers, but it does not actively update all of the programs that are running under the old hostname. The following code provides an example:
# hostnamectl set-hostname hostname.domain.com
# hostname
hostname.domain.com
#
5. Use the following command to restart networking on your server to ensure that changes persist on restart:
# /etc/init.d/network restart