Verified and Tested 07/28/2015
This article discusses how to change the hostname using the hostnamectl command on a server running CentOS 7. A hostname is a label given to a device that is connected on a network.
A server with CentOS 7 installed. If you do not have a server, check out the industry-leading cloud hosting options from Atlantic.Net.
Changing your Hostname in CentOS 7
If you are used to how CentOS 6 or earlier handles the hostname, you may notice that the same procedure doesn’t quite work the same way in CentOS 7. If you’re new to CentOS, in general, then you have no baggage to worry about having to leave behind!
In CentOS 7, hostname control is handled with the hostnamectl command. With this command, you can update three different hostnames–the static, transient, and pretty.
To see what your hostname currently is, issue the hostnamectl status command.
An example of the hostnamectl status command
Here, we only have the static hostname listed, which in this case, is identical to the transient and pretty hostnames. The static hostname is the one currently stored in /etc/hostname:
An example of the cat /etc/hostname command
The static hostname is the default hostname the kernel references during boot, and in most instances, will be the one you want to concern yourself with. (We’ll briefly cover transient and pretty hostnames in a bit.)
To change the hostname, issue the following command:
hostnamectl set-hostname "Your-Hostname"
In this case, we’ll be changing the current hostname of ‘bespin’ to ‘Endor’s Forest Moon’. Note what happens with the following comand:
An example of the hostnamectl set-hostname and hostnamectl command
A couple of things to note here. First, when specifying the hostname, I enclosed the full hostname in double-quotation marks. This syntax is only required when using a hostname that contains characters not ordinarily allowed in a standard FQDN, such as a space or an apostrophe. They can be omitted if your hostname will conform to FQDN standard formatting (see below for more information on FQDN formatting).
Next, also note how in the hostnamectl status output, we now have line entries for static and pretty hostnames. Hostnamectl automatically removes illegal hostname characters and also converts the name to all lowercase for the static (and transient) hostname. It also writes this information to the /etc/hostname file as well. You can see it by running the following command:
One thing the hostnamectl command doesn’t do is modify the /etc/hosts file, so we’ll still need to do that. Just open up that file with the text editor you prefer and change the current hostname entry (‘bespin’) to the new static hostname (‘endorsforestmoon’):
At this point, you are all set; your hostname is changed. If you are concerned that it’s not showing up in your command prompt, you will have to log out and log back in.
Now, to help demonstrate some of the differences between types of hostname, let’s set the transient hostname and take a look at the output from hostnamectl status.
It should be noted here that if you want to specify any particular hostname change, you would include which type of hostname (–static, –pretty, or –transient). This parameter can be included at nearly any point in the command, so long as it’s immediately preceded by the double hyphens. Any of the following would work:
sudo hostnamectl --transient set-hostname coruscant
sudo hostnamectl set-hostname --transient kashyyyk
sudo hostnamectl set-hostname tatooine --transient
An example of the sudo hostnamectl set-hostname tatooine –transient command
Since we haven’t made any changes to the static hostname (or the contents of the /etc/hostname file), which remains the same, and should this server reboot, it will pick up this hostname.
The pretty hostname remains “Endor’s Forest Moon”, which can be called in certain instances when presenting the hostname to the user. This configuration is stored in /etc/machine-info.
The transient hostname changes the hostname in the running kernel, but unless that same change is also made to the static hostname, it won’t persist across reboots (or, if the transient hostname is assigned/maintained via something like DHCP, then also when network connectivity is lost).
This example is more illustrative than practical, of course.
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)..
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 CentOS 7 initial setup up guide.