Change Hostname in Linux
Changing hostname is not a usual task that we perform in our day today activity,
however if somehow, we’ve to change the hostname, then we can do it using 2
methods.
nmcli hostnamectl
NOTE: changing hostname can also cause to change the configuration in several
areas like replacing the old hostname with new one in dns server or hosts file etc.
Types Of Hostnames
Linux system now has 3 types of hostnames configured:
(1) Static Hostname: Stored in /etc/hosts file or we can say the hostname set
by kernel. Typically, service use this name as the hostname
(2) Pretty Hostname: A descriptive name or a user-defined hostname, such as
proxy centre in data centre A.
(3) Transient Hostname: A fall-back value that is typically received from the
network configuration or how the network views our system.
nmcli Utility
This utility is use to update the system hostname however other utilities might use a
different parameter such as static or persistent.
Step 1: Display current hostname
[root@oratest ~]# nmcli general hostname
[Link]
Step 2: Set new hostname
[root@oratest ~]# nmcli general hostname [Link]
NOTE: [Link] in above command is the new hostname
Step 3: Network manager automatically restart the system-hostnamed to activate
the new name. however, below manual action is required if we don’t want to restart
the host.
[root@oratest ~]# systemctl restart [Link]
Step 4: Validate hostname
[root@oratest ~]# hostname -f
[Link]
[oracle@[Link] ~]$ ping -c 2 [Link]
PING [Link] ([Link]) 56(84) bytes of data.
64 bytes from [Link] ([Link]): icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from [Link] ([Link]): icmp_seq=2 ttl=64 time=0.046 ms
--- [Link] ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1024ms
rtt min/avg/max/mdev = 0.033/0.039/0.046/0.009 ms
hostnamectl utility
Another available method to update the hostname is hostnamectl utility.
STEP 1: Display hostname
[root@oraview ~]# hostnamectl status --static
[Link]
STEP 2: Change hostname using hostnamectl utility
[root@oraview ~]# hostnamectl set-hostname [Link]
STEP 3: Restart network service
[root@oracleview ~]# systemctl restart [Link]
NOTE: Active shell users must relogin to take the effect.
STEP 4: Validate new hostname
[root@oracleview ~]# hostname -f
[Link]