Sunday, 28 July 2013

Centos 6/RHEL changing Hostname and resolving Fully Qualified Domain Name

Although the hostname is typically set at installation time, there are occasions when you may need to change it for technical reasons or otherwise.

Here we look at changing the hostname and resolving the fully qualified domain name.

First open up the network script in a text editor

$ sudo /etc/sysconfig/network

Change the hostname value to your preferred name and then save and exit the file, if you want to rename the server to centosbox it will appear as

NETWORKING=yes
HOSTNAME=centosbox

Avoid capitals and irregular characters when naming the host, use only apha-numeric characters under 63 characters in length.

Now confirm the settings for the server in order to complete the Fully Qualified Domain Name or FQDN. An FQDM consists of a hostname and the DNS-based domain name, so in order to do this we will need to open and edit the hosts file

$ sudo vi /etc/hosts

127.0.0.1
localhostlocalhost.localdomain localhost4
localhost4.localdomain4
::1
localhostlocalhost.localdomain localhost6
localhost6.localdomain6

Now confirm the correct order and set the correct values with the intention of making the 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

Replace the values of the second line with something more appropriate, so if the server is called centosbox with an IP address of 192.168.0.100 and a domain name of centosbox.com then the final file will look like below.

If the server is on a local network, it is advisable to use a non-Internet based address. For example, you could use .local or .lan or .home.
By using these references you will avoid any confusion with the typical .com, .co.uk or .net domain names.

When done, save the file and reboot the server to allow the changes to take effect immediately.

$ reboot

Om rebooting, you can now check your new hostname by typing the following command and waiting for the response

$ hostname

To confirm the hostname type the following command and wait for the response

$ hostname -f

Or, 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

So, by changing the values in the two system configuration files /etc/sysconfig/network and /etc/hosts and rebooting the server we can easily change the hostname.

The hosts file is used by Centos to map hostnames to IP addresses and is often found to be incorrect on a new, un-configured or recently installed server. For this reason we first reorganise the references shown in order to support both the relevent IPv4 and IPv6 values as well as the hostname and domain name reference. So we rewrite the file to reflect the newly assigned values.

So to conclude we can say that we have not only renamed the server, but we have also dispelled the myths associated with hostnames as opposed to hostnames.

A server is not only known by the use of the shorter single word base name, it also consists of the three values separated with a period. The domain name remains distinct from the hostname because it is determined by the resolver system, and it is only by putting them both together that the server will give the Fully Qualified Domain Name or FQDN of the system.