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 domain.com , 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127.0.0.1 localhost
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you try to ping localhost

# ping localhost

you get
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.056 ms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Adding more host aliases to /etc/hosts

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127.0.0.1 localhost
192.168.50.35 vertito
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ping vertito

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

Adding more host aliases to /etc/hosts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127.0.0.1 localhost localhost.localdomain
192.168.50.35 vertito vertito.yourdomain.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ping vertito.yourdomain.com
# 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:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192.168.50.35 vertito tito.yahoo.com
192.168.50.35 vertito tito.yourdomain.com
192.168.50.35 vertito vertito.yourdomain.com tito.yourdomain.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192.168.50.35 vertito1
192.168.50.35 vertito2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192.168.50.35 vertito1.yourdomain.com
192.168.50.35 vertito2.yourdomain.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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 vertito.yourdomain.com # SETS HOST NAME TO vertito.yourdomain.com
# hostname # DISPLAYS CURRENT HOSTNAME

Wrong!
# hostname vertito.yahoo.com

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 "HOSTNAME=vertito.yourdomain.com" >> /etc/sysconfig/network

# less /etc/sysconfig/network # should show you something like
HOSTNAME=ver.yourdomain.com

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

DHCP_HOSTNAME=ver.yourdomain.com

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 213.255.201.254 and vertito.domain.com is its
reverse DNS name delegated by your network DNS servers.

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

213.255.201.100 vertito.otherdomain.com


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 213.255.201.100
# ping vertito.mydomain.com
would be successful, this is pinging immediately without using /etc/resolv.conf and DNS, but

# nslookup vertito
# nslookup vertito.mydomain.com
# 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!

0 comments:

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