brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ipconfig getifaddr en0 prints just your Mac's IP address

macOS
~/tips/tip-macos-ipconfig-getifaddr

ipconfig getifaddr en0

Get the local IP address of one interface as a single line, ready for scripts.

ifconfig on a Mac prints a wall of text for a dozen interfaces. When you just want your address on the local network, there’s a shorter way.

Ask ipconfig for the address of one interface:

ipconfig getifaddr en0

The answer is one line with an IPv4 address and nothing else. That makes it easy to use in a script or to copy into another device, for example when you want to open a dev server on your phone or type the Mac’s address into an app on the TV.

The man page says it prints the IP address for the first network service on that interface, and that the output is empty if no service is configured or active there. I find that empty result useful in itself: a script can check whether you’re on the cable or on Wi-Fi.

ipconfig getifaddr en5 || echo "not on the cable"

The command exits with an error when there’s no address, which is why the || part works.

The catch is the device name. en0 is usually Wi-Fi on a MacBook, but an Ethernet adapter or dock gets another name, and desktop Macs can differ. Run networksetup -listallhardwareports once to see which device is which. And this is your local address; the address websites see is the public one from your router.

Further reading: ipconfig.8 man page

More macOS tips System Settings > Accessibility > Hover Text > When using the pointer A Smart Folder in Notes collects notes by tag, checklist or date Type # in a Mac note to tag it, then filter by tag in the sidebar tmutil isexcluded ~/Projects/app/node_modules Crop an image in Preview by dragging a frame and clicking Crop All macOS tips (99) All topics

Link