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, like 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 a hostname or IP address to connect to any host. Sometimes you want to find the IP address of the localhost, sometimes the 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 local host or host on which you are working along with any other host for which we know hostname.

By the way, if you are a beginner in Linux or haven't worked in a Linux host for a long time then I also recommend you to join these hands-on Linux courses to learn some essential Linux commands like this one to become more productive and confident Linux user. 


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



1. ifconfig command example

Let's see an example of the 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 the address corresponding to each of them. 

The main drawback of the 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. . You can further see Learn Linux in 5 Days and Level Up Your Career course on Udemy to learn more options of ifconfig command and other essential Linux commands. 

Unix command to find IP address from hostname - Linux example



2. grep and hostname example

You can combine grep command and hostname to look at IP address from /etc/hosts file. here `hostname` will return the output of the hostname command and great will then search for that word in /etc/hostname. the benefit of using `hostname` is that you can just copy and run this command on any host without typing hostname by yourself. 

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

This is one of many great usages of grep in UNIX, see 10 ways to use the grep command in UNIX to find out why grep is one of the most useful commands in UNIX.

grep and hostname command example for IP address



3. 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 whether the target server is alive or not, but while using option -s it also displays the IP address of the corresponding host as well. 

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

Ping is actually one of the 10 useful networking commands in UNIX, see that link for other commands. If you want to improve your Linux skills then I highly recommend you to learn those essential networking commands in Linux. 

basic Linux networking commands for beginners



4. 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 the nslookup command to find the IP address of Both localhost and remote host in any UNIX-based system. nslookup is generally available in most UNIX-based systems, like 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

If you want to learn more about the nslookup command other important Linux commands, I suggest you check these free online courses to learn Linux commands in-depth.

how to find IP address from hostname in Linux



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


Other UNIX command tutorial you may like

Thanks a lot for reading this article so far. If you like these Linux IP address hostname command examples then please share them with your friends and colleagues. If you have any questions or comments then please drop a comment.

P. S. - If you are a new Linux user and looking for some free online training courses to start your Linux journey, you should check out my list of Free Bash Scripting Courses for Programmers, IT Professionals, and System Administrators.

11 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
  4. Do you mind if I quote a couple of your articles as long
    as I provide credit and sources back to your webpage? My blog site is in the very same niche as yours and my users would certainly benefit from a
    lot of the information you provide here. Please let me know if
    this alright with you. Cheers!

    ReplyDelete
  5. always i used to read smaller articles that ass well clear
    their motive, and that is also happening with this
    article which I am reading aat this time.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.