Friday, December 21, 2012

Unix command to find IP address from hostname - Linux example

IP address from hostname in UNIX
In this Unix command tutorial, we will see How to find IP address of any host in a UNIX based system e.g. Linux, Solaris or IBM AIX. In general hostname and IP address are two important things about any host in a UNIX based network.  You always need either hostname or IP address to connect to any host. Some time you want to find IP address of localhost, some time IP address of another host on network etc. We have shown some techniques to find IP address from hostname in UNIX and here we will see is particular list of UNIX commands to get IP address of localhost or host on which you are working along with any other host for which we know hostname. 


List of UNIX command to find IP address from hostname

Here is list of UNIX command which can be used to find IP address :

ifconfig
nslookup
hostname

ifconfig command example
Let's see example of ifconfig command to find IP address of localhost on which you are working :
ifconfig command shows lot of information about various NIC cards in system, you can check either for "inet" or "inet addr" for seeing IP address to respective NIC cards :

# /usr/sbin/ifconfig -a
inet 192.52.32.15 netmask ffffff00 broadcast 192.52.32.255

As I said ifconfig command shows lot of details but I have only included relevant inet one, if you have more than one network card attached to that host, you may see more than one inet address corresponding to each of them. Main drawback of ifconfig command in UNIX is that you can only get IP address of localhost from this command, you can not get IP address of any other host.

grep and hostname example
You can combine grep command and hostname to look IP address from /etc/hosts file. here `hostname` will return output of hostname command and great will then search for that word in /etc/hostname. benefit of using `hostname` is that you can just copy and run this command on any host without typing hostname by yourself. This is one of many great usage of grep in UNIX, see 10 ways to use grep command in UNIX to find out why grep is one of the most useful command in UNIX.

# grep `hostname` /etc/hosts
192.52.32.15     nyk4035        nyk4035.unix.com

ping command example
How to find IP address from hostname in UNIX Our plain old ping command can also be used to find IP address of localhost or any other host provided you know the hostname. ping is used to check wither target server is alive or not but while using with option -s it also display IP address of corresponding host as well. Ping is actually one of the of 10 useful networking command in UNIX , see that link for other commands.

# ping -s `hostname`
PING nyk4035: 56 data bytes
64 bytes from nyk4035 (192.52.32.15): icmp_seq=0. time=0.186 ms

nslookup command example
nslookup is one of the primary UNIX command to find IP address from hostname and again from hostname to IP address. Similar to ping you can also use nslookup command to find IP address of both local host and remote host in any UNIX based system. nslookup is generally available in most of UNIX based system e.g. Linux, Solaris, IBM AIX or any other UNIX system.

# nslookup `hostname`
nyk4035.unix.com       canonical name = nyk4035.unix.com
Name:   nyk4035.unix.com
Address: 192.52.32.15

That's all on How to find IP address in UNIX , Linux and other UNIX based system e.g. Solaris, IBM AIX, BSD etc. We have seen examples of getting IP address from hostname by using ipconfig, ping, hostname and nslookup command. If one command is not working in your machine you can always use other command or you can simply cross check IP address by using multiple UNIX commands.

Other UNIX command tutorial you may like

No comments:

Post a Comment

Java67 Headline Animator