This post will guide how to change hostname under a linux operating system. You can use the hostname or hostnamectl command to change hostname of your linux system. Those two command also can be used to set or display the current hostnaem of your system. The hostname also be used by lots of linux program to identify this machine, such as: DNS program, web service,and etc.
Linux Change Hostname with HOSTNAME Command
Most of the Linux distro support to use the HOSTNAME command to set or display the hostname of system.
Display the Current Hostname
To use the hostname command to display the current hostname, just execute the hostname command directly under command line option, type:
#hostname
Change Hostname
If you want to change hostname permanently, you need to update two config file. one file is hostname config file and another file is /etc/hosts file.
Note: the different Linux distro version maybe need to change the different hostname config file.
For CentOS/Fedora/Redhat linux, you need to update the /etc/sysconfig/network file, just execute the below command:
#vi /etc/sysconfig/network
Then add the new hostname line into this file, just like this:
HOSTNAME=devops-osetc-new.com
Save and close this file. type the following hostname command to set the hostname:
#hostname devops-osetc-new.com
For Debian/Ubuntu Linux, you need to update the /etc/hostname file, just run the below command to edit config file:
#vi /etc/hostname
Then replace the old hostname with new hostname
Save and close the file, then type the below hostname command to set hostname:
#hostname devops-osetc-new.com
For SuSe/OpenSUSE Linux, you need to update the /etc/HOSTNAME file to change hostname, just type the below command to update it:
#vi /etc/HOSTNAME
Delete the old name line and add new hostname in.
devops-osetc-new.com
then save and close the file. type the hostname command to set new hostname:
#hostname devops-osetc-new.com
Linux Change Hostname with HOSTNAMECTL Command
You can also use the hostnamectl command to display or change the current hostname in linux operating system.
Display your computer hostname
Just simply run the hostnamectl command under command line option:
#hostnamectl
Change your computer hostname
to use the hostnamectl command to set the hostname, just run the following command:
#hostnamectl set-hostname devops-osetc-new.com
You can run the hostnamectl command again to verify the newly hostname.