Verified and Tested 2/4/15
In this article, we discuss how to change your server’s hostname in CentOS 5.
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.
If you are running Centos 6.7, see that how-to here; and for Centos 7, click here.
Server running CentOS 5. If you do not have a server, consider a Virtual Private Server from Atlantic.Net.
How to change your hostname in CentOS 5
If you’d like to change your CentOS server’s hostname, you’ll need to change it in a few places.
Open up the /etc/sysconfig/network file with your text editor of choice:
sudo vim /etc/sysconfig/network
Change the HOSTNAME argument to the hostname you’d like to use. Here, we’ll be changing the old hostname of ‘bespin’ to ‘endor’. See below for more information on valid hostnames:
This change will take effect the next time you reboot your server. Before you do, though, you might want to also add the hostname to your /etc/hosts file.
Again, with your text editor of choice:
sudo vim /etc/hosts
In this instance, we’ll add our new hostname ‘endor’ to the entry for the loopback address 127.0.0.1 (and for fun, for the IPv6 loopback). We might also add an entry for an IP address configured on this server:
At this point, the next time you reboot your server, your new hostname would show up in your command prompt and would also show up with the ‘hostname’ command.
If you would rather not reboot your server, you can change the hostname with the ‘hostname’ command:
sudo hostname yourhostname
You may change the hostname with this command alone, but the next time you reboot your server, it would not be persistent and would load whatever hostname is still in the /etc/sysconfig/network file.
Also, you may notice that the hostname in your command prompt hasn’t changed. If you’re the sort who’s going to be annoyed by that (or, if you’d like to avoid the confusion of seeing the old hostname in the command prompt), then log out of the current session and when you log back in, the new hostname will show up 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 first character, however, 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 also 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).
Thank you for following along in this How-To and feel free to check back with us for any new updates or to learn more about our VPS hosting services.