3

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?

6
  • 1
    When you say LAN IP, are you looking for the one network gateway with the default IPv4 address? macOS have several networks and dumping the routing table to process them might be the closest equivalent on the different OS. Or is this more how to use a tool like awk or cut to process the output of ifconfig?
    – bmike
    Commented Aug 12 at 19:44
  • @bmike yes. I suppose I could use awk, but I figured there was a better solution. Commented Aug 12 at 20:02
  • Also I'm not too familiar with ifconfig output, so I don't know how stable awk would be. Commented Aug 12 at 20:04
  • 2
    Everything I know is far more verbose than ifconfig except ifconfig . You could make a shell alias and call it what you desire. Thinking ioreg, scutil --nwi, netstat, system-profiler …
    – bmike
    Commented Aug 12 at 20:06
  • @IconDaemon I happy with the answer I received, but I realized afterward that I was simply googling the wrong question. I thought it would be helpful for others to have a link to the duplicate answer. Commented Aug 13 at 15:02

1 Answer 1

4

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
2
  • 1
    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).
    – nohillside
    Commented Aug 13 at 19:32
  • 1
    Ah, figured it out: scutil --nwi | sed -n '/..*IPv4/{n;s/.*: //p;}'
    – nohillside
    Commented Aug 14 at 17:30

Not the answer you're looking for? Browse other questions tagged .