The hostname is used to identify the local host in a way that is more meaningful than the default “localhost” and “localhost.localdomain” that is set for all hosts by default.
The name of the host is set during the startup and can also be changed during normal running. The procedure and files involved have changed from the SystemV startup to the systemd startup.
For older releases of Linux, especially prior to Fedora 15, the ancient and venerable SystemV startup scripts were used to, among other things, set the hostname. The rc.sysinit script sets the hostname.
The hostname was stored as a fully qualified name (FQDN) in /etc/sysconfig/hostname. The rc.sysinit script read this file to set the hostname. The hostname command could also be used to permanently change the hostname as shown in the sample command below.
You could also change the hostname in the /etc/sysconfig/hostname file and then reboot the system for the name change to take effect.
On systems that use systemd for startup, the hostname command will set the hostname temporarily but this change is not persistent through a reboot.
To change the hostname on systems using systemd, change the contents of the /etc/hostname file.
Note that the MAN page for the hostname command is not correct when using Linux releases that start with systemd. All other documentation to the contrary, the only place and method to change the hostname of a system is by changing it in the /etc/hostname file. Reboot the system for the name change to take effect.
Note: Do not add comments to the /etc/hostname file, or comment out the localhost.localdomain line in the hostname file. Any comments at all in that file will cause strange and unexpected results.
Regardless of which startup system is used, you can temporarily set the host name using the hostname command. However any programs currently running such as shells, will still show the old hostname. New shells, those started after the hostname command is used to change the name of the host, will show the new hostname.