Last Updated: February 25, 2016
·
1.454K
· tonyseek

Getting Local Machine IP Address in Shell

IPv4 Address

ifconfig | awk '$1 == "inet" && $2 != "127.0.0.1" { print $2 }'

IPv6 Address

ifconfig | awk '$1 == "inet6" && $2 != "::1" { print $2 }'

2 Responses
Add your response

'ifconfig' is obsolete (according to its own man page). Since it could be going away in future, the use of 'ip' command is recommended. Therefore something like the following could be better.

ip addr | awk '$1 == "inet" && $2 != "127.0.0.1/8" { split ($2, a, "/"); print a[1] }'

over 1 year ago ·

curl ifconfig.me

over 1 year ago ·