Verified and Tested 8/25/15

Introduction

In this article, we discuss how to change your server’s hostname in CentOS 6.7, is a label given to a device that is connected on a network.

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.

Prerequisites

Server running CentOS 6.7  If you do not have a server, consider spinning up a powerful and affordable server from Atlantic.Net.

Modify your hostname in CentOS 6.7

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:

1
2

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:

3 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

4

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. 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 acceptable examples:

bespin.the-empire.starwars

endor.rebelalliance.starwars

The whole hostname should be no more than 255 characters (see RFC1123).

What Next?

With that, you now have a server with a hostname. Thank you for following along and feel free to check back with us for further updates, or check out our other CentOS guides.