Linux find ip by hostname

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

IP Address from hostname in Windows and Linux
How many times in a day do 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 the frequent things which we need to do for many things when dealing with networking commands in Unix. For one command we need an IP address for others we need a hostname even from bash script sometimes we require a hostname and sometimes we look for an 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 host name 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 the Java program then you can also see my post on how to find the IP address of the local host in Java.

Finding IP address from hostname in UNIX and Linux Example

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

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 the hostname 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

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.

Читайте также:  Linux ide for arduino

3) IP address using nslookup command in UNIX or Linux

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» .

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 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.

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.

11 comments :

Incoming Search term
lan ping determine ip of hostname
get hostname ip addresses java linux
get ip from hostname linux
search hostname of a machine
how to find hostname unix hostname
how to get computername from ip address
how to find ip for hostname in lan
how to find hostname on linux
display hostname ip address
find ip address from hostname in windows
finding remote computer name linux
get ip address from hostname windows
get ip from hostname windows
get the exact ip address via ifconfig for a specific device in windows
host name to ip windows
how to find ip address and host name
how to find ip address in unix
how to find ip address of an hostname
linux find address for host name
unix command to my ip address
unix find ip from hostname
bash script for ip address from hostname

Читайте также:  Linux редактирование файлов nano

Thanks . I was looking for exact same information, command to find hostname from ip address. we have linux machines in our lan network and I just joined. this unix tips will surely help me lot.

hostname -i will not work to find IP address of another computer, it will only work for current host. I think only available option to get IP address of another computer is «nslookup»

how to check the loction of my friend while chatting.

please help me one of my friend told me he is in us but i think he is telling lie so please help me to guess from where he is calling .

send the answer at papadonpart2@gmail.com

how to find IP address in Unix operating system which doesn’t have nslookup command ? is there way to determine IP address without using nslookup

Thanks for this. i was looking for this «nslookup» command

Hello Javin, How will you convert an IP address to Hostname in Java? Can you please write about that? Thank you sir.

I am using linux system for my office work first time even though I have hosted all my website at linux hosting servers. There are few things I should have learned before like you let us know about ip host, pinging commands at linux system. I am a fast leaner yet I think it will take a month to get used to it as always typed wrong command and right click is something which we always used at windows.

You can also use ‘host’ command to display IP address associated with any DNS name (or hostname). It’s a DNS lookup utility similar to nslookup but it doesn’t print name of DNS server used to resolve addresses. In fact, host command output is much cleaner than nslookup, you will see something like this

host yahoo.com
yahoo.com has address 130,.40.23.19
yahoo.com has address 130,.40.23.29

It will print all IP address associated with a DNS name and registered in DNS server.

@Anonymous, indeed I would prefer host and nslookup any day than other commands, but you should aware that there is something called nscd which keeps a cache of most used queries on naming server. If a host got IP address change but still resolving to old IP address then this is one place you should check. Sometime name server can also be faulty.

Источник

How can I resolve a hostname to an IP address in a Bash script?

Every system in a TCP/IP network is assigned a unique identifier known as IP address that helps to connect it with other system o the network and all over the internet. All the websites you access on the internet also have unique IP addresses. As it is difficult for everyone to remember the IP addresses of these websites, the DNS system comes which helps to translate these hard to remember IP addresses into human-readable names. With DNS, you no longer have to remember the IP addresses. Instead, you have to just have to remember the domain name and all done. Actually, on the backed, the DNS server takes the hostname and resolves it to an IP address which the browser or application then connects to.

In this article, we will explain how to resolve a hostname/domain name to an IPv4 and IPv6 address in a Bash script. However, before proceeding towards creating the script, let us review some of the commands that can be used to resolve the hostname/domain name to an IP address.

Читайте также:  Linux kernel debian package

Ping

Ping is the most simple and built-in tool that is available on almost all operating systems. It is used to verify the reachability of a host in a network. However, we can also used it to find the IP address against any hostname/domain name. Use the following syntax to find the IP address of a targeted hostname/domain name:

Nslookup

Nslookup is widely used to resolve the hostname to an IP address. In order to use this command for an IP lookup, use the following syntax:

Host

Another command-line utility “host” can be used to find IP address against any hostname/domain name. In order to use this command, use the following syntax:

Dig

Dig is another useful command line tool that is used to query various DNS related records. It can be used to find IP address against any hostname/domain name. Use Dig command in the following way to find an IP address against a specific hostname/domain name.

Bash script to resolve a hostname to an IP address

In order to use the bash script for an IP lookup, follow the below steps:

    Create a bash file using any text editor. Here I will be using the Nano editor to create a script named “iplookup.sh”.

# Specify DNS server
dnsserver = «8.8.8.8»
# function to get IP address
function get_ipaddr
ip_address = «»
# A and AAA record for IPv4 and IPv6, respectively
#
ip_address = «»
# A and AAA record for IPv4 and IPv6, respectively
# $1 stands for first argument
if [ -n «$1» ] ; then
hostname = » $ »
if [ -z «query_type» ] ; then
query_type = «A»
fi
# use host command for DNS lookup operations
host -t $ $ &>/ dev / null $
if [ «$?» -eq «0» ] ; then
# get ip address
ip_address = » $(host -t $ $ $| awk ‘/has.*address/’) »
else
exit 1
fi
else
exit 2
fi
# display ip
echo $ip_address
stands for first argument
if [ -n »
ip_address = «»
# A and AAA record for IPv4 and IPv6, respectively
# $1 stands for first argument
if [ -n «$1» ] ; then
hostname = » $ »
if [ -z «query_type» ] ; then
query_type = «A»
fi
# use host command for DNS lookup operations
host -t $ $ &>/ dev / null $
if [ «$?» -eq «0» ] ; then
# get ip address
ip_address = » $(host -t $ $ $| awk ‘/has.*address/’) »
else
exit 1
fi
else
exit 2
fi
# display ip
echo $ip_address
» ] ; then
hostname = » $ »
if [ -z «query_type» ] ; then
query_type = «A»
fi
# use host command for DNS lookup operations
host -t $ $ &>/ dev / null $
if [ «$?» -eq «0» ] ; then
# get ip address
ip_address = » $(host -t $ $ $
hostname = » $ »
for query in «A-IPv4» «AAAA-IPv6» ; do
query_type = » $(printf $query | cut -d- -f 1) »
ipversion = » $(printf $query | cut -d- -f 2) »
address = » $(get_ipaddr $) »
if [ «$?» -eq «0» ] ; then
if [ -n » $ » ] ; then
echo «The $ adress of the Hostname $ is: $address »
fi
else
echo «An error occurred»
fi
done

The output would be similar to this: Similarly, to resolve the IP address of “yahoo.com”, the command would be:

The output would be similar to this: That is all there is to it! In this article, we have learned to resolve the hostname to an IPv4 and IPv6 address using a bash script. We also learned some other command-line tools such as Ping, Nslookup, Host, and Dig that can be used to perform an IP lookup.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Оцените статью
Adblock
detector