How do I find my IP Address from the command line?
Use ipconfig getifaddr en1
for wireless, or ipconfig getifaddr en0
for ethernet.
ipconfig getifaddr en0
is default for the Wi-Fi network adapter.
Alternatively you can also do:
dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com
Output, e.g.:
172.79.136.120
The following works for me on 10.8 and on 10.10 Yosemite.
ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}'
If you find the above gives you more than one answer, save the following to a script, and run it instead
ip_address.sh
#!/usr/bin/env bash
dumpIpForInterface()
{
IT=$(ifconfig "$1")
if [[ "$IT" != *"status: active"* ]]; then
return
fi
if [[ "$IT" != *" broadcast "* ]]; then
return
fi
echo "$IT" | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
}
main()
{
# snagged from here: https://superuser.com/a/627581/38941
DEFAULT_ROUTE=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
if [ -n "$DEFAULT_ROUTE" ]; then
dumpIpForInterface "$DEFAULT_ROUTE"
else
for i in $(ifconfig -s | awk '{print $1}' | awk '{if(NR>1)print}')
do
if [[ $i != *"vboxnet"* ]]; then
dumpIpForInterface "$i"
fi
done
fi
}
main
Just type curl ifconfig.me
in the terminal.