Skip to content
shellmap

Get the local IP address

Find the IPv4 address your machine is using on the local network — for sharing dev servers, configuring peers, and debugging "why can't my phone reach my laptop".

How to get the local ip address in each shell

Bashunix
ip -4 addr show | grep -oP "inet \K[\d.]+" | grep -v 127.0.0.1

Modern Linux uses `ip` from iproute2 (procps-ng replacement). For a single primary IPv4: `hostname -I | awk '{print $1}'` (GNU `-I` shows all non-loopback IPs, space-separated). `ifconfig` still works if installed (`net-tools`) but is deprecated.

Zshunix
ipconfig getifaddr en0
Fishunix
ipconfig getifaddr en0
PowerShellwindows
(Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notmatch "Loopback" -and $_.IPAddress -ne "127.0.0.1" }).IPAddress

`Get-NetIPAddress` requires Win8+/Server2012+ (NetTCPIP module). On older Windows fall back to `ipconfig` parsing or `Get-WmiObject Win32_NetworkAdapterConfiguration`.

cmd.exewindows
ipconfig | findstr /R /C:"IPv4 Address"

Output includes ALL active interfaces (Ethernet, Wi-Fi, VPN, virtual). For just the primary: `route print 0.0.0.0 | findstr "0.0.0.0"` shows the default-route interface, then cross-reference.

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • **Linux: `ip addr show` is the modern command** — iproute2 replaced net-tools (`ifconfig`, `route`, `netstat`, `arp`) over the 2010s. Most distros still ship `ifconfig` for muscle memory, but it's not in Alpine's default install and may report wrong info on multi-IP interfaces. `ip -4 addr show` (filter to IPv4), `ip -6 addr show` (IPv6 only), `ip -o addr show` (one line per address — easier for grep). Quick "what's my primary IP for outbound": `ip route get 1.1.1.1 | awk '/src/ {print $7}'` — asks the kernel which source IP it would use to reach a real host, which is the most reliable definition of "primary" on a multi-homed box.
  • **macOS: `ifconfig` is still canonical** (`ifconfig en0 | grep "inet "` for Wi-Fi, `en1` for second Ethernet). The portable helper: `ipconfig getifaddr en0` — a macOS-specific `ipconfig` (NOT the same `ipconfig` as Windows; macOS's lives at `/usr/sbin/ipconfig`) that prints just the IPv4 of one interface, no parsing. For "find the active interface name without hardcoding `en0`": `route -n get default | awk '/interface:/ {print $2}'`. macOS Sequoia + still ships `ifconfig` despite Linux deprecating it.
  • **Windows: three idioms, choose by version**: (1) `ipconfig` for any version since Win95 — human-readable, but multi-interface output needs parsing. (2) `Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "Wi-Fi"` for Win8+/pwsh ≥ 4 — structured objects, filterable. (3) `(Test-Connection -ComputerName 1.1.1.1 -Count 1).Source` returns the source IP the kernel would use to reach 1.1.1.1 — equivalent to the Linux `ip route get` idiom. WSL adds another layer: inside WSL2 you see the WSL VM's IP, NOT the Windows host's; `cat /etc/resolv.conf` shows the host IP that WSL uses as its gateway (changes on every WSL restart).
  • **LOCAL ≠ PUBLIC** — `192.168.x.x`, `10.x.x.x`, `172.16-31.x.x` (RFC1918), `169.254.x.x` (link-local fallback) are all PRIVATE. Public IP requires asking an external service: `curl ifconfig.me`, `curl ipinfo.io/ip`, `curl https://api.ipify.org`, `dig +short myip.opendns.com @resolver1.opendns.com` (DNS-based, no HTTP). pwsh: `(Invoke-WebRequest ifconfig.me).Content`. On corporate networks behind layers of NAT, your "outbound public IP" depends on which target you ask (some traffic egresses through different gateways).
  • **Multi-interface machines** (laptops with Wi-Fi + Ethernet + VPN + Docker bridges) need a "primary for outbound" definition: `ip route get 1.1.1.1` (Linux), `route -n get default` (macOS), `(Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Sort-Object RouteMetric | Select -First 1).InterfaceAlias` (Windows). Docker creates `docker0`, `br-XXXX` bridges — `hostname -I` returns all of them space-separated, often confusing (`172.17.0.1` is your Docker bridge, NOT your LAN address). Filter explicitly: `ip -4 addr show | grep -oP "inet \K[\d.]+" | grep -v -E "^(127|172\.17|172\.18)\."`.

Related commands

Related tasks