[ifconfig] Get IP address of your machine (internal & external)

NOTE: Much simpler is hostname -I or hostname --all-ip-addresses which gives you all addresses for the host, IPV4s as well as IPV6s. Check hostname -h for more nifty details

ifconfig is for configuring a network interface. (Interface Configuration)

Get IP by interface (eth, wlan, lo etc)

ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

cmd-ifconfig-3

grep prints lines mathcing a pattern awk is a pattern scanning and processing language.

interfaces

  • eth0 = Ethernet
  • wlan0 = Wi-Fi
  • lo = Local Network (127.0.0.1)
  • inet = TCP/IP, default
  • inet6 = IPv6

See more about linux interface. ifconfig -l on a Mac lists all inetrfaces available.

Get IP of every interface

ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'

cmd-ifconfig-2

Get IPv6

ifconfig eth | grep --color 'inet6'

cmd-ifconfig

OR

ifconfig eth | grep 'inet6' | awk '{print $3}'

cmd-ifconfig-4

external IP

You can curl an endpoint for your external public IP, like so:

curl http://ipecho.net/plain; echo

There are plenty of services that give you your public IP address by going to a URL. You can then curl that URL to see your extrenal public IP in CLI.

Some alternatives are:

curl http://ip.appspot.com/
curl https://icanhazip.com/