This post will help you to learn, “How to change Hostname of CentOS 8 Linux” in 4 very easy ways. Also, I will explain what are the challenges or issues you can face after changing the hostname. We will also talk about, where else you need to change hostname in your environment.
The hostname of any server or system is really important, as it’s used to communicate in the network. let’s deep dive and understand the types, pre-requisite and methods to change the hostname of CentOS.
Understanding the CentOS Hostname
There are basically three classes of Hostname known in CentOS –
- The static hostname – is the traditional hostname, which is given by a user to his/her system or machine. Also, it is stored in /etc/hostname file.
- The transient hostname – also called Dynamic hostname, which is managed by the kernel. This value can be changed or set by using DHCP (Dynamic host configuration protocol) or mDNS (multicast Domain name system) at run time. This transient hostname can be useful when you want to change hostname temporarily.
- The pretty hostname – got its name because it supports UTF 8 characters and you can give pretty names to your machine. The pretty hostname is stored in the /etc/machine-info directory. Also, these are more user-friendly hostnames but not recommended and supported by machine code.
Both transient and pretty hostnames are optional. While Static hostname is the mandatory hostname and recognizes a machine on the network.
Hostname Rules
Computer hostname is a unique name, which is assigned to your system. it’ can be up to 64bit characters in length. Also, keep below-mentioned rules in mind, while setting up the hostname for your system.
- Hostnames can consist of letters lowercase (a to z), uppercase (A to Z)
- Hostnames can consist of digits (0 to 9)
- A hyphen (-), comma (,), dot(.) and underscore( _ ) can only be used as a special character in hostnames.
- Hostnames can start or end with a dot (.) and no two dots continuously
- Hostnames letters are not case-sensitive.
- Up to 64 characters are allowed in the hostname
- Try to make your hostname descriptive in case of servers. So, once you log in, you can easily identify it and reduces human errors.
Pre-requisite to set the hostname of CentOS 8
- sudo or root access
- Text editor nano or vim
- CentOS (Version 7 or 8 )
- Terminal
Check which CentOS version you have –
I will be using the root user to run the commands shown in the tutorial. If you have Sudo user rights, You can prepend sudo in front of commands.
As I mentioned in pre-requisite, CentOS is required for this post. So, run cat /etc/os-release command to check your CentOS version.
[root@localhost ~]# cat /etc/os-release
How to check hostname in CentOS 8
Let’s check current hostname before changing it. There are many ways to check existing hostname, i have listed 3 for you.
1.) – Using Hostname command.
Hostname command will display complete FQDN name, which includes hostname and domain name. Hostname and domain names are separated by “.” period.
# hostname
In the output localhost.localdomain is the default Fully qualified domain name(FQDN), you will get after CentOS installation.
2.) – By Viewing “hostname” file in /etc directory.
# cat /etc/hostname
3.) – With “hostnamectl” command
The Command “hostnamectl” is famous to control the hostname of CentOS Linux system.
# hostnamectl
4 Ways – to change or set the hostname of CentOS
Centos – How to change the hostname using command
Step-1
Run “hostnamectl” command and input your new hostname. For example, i have given “mycentos8” as hostname.
# hostnamectl set-hostname "hostname-you-want"
Step-2
Reboot your system by using any one of the command.
# reboot or # init 6 or shutdown -r
In case you can’t reboot your system, just restart “hostnamed” service. then Login and logout once.
# service systemd-hostnamed restart
Check your hostname after reboot. In this example, i have shown you uptime also, just to make you sure that after reboot, my system has new hostname.
How to Change hostname by modifying “hostname” file
In this method, you just need to edit hostname file exist in /etc directory and reboot system. Basically, method 1 and method2 are same. In method 2, we are just editing this file manually instead of using hostnamectl command.
You can use any of your favourite editor to edit hostname file. In this example, i have used “vi” editor.
[root@mycentos8 ~]# vi /etc/hostname ### check changed hostname in file## [root@mycentos8 ~]# more /etc/hostname
I have change it from “mycentos8” to “mycentoslinux” in this example.
After reboot, Linux prompt will also change with new name.
Change or set hostname using “nmtui”
Step-1
Run “nmtui” command
[root@mycentoslinux ~]# nmtui
You will see a text user interface for easy interaction. Select “Set system hostname” and then press enter.
Step-2
Input your hostname and select <OK>. Once you are out of “hostname” dialogue box, select <Quit> to leave nmtui interface. For example, I set it “Centos1” as the hostname.
Now time to reboot your system and validate your hostname changes. Also, you can just restart hostnamed service as show in method-1, if you don’t want to reboot.
How to Change hostname in CentOS using “nmcli”
nmcli is a command-line tool that helps to control the network manager. To change hostname using nmcli, run the below-mentioned sequence of commands.
Step1:- Check current hostname
[root@centos1 ~]# nmcli general hostname
Step2:- Set new hostname
[root@centos1 ~]# nmcli general hostname "New-hostname"
Step3:- Restart hostnamed service or restart your system with reboot command.
[root@centos1 ~]# service systemd-hostnamed restart
For example, I have changed my current hostname centos1 to centos8 and restart the hostnamed service to make changes persistent.
How to Change or set Transient hostname in CentOS (optional)
–> Check current transient or kernel hostname
# sysctl kernel.hostname or # hostnamectl
–> Set new kernel hostname
# sysctl kernel.hostname=new-hostname
Please make a note –
Transient hostnames are dynamic and not persistent. That’s why, it lasts only till your system reboot.
How to Change or set the “pretty” hostname in CentOS (optional)
If you want to setup a more user friendly or pretty name to your system, then follow these steps
Step1-
Check your current pretty hostname, if set already. For example, i have not setup any pretty hostname yet.
# hostnamectl
Step2-
Setup your hostname, which you think really pretty or user friendly for your system.
# hostnamectl --pretty set-hostname “mention Pretty Hostname”
For example, i have set it to “my centos system“
Step3-
Check your pretty hostname
Now when you know all four methods to change the hostname of CentOS and also aware of pretty and transient hostnames. You must understand, the impact of changing the hostname of a system.
What else after changing CentOS hostname locally?
So once you change the hostname of your system, you must also change it in DNS (A records), LDAP/Active directory or hosts files. Wherever you have entries in your environment, you have to reflect the hostname change. For example, Backup, monitoring and configuration management. This will help you to make sure all services are running fine dependent on the hostname of your system. A lot of stuff may break if you ignore making necessary changes.
Which services must be restarted, after hostname change?
There is no defined answer to this because it depends on what all services are running on your system. Many of the services look at servers or the system’s hostname. For example, Apache, Postfix or Sendmail, NFS, any cluster services and the list goes on.
My suggestion would be to check the services running on this particular system or server. Try to grep the hostname parameter in configuration files. Then update new hostname in the configuration file and restart service.
Getting “unable to resolve host error”, after changing hostname?
It may happen because everybody talks about changing hostname using commands and editing /etc/hostname file. But you need to keep in mind, that you need to alter /etc/hosts file also, if you are not part of any large network and not using DNS services. if you are using DNS (domain name system) services, then check for nameserver entry in /etc/resolv.conf file.
Getting “ping: “hostname”: Name or service not known error.
To get rid of this error, follow these steps for standalone system.
Step1-
Ping your system with your hostname, to make sure you get “Name or service not known error“. For example, my system’s hostname is centos8.
# Ping centos8
Run hostname command and provide the system name you have set up using methods mentioned in this post. For example, my system’s hostname is “Centos8“
# hostname “your-hostname“
Check ping again, it must start resolving. Also, validate hostname by just running “hostname” command.
Alternatively you can resolve “Name or service not known error“ with editing /etc/hosts file.
-> Just open /etc/hosts file in your favourite editor and give IP address and hostname of your system. For example, i have given IP “192.168.126.132” and hostname “Centos8” of my system.
# vi /etc/hosts
Issues in changing hostname in Virtual box
Run below mentioned commands –
1.) Edit /etc/cloud/cloud.cfg file and set preserve_hostname parameter to “true”.
2.) Update hostname. For example, Centos8 in my case, Replace it with your hostname and reboot your system.
# hostnamectl set-hostname "Centos8" # reboot
Video tutorial
If you still have any issue in understanding concept, watch out this video on “How to change Hostname of CentOS 8 Linux“
Conclusion
Still, there are more options to change the hostname of CentOS Linux. But I have explained 4 ways on “How to change Hostname of CentOS 8 Linux”, which are more than enough to change any system’s name.
If you are interested in further reading, Visit CentOS website for manuals and admin guides.
Which method you like most and find interesting to change hostname. Please comment and let me know.
Share this post on your social media, if it can help someone in learning.