Unix command to find IP address from hostname - Linux example

The IP address from hostname in UNIX
In this Unix command tutorial, we will see how to find the 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. Sometimes you want to find the IP address of the localhost, some time IP address of another host on the network, etc. We have shown some techniques to find the IP address from hostname in UNIX and here we will see is a particular list of UNIX commands to get the 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 the IP address from hostname

Here is a list of UNIX commands which can be used to find the IP address :

ifconfig
nslookup
hostname


ifconfig command example

Let's see an example of ifconfig command to find The IP address of localhost on which you are working :
ifconfig command shows a lot of information about various NIC cards in the 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 a 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. The main drawback of ifconfig command in UNIX is that you can only get the IP address of localhost from this command, you can not get the IP address of any other host.

Unix command to find IP address from hostname - Linux example



grep and hostname example

You can combine grep command and hostname to look IP address from /etc/hosts file. here `hostname` will return the 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 usages 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 UNIXOur plain old ping command can also be used to find the IP address of the localhost or any other host provided you know the hostname. ping is used to check to wither the target server is alive or not, but while using with option -s it also display the IP address of the 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 commands to find the IP address from the hostname and again from hostname to IP address. Similar to ping you can also, use nslookup command to find the IP address of Both localhost and remote host in any UNIX based system. nslookup is generally available in most of the UNIX based systems, 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 the IP address in UNIX, Linux, and other UNIX based systems, e.g. Solaris, IBM AIX, BSD, etc. We have seen examples of getting the IP addresses from the 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 the IP address by using multiple UNIX commands.

Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career

Other UNIX command tutorial you may like

8 comments:

  1. You can also use 'host' command to find IP address associated with a hostname. Since a hostname can have multiple IP address, its better to use a DNS lookup utillity like host or nslookup. This way you will get all the IP address which that DNS name (hostname) is pointing. Commands like ping will only show one of the IP address mapped to the DNS name.

    Here is how you can use 'host' command :
    host google.com
    google.com has address 176.50.192.43
    google.com has address 176.50.192.44
    google.com has address 176.50.192.45

    nslookup also prints the name server which is used to resolve DNS names into IP address.

    ReplyDelete
    Replies
    1. Thanks Anonymous, this is seriously good information. We recently have a problem where we migrated machine to new infrastructure. we kept the DNS alias and hostname same but IP address was changed. Most of things went fine but one of the host in infrastructure was still resolving to old IP address. After much debugging it turns out the stale entry in nscd, a daemon which provide cache for most common name service requests. Once the nscd was restarted in problematic host the issue was resolved and hostname was resolved to new IP address.

      Delete
  2. I can't get the ip address by giving ifconfig in oracle

    ReplyDelete
  3. how to check the ip address of the server in which we are currently working

    ReplyDelete