- CentOS 6 Linux Server Cookbook
- Jonathan Hobson
- 1486字
- 2021-07-27 18:01:36
Changing the hostname and resolving a fully qualified domain name
In this recipe we will learn how to change the hostname and resolve the Fully Qualified Domain Name (FQDN) of your server.
The process of setting the hostname is typically associated with the installation process but on many occasions there comes a time when the name used is either incorrect for a technical reason or you just don't like it any more. For some people the name of the server lacks importance, but for many others, a good server demands a good name and so it is the purpose of this recipe is to show you how quickly the hostname can be changed with the intention to revise and resolve the resulting fully qualified domain name.
Getting ready
To complete this recipe you will require a working installation of the CentOS 6 operating system with root privileges and a console-based text editor of your choice.
How to do it...
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.
- To do this, log in as root and using your favorite text editor type:
vi /etc/sysconfig/network
- Now change the hostname value to your preferred name and when finished save and close the file. For example, if you wanted to call your server
henry
, your configuration file will look like this:NETWORKING=yes HOSTNAME=henry
- We will now confirm the settings for the server in order to complete the Fully Qualified Domain Name or FQDN. An FQDN consists of a hostname and the more familiar DNS-based domain name, and in order to do this we will need to open and edit the
hosts
file:vi /etc/hosts
- A typical file may look like this:
127.0.0.1 localhostlocalhost.localdomain localhost4 localhost4.localdomain4 ::1 localhostlocalhost.localdomain localhost6 localhost6.localdomain6
- So let's concentrate on confirming the correct order and setting the correct values with the intention of making our file look similar to the following:
127.0.0.1 localhost.localdomain localhost XXX.XXX.XXX.XXX hostname.domainname.suffix hostname ::1 localhost6.localdomain6 localhost6
- You should now replace the values of the second line with something more appropriate to your needs. For example, if your server was called
henry
, (with an IP address of192.168.1.100
and domain name ofhenry.com
) your final file will look like this:127.0.0.1 localhost.localdomain localhost 192.168.1.100 henry.henry.com henry ::1 localhost6.localdomain6 localhost6
- 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 by typing the following command and waiting for the response:
hostname
- To confirm the domain name, you should type the following command and wait for the response:
hostname –f
- Finally, and as an alternative to the preceding method, to confirm the Fully Qualified Domain Name (FQDN), you can type the following command and wait for the response:
hostname -fqdn
How it works...
A hostname is a unique label created to identify a machine on a network. They are restricted to alpha-numeric based characters, and making a change to your server's hostname can be achieved by simply changing the values found in two system configuration files and rebooting your server.
So what have we learned from this experience?
In the first stage of the recipe, our intention was to change the current hostname used by our server that is maintained by a configuration file located at /etc/sysconfig/network
.
A typical example of this file would look like this:
NETWORKING=yes HOSTNAME=your_computername_here
Based on the example shown, if you did change the name of your server to henry
, then your resulting configuration file would look like this:
NETWORKING=yes HOSTNAME=henry
However, if you wanted to use a slightly different name, then you can use this:
NETWORKING=yes HOSTNAME=webserver
Following this, and having saved our work, we were then required to confirm the Fully Qualified Domain Name or FQDN of our server. This is achieved by updating the hosts file found at /etc/hosts
.
A typical example of the hosts file can look like this:
127.0.0.1 localhostlocalhost.localdomain localhost4 localhost4.localdomain4 ::1 localhostlocalhost.localdomain localhost6 localhost6.localdomain6
The hosts
file is used by CentOS to map hostnames to IP addresses and it is often found to be incorrect on a new, un-configured, or recently installed server. For this reason our first task was to reorganize the references shown in order to support both, the relevant IPv4 and IPv6 values as well as a hostname and domain name reference.
As a consequence of this, we found that the entire file would need rewriting in order to look like this:
127.0.0.1 localhost.localdomain localhost XXX.XXX.XXX.XXX hostname.domainname.suffix hostname ::1 localhost6.localdomain6 localhost6
You were then informed that an FQDN should consist of both, a short hostname and the domain name. Based on the example shown in the main recipe, for a server named henry
whose IP address is 192.168.1.100
and whose domain name is henry.com
, the revised hosts file would now look like this:
127.0.0.1 localhost.localdomainlocalhost 192.168.1.100 henry.henry.com henry ::1 localhost6.localdomain6 localhost6
Whereas the alternative name suggested previously will look like this:
127.0.0.1 localhost.localdomainlocalhost 192.168.1.100 webserver.webserver.com webserver ::1 localhost6.localdomain6 localhost6
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 with the following command:
reboot
On a successful reboot, you were then shown three basic commands that would enable you to confirm your new settings, starting with a command to call your current hostname. You did this by typing:
hostname
To confirm the domain name, you were invited to use the following command:
hostname –f
Finally, the following command is used to confirm the Fully Qualified Domain Name or FQDN like so:
hostname -fqdn
So in conclusion you can say that this recipe has not only served to show you how to rename your server, but it has also served to dispel the many myths associated with your hostnames as opposed to domain names.
As we all know, a server is not only known by the use of a shorter, easier-to-remember, and quicker-to-type single-word based name, but it also consists of three values separated with a period. The relationship between these values may seem strange at first, especially where many people will see them as a single value, but by completing this recipe you have discovered that the domain name remains distinct from the hostname by the virtue that it is determined by the resolver subsystem and it is only by putting them together that your server will yield the Fully Qualified Domain Name or 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 the hosts
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 like so:
vi /etc/hosts
Now scroll down to the bottom of this 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 192.168.1.102 www.example3.lan 192.168.1.103 www.example4.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 repointing 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 of the viewing computer:
192.168.1.201 www.website.com
It isn't failsafe, but in this instance, anyone trying to access www.website.com
will automatically be sent to 192.168.1.201
instead of 192.168.1.200
.
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.