How to find IP Address from hostname in Windows Linux and Unix

IP Address from hostname in Windows and Linux

How many times in a day you have a hostname and you want to know the IP address? Hostname to IP address and IP address to hostname conversion is one of frequent thing which we need to do for many things when dealing with networking command in Unix. For one command we need an IP address for others we need hostname even from bash script sometimes we require a hostname and sometimes we look for IP address. networking commands are not as popular as find command or grep command but they are equally important and if you working in Windows or UNIX environment you must learn them and they should also be included in any list of Unix commands for beginners.

By the way, In this hostname to IP address tutorial, I will show you how to get the IP address from hostname or computer name in Unix or Linux and how to get hostname if you have an IP address. If you are looking for a way to do it via Java program then you can also see my post onhow to find the IP address of the localhost in Java.

Finding IP address from hostname in UNIX and Linux

If you are working in a UNIX network and have lots of machines in LAN (Local Area Network),  many times you want to know the IP address of computers from the hostname.

Here are the top 4 ways of getting an IP address from hostname in Linux or UNIX machine

1) IP address using hostname command in Unix/Linux

~/hostname -i

This is the easiest way of finding the IP address of your computer but the limitation is that sometimes this option may or may not available in your UNIX machine e.g. I hardly find this command on Solaris and IBM AIX machines but they are mostly available on Linux Servers. also, a limitation of thehostname is that you can not find the IP address of any other machine. It's like finding IP address of the localhost.

2) IP address using ping command in UNIX or Linux

stock_trader@system:~/test ping trading_system

Pinging trading_system.com [192.24.112.23] with 32 bytes of data:

Reply from 192.24.112.23: bytes=32 time<1ms TTL=128

Reply from 192.24.112.23: bytes=32 time<1ms TTL=128

Reply from 192.24.112.23: bytes=32 time<1ms TTL=128

ping is another simplest way of finding the IP address of localhost or any other host in network, if you know the hostname or computer-name just ping and it will display IP address associated with it. Normally pint command is used to check if a host is alive and connected with the network.

In the above example, the IP address associated with trading_system is "192.24.112.23". The disadvantage of using ping command is that you can not convert IP address back to the hostname.

3) IP address using nslookup command in UNIX or Linux

stock_trader@system:~/test nslookup trading_system

Name:    trading_system.com

Address:  192.24.112.23

nslookup is my favorite command for getting IP address form hostname, its very powerful and also available in many UNIX operating systems like Linux, Solaris, IBM AIX, Ubuntu or BSD. Another advantage of the nslookup command is that we can get either from hostname to IP address or from IP address to hostname.

It can also be used to find the IP address of your own host or any other machine in the network. In the above example of nslookup, we have displayed IP address associated with trading_system. If you want to find hostname from IP address you can just provide IP address instead of hostname

4) How to find the IP address using the ifconfig command

ifconfig is another networking utility in UNIX or Linux which can be used to find IP address of your UNIX machine. ifconfig shows a lot of information so I just grep on inet to find the IP address in the below example, IP address associate with "trading_system.com" is "192.24.112.23".

trading_system.com $ /sbin/ifconfig -a | grep inet

inet 192.24.112.23 netmask ffffff00 broadcast 192.24.112.255

How to find IP Address from hostname in Windows Linux and Unix

IP Address from hostname in Windows Linux and Unix

Now, let's see some examples to convert hostname to IP address in Windows, Linux, and other UNIX based systems.

1. How to find the IP address of your computer in Windows

Surprisingly some of the above examples of finding IP address from hostname will work is windows ditto. You can use ping and nslookup in the exactly the same way as shown above. Even hostname command is available in windows command prompt but I doubt about options "hostname -i". Another change is in command ifconfig , instead of  ifconfig windows uses ipconfig command to find out the IP address of your computer in windows.

How of use ipconfig command in windows to find IP address

Here is an example of using ipconfig command in windows to find out IP address of our computer:

C:\Documents and Settings\stock_trader>ipconfig

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix  . : trading_system.com

IP Address. . . . . . . . . . . . : 192.24.112.23

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.24.112.254

2. How to find the external IP address of your network or computer

All the above examples will show your internal IP address if you are inside a LAN. If you have connected with the internet and want to know your external IP address assigned by your service provider there are lots of websites that will let you know your IP address e.g. ipchicken.com just logging into this site and it will show you your IP address and location. If you have an IP address and wanted to know about the location you can also get it from the internet.

That’s it from me on these nice little tips of converting an IP address to a hostname and then back from hostname to IP address. Let me know if you have some other ways to find an IP address and hostname of the local machine and remote machine.

Enjoy

Further Learning

Linux Command Line Basics

Linux Command Line Interface (CLI) Fundamentals

Learn Linux in 5 Days and Level Up Your Career

Related post:

How to improve speed and productivity in Unix

Difference between Hard link and soft link in Unix

Top 30 UNIX command interview questions asked in Investment banks

10 example of using VI Editor in Unix

10 xargs command example in Linux and UNIX

How to create archive in UNIX using tar command

How to sort output of a command in UNIX and Linux