How To Set System hostname on CentOS 8 / RHEL 8

How can I set or change hostname in CentOS 8 / RHEL 8. During your CentOS or Fedora installation, you’re asked to set a hostname for the server. This is okay but changing it after installation is sometimes inevitable. Hostname uniquely identifies a computer in a network.

A server’s hostname can contain letters(a-z, A-Z), digits(0-9), hyphen(–), dot(.) but must end with a letter or number. It is recommended to use a descriptive name when assigning hostnames for easy identification of a server/service.
Before you set a hostname, first check existing hostname.
$ hostname -s
cent-01
$ hostname -f
cent-01
$ hostnamectl
Static hostname: cent-01
Icon name: computer-vm
Chassis: vm
Machine ID: 596e865e6c594db98c4fd47fdb858138
Boot ID: 47d2f07eb2ea44b799ae6b0c0ba8600e
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
Where:
- -s, –short – Used to print short host name
- -f, –fqdn, –long – Used to print a long host name (FQDN)
Change hostname with hostnamectl
To set a persistent hostname using hostnamectl command, use the command.
sudo hostnamectl set-hostname <name> --static
See example below.
sudo hostnamectl set-hostname backend-sms-app.mydomain.com --static
Confirm your new hostname.
$ hostnamectl
Static hostname: backend-sms-app.mydomain.com
Transient hostname: cent-01
Icon name: computer-vm
Chassis: vm
Machine ID: 596e865e6c594db98c4fd47fdb858138
Boot ID: 47d2f07eb2ea44b799ae6b0c0ba8600e
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
This setting will automatically update the /etc/hostname file.
$ cat /etc/hostname
backend-sms-app.mydomain.com
There are three classes of hostnames that can be set with hostnamectl command.
- Transient hostname – This is a dynamic hostname managed by kernel and can be changed by DHCP or mDNS server at run time. Use the –transient flag to set a transient hostname.
- Pretty hostname – As the name suggests, this is a user-friendly UTF8 hostname used to represent a user of a machine. Use –pretty flag to set it.
- Static hostname – This is stored in /etc/hostname for use at runtime.
Set Pretty hostname.
sudo hostnamectl set-hostname "Computingforgeeks PC" --pretty
Set Transient hostname – Usually same as static hostname.
sudo hostnamectl set-hostname backend-sms-app.mydomain.com --transient
Confirm your settings.
$ hostnamectl
Static hostname: backend-sms-app.mydomain.com
Pretty hostname: Computingforgeeks PC
Icon name: computer-vm
Chassis: vm
Machine ID: 596e865e6c594db98c4fd47fdb858138
Boot ID: 47d2f07eb2ea44b799ae6b0c0ba8600e
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
To map your new hostname to IP address, edit the /etc/hosts file and replace old hostname with a new one.
$ sudo vim /etc/hosts
192.168.121.17 backend-sms-app.mydomain.com backend-sms-app
There you have it.
$ ping -c 2 backend-sms-app PING backend-sms-app.mydomain.com (192.168.121.17) 56(84) bytes of data. 64 bytes from backend-sms-app.mydomain.com (192.168.121.17): icmp_seq=1 ttl=64 time=0.019 ms 64 bytes from backend-sms-app.mydomain.com (192.168.121.17): icmp_seq=2 ttl=64 time=0.040 ms --- backend-sms-app.mydomain.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.019/0.029/0.040/0.011 ms
Related content:
Arch Linux vs Manjaro – Why I Use Arch / Manjaro
How To Search Google from Linux Terminal



