The process of setting the hostname is typically associated with the installation process. If you ever need to change it or your server’s Domain Name System (DNS) resolver, this process will show you how.
To Start With: What Do You Need?
For completing this process, you will require a working installation of the CentOS 7 operating system with root privileges, and a console-based text editor of your choice.
The Process
To start this process, we shall start by accessing the system as root and opening the following file in order to name or rename your current server’s hostname:
- Log in as root and type in the following command to see the current hostname:
hostnamectl status
- Now, change the hostname value to your preferred name. For example, if you want to call your server jimi, you would type (change appropriately):
hostnamectl set-hostname jimi
Note
Static hostnames are case-sensitive and restricted to using an Internet-friendly alphanumeric string of text. The overall length should be no longer than 63 characters, but try to keep it much shorter. - Next, we need the IP address of the server. Type in the following command to find it (you need to identify the correct network interface in the output):
ip addr list
- Afterward, we will set the Fully Qualified Domain Name (FQDN), in order to do this, we will need to open and edit the host's file:
vi /etc/hosts
- Here, you should add a new line appropriate to your needs. For example, if your server’s hostname was called jimi, (with an IP address of 192.168.1.100, and a domain name of henry.com) your final line to append will look like this:
192.168.1.100 jimi.henry.com jimi
Note
For a server found on a local network only, it is advisable to use a non-Internet based top-level address. For example, you could use .local or .lan, or even .home, and by using these references you will avoid any confusion with the typical .com, .co.uk, or .net domain names. -
Next, we will open the resolv.conf file, which is responsible for configuring static DNS server addresses that the system will use:
vi /etc/resolv.conf
-
Replace the content of the file with the following:
# use google for dns
nameserver 8.8.8.8
nameserver 8.8.4.4 -
When complete, save and close your file before rebooting your server to allow the changes to take immediate effect. To do this, return to your console and type:
reboot
-
On a successful reboot, you can now check your new hostname and FQDN by typing the following commands and waiting for the response:
hostname --fqdn
-
To test if we can resolve domain names to IP addresses using our static DNS server addresses, use the following command:
ping -c 10 google.com
How it works…
A hostname is a unique label created to identify a machine on a network. It is restricted to alphanumeric-based characters, and making a change to your server’s hostname can be achieved by using the hostnamectl command. A DNS server is used to translate domain names to IP addresses. There are several public DNS servers available; in a later process, we will build our own DNS service.
So, what have we learned from this experience?
In the first stage of the process, we changed the current hostname used by our server with the hostnamectl command. This command can set three different types of hostnames. Using the command with the set-hostname parameter will set the same name for all three hostnames: the high-level pretty hostname, which might include all kinds of special characters (for example, Lennart's Laptop), the static hostname which is used to initialize the kernel hostname at boot (for example lennarts-laptop), and the transient hostname, which is a default received from network configurations.
Following this, we set the FQDN of our server. A FQDN is a hostname along with a domain name after it. A domain name gets important when you are running a private DNS, or allowing external access to your server. Besides using a DNS server setting the FQDN can be achieved by updating the host's file found at /etc/hosts.
This file is used by CentOS to map hostnames to an IP address, and it is often found to be incorrect on a new, un-configured, or recently installed server. For this reason, we first had to find out the IP address of the server using ip addr list.
An FQDN should consist of a short hostname and the domain name. Based on the example shown in this process, we set the FQDN for a server named henry, whose IP address is 192.168.1.100 and domain name is henry.com.
Saving this file would arguably complete this process. However, because the kernel makes a record of the hostname during the boot process, there is no choice but to reboot your server before you can use the changed settings.
Next, we opened the system’s resolv.conf file, which keeps the IP addresses of the system’s DNS servers. If your server does not use or have any DNS records, your system is not able to use domain names for network destinations in any program at all. In our example, we entered the public Google DNS server IP addresses, but you are allowed to use any DNS server you want or have to use (often in a cooperate environment, behind a firewall, you have to use internal DNS server infrastructures). On a successful reboot, we confirmed your new settings by using the hostname command, which can print out the hostname or the FQDN based on the parameters given.
So, in conclusion, you can say that this process has not only served to show you how to rename your server and resolve the network but has also shown you the difference between a hostname and domain name:
As we have learned, a server is not only known by the use of a shorter, easier-to-remember, and quicker-to-type single-word-based hostname, it also consists of three values separated with a period (for example jimi.henry.com). The relationship between these values may have seemed strange at first, especially where many people would have seen them as a single value, but by completing this process you have discovered that the domain name remains distinct from the hostname by virtue of being determined by the resolver subsystem, and it is only by putting them together that your server will yield the FQDN of the system as a whole.
There's more…
The hosts file consists of a list of IP addresses and corresponding hostnames, and if your network contains computers whose IP addresses are not listed in an existing DNS record, then in order to speed up your network it is often recommended that you add them to this file.
This can be achieved on any operating system, but to do this on CentOS, simply open the host's file in your favorite text editor, as shown next:
vi /etc/hosts
Now, scroll down to the bottom of the file and add the following values by substituting the domain names and IP addresses shown here with something more appropriate to your own needs:
192.168.1.100 www.example1.lan
192.168.1.101 www.example2.lan
You can even use an external address such as:83.166.169.228 www.packtpub.com
This method provides you with the chance to create mappings between domain names and IP addresses without the need to use a DNS, and it can be applied to any workstation or server. The list is not restricted by size, and you can even employ this method to block access to certain websites by simply re-pointing all requests to visit a known website to a different IP address. For example, if the real address of www.website.com is 192.168.1.200 and you want to restrict access to it, then simply make the following changes to the host's file on the computer that you want to block from access:127.0.0.1 www.website.com
It isn’t failsafe, but in this instance, anyone trying to access www.website.com on this system will automatically be sent to 127.0.0.1, which is your local network address, so this will just block access.
When you have finished, remember to save and close your file in the usual way before proceeding to enjoy the benefits of faster and safer domain name resolution across any available network.