Saturday, July 21, 2007

host name and host aliases explained

Most servers and workstations have their own hostname or computer names. If your company has its own , and you are not the one managing your company? DNS servers, you can quickly configure and change your hostname using /etc/hosts.

This /etc/hosts file contains the list of IP addresses and their corresponding system host names.
Even servers have their own hostnames besides from their own web-known fully qualified domain names. Basically, for any DNS queries, your linux system check this file first before referencing further DNS queries with your /etc/resolv.conf DNS IPs. If the queried hostname is already found with a corresponding IP address then any further DNS queries is not anymore needed.

Say a sample /etc/hosts file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ localhost

If you try to ping localhost

# ping localhost

you get
64 bytes from localhost ( icmp_seq=1 ttl=64 time=0.056 ms

Adding more host aliases to /etc/hosts

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ localhost vertito

# ping vertito

would give your
64 bytes from vertito ( icmp_seq=1 ttl=127 time=3.26 ms

Adding more host aliases to /etc/hosts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ localhost localhost.localdomain vertito

# ping
# ping vertito

Issuing the above commands would give your pretty same results, the same goes

# ping localhost
# ping localhost.localdomain

Now, knowing how hostnames are being traversed is really handy at times. You will see more samples below t get more overview of how hostnames are being traversed and looked up.

Below sample are not advisable:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ vertito vertito vertito

You may be able to ping them, but it would not be publicly available outside from web since outside DNS servers would be able to traverse them.

Another host name misuse:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ vertito1 vertito2


You could ping them locally, but doing reverse lookup from your workstation for that IP or hostname would still fail.

Moreover, setting your hostname via "hostname" linux command would be

# hostname # SETS HOST NAME TO

# hostname

Doing hostname via CLI is not permanent.

Another way to permanently set your hostname is to append them to your /etc/sysconfig/network file :

# echo "" >> /etc/sysconfig/network

# less /etc/sysconfig/network # should show you something like

Now, if you eth0 IP address is given dynamically, that is via DHCP, you can set your hostname by having the below line into /etc/sysconfig/network-scripts/ifcfg-eth0

and restart your network service to renew your IP address from network DHCP server

# service network restart

Pretty nice.

Now, let us say your eth0 public IP address is and is its
reverse DNS name delegated by your network DNS servers.

From your own system, adding the below into your /etc/hosts

Noticed the change of IP address and hostname which is not equal to your delegated reverse and forward DNS names from your DNS servers. Now

# ping
# ping
would be successful, this is pinging immediately without using /etc/resolv.conf and DNS, but

# nslookup vertito
# nslookup
# host vertito
would definitely fail using your DNS since your system doesn know its forward/reverse name so it asks your DNS server to do this for you.

Well, you find this long but interesting too!

Have a nice host day!


Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy