
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. This tutorial helps you to change a hostname on a CentOS 7 system.
Prerequisites
Before starting, make sure you are logged in with non-root user with sudo privileges.
Change Hostname on CentOS
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, TecNStuff’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.tecnstuff.net
.
Check Existing Hostname
Run below command to check the existing hostname for your system:
hostnamectl
Static hostname: local.tecnstuff
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
As you can see on above output, here current hostname name is local.tecnstuff
.
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.tecnstuff.net
Here we changed hostname from local.tecnstuff
to server.tecnstuff.net
using hostnamectl
command. Now the hostname is server.tecnstuff.net
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 "TecNStuff's Server" --pretty
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.
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.tecnstuff.net
Pretty hostname: TecNStuff'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.tecnstuff
to server.tecnstuff.net
and also Pretty hostname changed from CentOS7 Server
to TecNStuff's Server
.
Conclusion
You learned how to change the hostname on CentOS 7 using different methods. If you face any issues you can leave a comment below.
Leave a Reply