15

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?

1
5

try this code to see the IP address of unix machine

nslookup mach_name
14

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
1
  • 4
    Some UNIX systems do not label their interfaces en0 - a more general answer is to use ifconfig -a. Also, the ifconfig binary is not always on the PATH for ordinary users so they may need to try various incantations such as /sbin/ifconfig -a, /etc/ifconfig -a or login as root and try ifconfig -a. Oct 17 '11 at 13:32
10

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
5

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

0

use ifconfig and look for the inet part of the output. Note if you have more than one network card (ethernet and wireless for example) there will be more than one entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.