How to get the IP address of a Unix machine?

You can use ifconfig to get the IP address of any of the interfaces on the system (note that there may well be more than one interface and more than one IP address).

Start with:

 $ ifconfig -a

Use this command

host `hostname`

or this one

nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F# '{print $1}' | tail -n 1

Explanation

Start with nslookup

nslookup `hostname`      

then search for "address"

nslookup `hostname` | grep -i address

This will return something like

Address:    192.168.1.1#53
Address: 192.168.1.167

Now let's retrieve only the addresses by selecting the second column of text. We pass " " as the field separator

nslookup `hostname` | grep -i address | awk -F" " '{print $2}'

We'll get rid of the "#53" part by selecting the first column. We pass "#" as the field separator

nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F# '{print $1}'

The last address is the local address. Tail will help us get it.

nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F# '{print $1}' | tail -n 1

try this code to see the IP address of unix machine

nslookup mach_name

Tags:

Unix

Ip