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?
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?
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
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
.
Commented
Oct 17, 2011 at 13:32
If 'ifconfig' is not a typo you can use command-not-found to lookup the package ...
Commented
Oct 26, 2022 at 18:36
host `hostname`
or this one
nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F# '{print $1}' | tail -n 1
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
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
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.