Change linux hostname

Quick way to change linux server hostname in ubuntu and centos 7 server without reboot

If you need to change your server hostname, first of all you need to edit /etc/hostname file.
The command you need to run to see your active hostname is “hostname” (unbelievable).

Here the command as root user, if you are not root, please use sudo

root@myoldname:~# hostname
myoldname

root@myoldname:~# vim /etc/hostname
myfantasticnewhostname 

root@myoldname:~# hostname
myoldname

As you can see, the hostname doesn’t change quickly (you shuld wait for a reboot to see changes in hostname)

After you change hostname in /etc/hostname file, if you want to quckly apply the new name you should do:

root@myoldname:~# hostnamectl set-hostname myfantasticnewhostname 
root@myoldname:~# hostname
myfantasticnewhostname 

If you log-off and log-on your server again, you should see:

root@myfantasticnewhostname:~#

your hostname has been changed.

Best practice, but not mandatory, is to edit also /etc/hosts file

root@myfantasticnewhostname:~# vim /etc/hosts
127.0.0.1     localhost myfantasticnewhostname 

This procedure is OK for Ubuntu and Centos 7