How to get the IP address of a Unix machine?

I need to know the IP address of a UNIX machine. I can login to it with ssh but don't know the IP address.

Can anyone please tell me a command to get the IP address of the Unix machine I logged in to?


Solution 1:

try this code to see the IP address of unix machine

nslookup mach_name

Solution 2:

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

Solution 3:

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

Solution 4:

An alternative to ipconfig is ip(8) where the output can be narrowed somewhat. For example:

$ ip -f inet addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    inet 172.31.39.10/24 brd 172.31.39.255 scope global eth0

The interface which is not loopback (lo) is the one you want: 172.31.39.10