23

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 Answers 5

8

try this code to see the IP address of unix machine

nslookup mach_name
16

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
2
  • 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, 2011 at 13:32
  • 1
    If 'ifconfig' is not a typo you can use command-not-found to lookup the package ... Oct 26, 2022 at 18:36
14

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
9

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

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .