The Linux version of hostname
has the -I
flag, which prints the LAN IP. The Mac OS version of hostname
doesn't have the -I
flag, so the only way I know how to get the LAN IP is with ifconfig
.
How can I print only the LAN IP from terminal?
The way to determine the address of an arbitrary Ethernet interface is easy and clean.
ipconfig getifaddr en0
The problem is deciding if en0, en1, en2 or en??? is active or if many are active, which address you want. The ifconfig
is a bit messier as it lists IPv6 addresses that are used heavily for AirDrop and mDNS type traffic.
Perhaps an scutil --nwi | grep DNS | grep IPv4
is good enough to grab an active Ethernet abbreviation to feed to my first command is good enough for your networking choices?
$ scutil --nwi
Network information
IPv4 network interface information
en0 : flags : 0x7 (IPv4,IPv6,DNS)
address : 192.168.1.161
reach : 0x00000002 (Reachable)
en1 : flags : 0x7 (IPv4,IPv6,DNS)
address : 192.168.1.208
reach : 0x00000002 (Reachable)
REACH : flags 0x00000002 (Reachable)
IPv6 network interface information
en0 : flags : 0x7 (IPv4,IPv6,DNS)
address : 2a02:168:67f1:0:cca:5bb4:83ae:8df3
reach : 0x00000002 (Reachable)
en1 : flags : 0x7 (IPv4,IPv6,DNS)
address : 2a02:168:67f1:0:4e2:ddde:ef49:c3ea
reach : 0x00000002 (Reachable)
REACH : flags 0x00000002 (Reachable)
Network interfaces: en0 en1 utun5
Getting to
$ ipconfig getifaddr en0
192.168.1.161
$ ipconfig getifaddr en1
192.168.1.208
scutil --nwi | grep -A 1 -e IPv4 | sed -En '/address/s/.*: (.*)/\1/p'
(and I know that grep | sed
shouldn't be necessary, but somehow I couldn't get it to work with sed
only).
scutil --nwi | sed -n '/..*IPv4/{n;s/.*: //p;}'
awk
orcut
to process the output of ifconfig?awk
, but I figured there was a better solution.ifconfig
output, so I don't know how stableawk
would be.