In Linux, it is not difficult to change the hostname. In Linux, you can change hostname using hostname command.
If you execute hostname command without any parameter, it will display current hostname.
If you want to change or set hostname of your Linux system, simply run:
# hostname new-hostname
The above command will change the hostname immediately but the real problem is, original hostname will be restored again on next reboot.
Set System Hostname Permanently in Linux
Newer version of different Linux distributions such as latest Ubuntu, Debian, CentOS, Fedora, RedHat, etc. comes with systemd, a system and service manager that provides a hostnamectl command to manage hostnames in Linux.
To set system hostname on SystemD based distributions, we will use hostnamectl command as follow:
# hostnamectl set-hostname new-hostname
For older Linux distributions, which uses SysVinit in short init, can have their hostnames changed by simply editing the hostname file.
# vi /etc/hostname
Now, you need to add another record for the hostname:
# vi /etc/hosts
For example:
127.0.0.1 new-server.webhostingdiscussion.net
Then you need to restart service as follow:
# /etc/init.d/hostname restart
On RHEL/CentOS based systems that use init, the hostname is changed by modifying network file:
# vi /etc/sysconfig/network
NETWORKING=yes HOSTNAME="new-server.webhostingdiscussion.net" GATEWAY="192.168.0.1" GATEWAYDEV="eth0" FORWARD_IPV4="yes"