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 recipe will show you how.
To complete this recipe, you will require a working installation of the CentOS 7 operating system with root privileges, and a console-based text editor of your choice.
To begin this recipe, 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:
hostnamectl status
jimi
, you would type (change appropriately):hostnamectl set-hostname jimi
ip addr list
vi /etc/hosts
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
resolv.conf
file, which is responsible for configuring static DNS server addresses that the system will use:vi /etc/resolv.conf
# use google for dns nameserver 8.8.8.8 nameserver 8.8.4.4
reboot
hostname --fqdn
ping -c 10 google.com
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 recipe, we will build our own DNS service.
So, what have we learned from this experience?
In the first stage of the recipe, 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 the 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 hosts 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 recipe, 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 recipe has not only served to show you how to rename your server and resolve the network, but has also showed 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 host name, 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 recipe 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.
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 hosts 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 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 hosts 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.