Skip to content
shellmap

Look up a hostname's IP address

Resolve a hostname (e.g. example.com) to its A / AAAA record from the shell.

How to look up a hostname's ip address in each shell

Bashunix
dig +short A example.com

`dig +short` gives JUST the IP addresses (one per line). `dig example.com` dumps the full DNS response (QUESTION + ANSWER + AUTHORITY + ADDITIONAL sections). `host example.com` is a more concise alternative. `dig @8.8.8.8 A example.com` queries a specific server (bypassing the OS resolver). `+norecurse` queries the server without iteration (useful for debugging authoritative chains).

Zshunix
dig +short A example.com

Same external `dig` (from BIND tools). macOS ships `dig` by default; on minimal Linux containers you may need `apt install dnsutils` / `apk add bind-tools` to get it. `host example.com` is the BSD/macOS-friendly concise alternative.

Fishunix
dig +short A example.com

Same external. Fish-friendly capture: `set -l ip (dig +short A example.com | head -1)`. For multiple records, iterate: `for r in (dig +short A example.com); echo $r; end`.

PowerShellwindows
Resolve-DnsName example.com -Type A

Built-in cmdlet — **Windows-only** in pwsh 5.1; pwsh 7 on Linux/macOS does NOT carry `Resolve-DnsName` (it's in the `DnsClient` module which Windows-only). On Linux/macOS pwsh, shell out to `dig` or use `[System.Net.Dns]::GetHostAddresses("example.com")` (.NET, cross-platform — but uses the OS resolver, not pure DNS). `-Server 8.8.8.8` queries a specific resolver; `-DnsOnly` skips the local resolver cache.

cmd.exewindows
nslookup example.com

`nslookup` is interactive by default — `nslookup example.com` does a one-shot query then exits. The output is human-formatted (`Server:`, `Address:`, `Non-authoritative answer:`) — not script-friendly. For just-the-IP: `for /f "tokens=2" %i in ('nslookup example.com ^| findstr Address ^| findstr /v "#53"') do @echo %i`. Better: shell out to pwsh `Resolve-DnsName`.

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

Gotchas & notes

  • **Resolver cache vs authoritative DNS**: the OS keeps a resolver cache (`/etc/nsswitch.conf` order on Linux, `/etc/hosts` first, then `mDNS`/`DNS` — verify with `getent hosts example.com`). `dig` queries the configured resolver (`/etc/resolv.conf`) which may also cache. To bypass ALL caches and ask the authoritative servers directly: `dig +trace example.com` walks the delegation from root → TLD → authoritative. Useful when DNS changes haven't propagated. pwsh `Resolve-DnsName -DnsOnly` skips local caches but still uses the configured DNS servers.
  • **`/etc/hosts` precedence trap**: by default the OS resolver checks `/etc/hosts` BEFORE DNS. A line like `127.0.0.1 example.com` makes `dig example.com` return the real IP (DNS) while `ping example.com` returns 127.0.0.1 (hosts). Diagnose with `getent hosts example.com` vs `dig +short example.com` — if they differ, hosts file is winning. Windows equivalent: `C:\Windows\System32\drivers\etc\hosts`, same precedence.
  • **A vs AAAA vs CNAME vs MX**: `-Type A` (IPv4), `-Type AAAA` (IPv6), `-Type CNAME` (alias chain), `-Type MX` (mail exchangers), `-Type TXT` (SPF/DKIM/verification), `-Type SOA` (zone authority), `-Type NS` (nameservers). For a complete picture: `dig example.com ANY` (but many resolvers refuse ANY queries as RFC 8482 — fall back to multiple typed queries). pwsh `Resolve-DnsName example.com -Type ANY` works on Windows but gives a per-resolver subset.
  • **Cross-OS scripts**: a hostname-to-IP one-liner that works on Linux, macOS, Windows: `python3 -c "import socket; print(socket.gethostbyname('example.com'))"` — uses the OS resolver (cache + hosts file + DNS). For pwsh portability: `[System.Net.Dns]::GetHostAddresses("example.com")[0].IPAddressToString` (cross-platform, .NET-based). Avoid `Resolve-DnsName` if your script must run on Linux/macOS pwsh.

Related commands

Related tasks