Verified and Tested 02/17/2015
In this article, we discuss how to change your server’s hostname in Ubuntu 12.04 or 14.04.
Note: The scope of this article does not extend to updating your DNS name, so if you want your server reachable via its DNS name, you will still need to ensure DNS is configured correctly.
Changing the server Hostname in Ubuntu
If you’d like to change your Ubuntu server’s hostname, you’ll need to change it in a few places, the first of which being the /etc/hostname file. Open the /etc/hostname file with your text editor of choice. In this instance, we will be using “vim”.
sudo vim /etc/hostname
Simply change the name here to the hostname you’d like to use. See below for more information on valid hostnames.
This change will take effect the next time you reboot your server. Before you do, however, you may also want to add the hostname to your /etc/hosts file, again with the text editor of your choice.
sudo vim /etc/hosts
In this instance, we will be changing the old hostname of “bespin” to “endor” at the line for the IP address configured on this server (in this case, 10.5.132.2).
It is possible here that you will have a loopback address (such as 127.0.1.1) instead of a routable address. If this is the case, just edit your hostname on that line instead.
At this point, the next time you reboot your server your new hostname will show up in your command prompt. It will also show up by running the command:
If you would prefer not to reboot your server, you can instead change the hostname with the following command. In this example, we are changing the hostname to endor:
sudo hostname endor
You may change the hostname with this command alone, however, the next time you reboot your server it will not persist, and will load whatever hostname is still established in the /etc/hostame file. This change is only temporary.
Also, you may notice that the hostname in your command prompt hasn’t changed. In order to update this, simply log out of the current session and log back in. After a new login, the new hostname will show in the command prompt.
One other caveat: until you do reboot your server after a hostname change, you may also notice that your logs still use the old hostname.
Valid Hostname Restrictions
In each of these configurations, you’ll need to be sure your hostname conforms to the standards for FQDNs (Fully Qualified Domain Names). The ASCII letters a – z, the digits 0 – 9, and the hyphen (‘-‘) are the only characters acceptable. The only limitation to this is that the first character cannot be a hyphen. You may also find it necessary or useful to include the domain name as well, in which case you would then use periods (dots) to separate the hostname and the domain name (and top-level domain). So the following would be examples of acceptable hostnames:
The whole hostname should be no more than 255 characters (see RFC1123).