Learn how to quickly set a Linux hostname without having to reboot the server.
Long ago, in an operating system far, far away, the act of changing a hostname in Linux required a reboot. This wasn’t too big of a deal, especially considering most production machines rarely change hostnames. However, there are times when changing a hostname is advantageous—say in a testing environment, or when a machine has been repurposed for another use.
So there are times when you might find yourself needing to change that hostname. Instead of modifying a file and rebooting the server as we had to do during the early years of Linux, there’s a much more efficient way of changing a hostname.
I’m going to show you how that’s done.
SEE: 10 free alternatives to Microsoft Word and Excel (TechRepublic download)
If you open a terminal window or log in to the machine in question, you should see the hostname listed after the USERNAME@ entry (where USERNAME is the name of the user you’ve logged in with). You could also use the command hostname to view the hostname of the machine. So, how do you change that hostname?
With the hostnamectl command. To do this issue the command sudo hostnamectl set-hostname NAME (where NAME is the name of the hostname to be used). Now, if you log out and log back in, you’ll see the hostname has changed.
That’s it–you’ve changed the hostname without having to reboot the server. Just make sure that any user who has logged in logs out and logs back in, otherwise the change won’t take effect for their session. Also, if you have any host files mapping an IP address to the previous hostname, make sure to edit those. Outside of that, happy hostnaming.