How To

Mac Network Commands: IP Address, DNS Flush, networksetup

Type ip a on a Mac and zsh tells you the command does not exist. The usual mac IP address command, ipconfig getifaddr en0, can print nothing at all when the active link is not en0. macOS has its own tools for this: ipconfig and ifconfig for addresses, scutil for the DNS resolvers apps really use, and networksetup for anything the Network pane in System Settings can change.

Original content from computingforgeeks.com - post 72398

This guide finds the IP on the right interface, reads the router and DNS from DHCP, adds the ip command with iproute2mac (and shows the one thing it gets wrong), flushes DNS, and changes resolvers with networksetup. A Linux-to-macOS command map closes it out. Every command here was run on macOS 27.0 on an Apple silicon Mac mini on 31 October 2026.

Use the Mac IP address command on the right interface

ipconfig getifaddr prints the IPv4 address of one interface and nothing else, which makes it the quickest answer and the easiest one to drop into a script. The interface name trips people up. Most guides hardcode en0, and on our Mac mini en0 is the built-in Ethernet port with no cable in it, so the command printed an empty line. Do not guess the name. Ask the routing table which interface carries the default route, then query that one.

route -n get default | grep interface
ipconfig getifaddr en4

Here the default route leaves through en4, a port macOS names “Thunderbolt Ethernet Slot 1”, and that’s where the address lives. A MacBook on Wi-Fi or a Mac with a USB-C dongle will show another name, which is why you let route pick it. The first line of the capture is the error a stock Mac gives you for ip a, taken before iproute2mac went on.

Mac IP address command ipconfig getifaddr returning empty on en0 and 10.78.1.123 on en4 in macOS 27 Terminal

To skip the two-step lookup, nest it. This one-liner prints the address of whichever interface holds the default route, so it keeps working when you move from Wi-Fi to Ethernet:

ipconfig getifaddr $(route -n get default | awk '/interface:/{print $2}')

It returns the same private address as the manual lookup:

10.78.1.123

That is your LAN address, the one other machines on the same network connect to. It is not the address websites see, which comes up in the next section.

Read the router, subnet and DNS from DHCP

ipconfig getoption reads single fields out of the DHCP lease and prints each as one bare value, so you do not have to parse anything. We use it in scripts that need the gateway or the DNS server without pulling in a JSON parser. The public IP needs an outside service, because only the NAT gateway or your home router knows it.

ipconfig getoption en4 subnet_mask
ipconfig getoption en4 router
ipconfig getoption en4 domain_name_server
curl -s https://api.ipify.org; echo

The trailing echo is there because ipify returns the address with no newline, and zsh would otherwise mark the line with a stray %. The public address in the capture is replaced with a documentation IP.

ipconfig getoption subnet_mask router domain_name_server and public IP lookup on a Mac

For the whole lease, run ipconfig getsummary en4. It dumps the raw DHCP packet with the server identifier, lease start and expiry, and every option the server sent, including the search domain that later turns up in the DNS config. The options you’ll query most often are these:

  • router for the default gateway
  • subnet_mask in dotted form
  • domain_name_server for the DHCP-supplied resolver

If an option comes back empty, the DHCP server did not send it, or the interface is set to a manual address. networksetup -getinfo (later in this guide) tells you which of the two it is.

Read the full interface with ifconfig

ifconfig still ships with macOS and still works, so there’s nothing to install for a full view of one interface. It puts flags, MTU, the hardware address on the ether line, IPv4 and IPv6 addresses and the link state in one block.

ifconfig en4

Read the inet line first. macOS prints the netmask in hex, so 0xffffff00 is a /24. The 9001-byte MTU is AWS jumbo framing and you’ll see 1500 on a home network. status: active is the line that tells you a cable or Wi-Fi association is up. The capture adds ifconfig | grep 'inet ', which filters every interface down to its IPv4 lines. It’s the fastest way to spot a VPN or a second NIC that grabbed an address.

ifconfig en4 output on macOS 27 showing inet address, hex netmask, MTU 9001 and status active

The MAC address in that capture is masked. Real ether values are worth redacting before you paste output into a ticket, because they identify the machine.

Install iproute2mac for the ip command

If your hands only know Linux, iproute2mac gives you ip as a Python wrapper. Each call is translated into the macOS tools that do the work (ifconfig, route, netstat and ndp), and the package also ships ss and bridge. It installs from Homebrew, so if brew is not on your Mac yet, set up the Xcode Command Line Tools and then Homebrew first.

brew install iproute2mac

The formula is 88 KB, but it is not a light install. It needs a Python of its own, so on our Mac it pulled in a Homebrew Python before pouring. The capture further down keeps only the final pour lines of that long output. After the install, /opt/homebrew/bin/python3 points at that Homebrew Python, which matters if your scripts call python3 and expect a particular version. Once it’s in, check the version and try the commands you’d use on Linux.

ip -V
ip -br -4 a
ip route get 1.1.1.1

ip -br -4 a is the brief view, one line per interface with an IPv4 address, and it beats scrolling through ifconfig. ip route get answers the same question as route -n get default. It also prints the source address the Mac would use, which helps when a VPN adds routes.

brew install iproute2mac then ip -br -4 a and ip route get 1.1.1.1 on macOS 27

The wrapper is narrower than the Linux tool. Per the iproute2mac README, it handles these:

  • Objects link, addr, route and neigh
  • -br for the brief one-line view
  • -4 and -6 to filter by address family

The README warns that output is not fully compatible with iproute2. Do not feed it to scripts written for Linux and expect identical columns.

Why ip link shows 02:00:00:00:00:00

On macOS 27, ip link show en4 reported link/ether 02:00:00:00:00:00 while networksetup showed the real address. We traced it rather than blame the parser. iproute2mac runs /sbin/ifconfig from the Homebrew Python, and the same call from that interpreter got the placeholder back. The identical call from the system /usr/bin/python3, and from the shell, got the real MAC. So macOS hands the masked value to the Homebrew interpreter, and iproute2mac prints what it is given.

iproute2mac ip link showing placeholder MAC 02:00:00:00:00:00 while networksetup and ifconfig show the real address on macOS 27

Take the MAC from networksetup -getmacaddress or a plain ifconfig in your shell. Addresses, routes and neighbours from ip were all correct on the same host, so the rest of the wrapper is safe to use.

Check which DNS servers macOS uses

/etc/resolv.conf still exists, but its own header says it “is not consulted for DNS hostname resolution” by most processes. It points you to scutil --dns instead. macOS resolves names through mDNSResponder, which can hold several resolvers at once, one per interface plus scoped ones for .local and VPN domains, and scutil shows that whole list.

scutil --dns | head -16
networksetup -getdnsservers "Thunderbolt Ethernet Slot 1"

Resolver #1 is the DHCP-supplied server 10.78.0.2 with its search domain, bound to en4. Resolver #2 handles .local names over multicast DNS. The networksetup line looks alarming but is harmless. “There aren’t any DNS Servers set” means nobody configured a manual server on that service, so the DHCP one is in use.

scutil --dns resolver list and networksetup -getdnsservers on macOS 27

Don’t test DNS on a Mac with dig alone. dig and nslookup read /etc/resolv.conf and ask the server directly, so they skip the system cache and any scoped resolver. To see what Safari or curl would get, use dscacheutil -q host -a name <host>, which goes through the same path apps use.

Flush the DNS cache on a Mac

You flush the DNS cache when a record changed and the Mac keeps returning the old address, or after you switch DNS servers and want the new ones answering at once. It takes two commands, both with sudo. One clears the Directory Services cache, the other tells mDNSResponder to drop its cache.

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Neither prints anything, which is normal. Check the exit status and resolve a name through the system path to confirm lookups still work:

Flush DNS on Mac with dscacheutil -flushcache and killall -HUP mDNSResponder, then dscacheutil -q host

The HUP signal clears the cache without restarting the daemon, so nothing else on the network stack is disturbed. We ran it over SSH and the session never blinked, since an open TCP connection does not go back to DNS once it’s established.

Change DNS servers with networksetup

networksetup works on network services by name (“Wi-Fi”, “Thunderbolt Ethernet Slot 1”), not on device names like en4. List the services first, since every other subcommand needs that exact name in quotes. An asterisk marks a disabled service.

networksetup -listallnetworkservices
networksetup -getinfo "Thunderbolt Ethernet Slot 1"

-getinfo answers the question ipconfig cannot. It says “DHCP Configuration” or “Manual Configuration” at the top, then the address, mask, router and hardware address. Use networksetup -listallhardwareports when you need to map a service name back to its device.

networksetup -listallnetworkservices and -getinfo showing DHCP configuration on a Mac

To point the Mac at different resolvers, pass the servers in order. Setting the word empty clears them and puts the service back on DHCP DNS:

sudo networksetup -setdnsservers "Thunderbolt Ethernet Slot 1" 1.1.1.1 9.9.9.9
networksetup -getdnsservers "Thunderbolt Ethernet Slot 1"
sudo networksetup -setdnsservers "Thunderbolt Ethernet Slot 1" empty

The change applies immediately, with no restart. scutil --dns showed 1.1.1.1 and 9.9.9.9 as resolver #1 straight after the set, and 10.78.0.2 returned once we cleared it:

networksetup -setdnsservers 1.1.1.1 9.9.9.9 then empty, verified with scutil --dns on macOS 27

Changing DNS does not touch the IP address, so an SSH session survives it. Switching the address of a remote Mac with -setmanual or -setdhcp is a different story, because the session drops the moment the address moves. We did not run those on the remote host for that reason, and you shouldn’t either without console access. The read-only getters below were all run on the same Mac.

CommandWhat it returned
networksetup -listnetworkserviceorderServices in priority order with their devices
networksetup -getmtu "<service>"Active MTU: 9001 (Current Setting: 9001)
networksetup -getmacaddress "<service>"The real hardware address
networksetup -getsearchdomains "<service>"None set, so DHCP supplies them
networksetup -getcomputernameMac mini
networksetup -getairportnetwork en0All Wi-Fi network services are disabled

Our host has no Wi-Fi service at all, hence that last message. We could not test -getairportnetwork against a joined Wi-Fi network, and recent releases put the network name behind Location Services, so on a laptop rely on the Wi-Fi menu for the SSID.

Map Linux network commands to macOS

If you work on Linux servers all day and on a Mac in between, this is the table worth keeping. Every macOS command in it was run for this guide. The Linux side assumes systemd-resolved, and the nmcli cheat sheet covers the NetworkManager equivalents.

TaskLinuxmacOS built-in
IP of one interfaceip -4 addr show eth0ipconfig getifaddr en4
All interfacesip aifconfig
Default routeip routeroute -n get default or netstat -rn -f inet
DNS resolvers in useresolvectl statusscutil --dns
Flush DNS cacheresolvectl flush-cachessudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Set DNS serversnmcli con mod <conn> ipv4.dns "1.1.1.1 9.9.9.9"sudo networksetup -setdnsservers "<service>" 1.1.1.1 9.9.9.9
Resolve like an appgetent hosts <host>dscacheutil -q host -a name <host>

Learn ipconfig getifaddr, scutil --dns and networksetup first. Treat iproute2mac as a convenience for addresses and routes, not a dependency, because it drags a whole Python into /opt/homebrew and gets the MAC wrong on macOS 27. For the rest of the Mac setup, the Zsh and Oh My Zsh guide pairs well with these aliases, LuLu shows you which apps open outbound connections, and the IP settings check across Windows, macOS and Linux helps when the same network problem hits all three. If you’re still settling into the new release, the macOS 27 developer notes list what else changed.

Keep reading

Free CCNA Practice Test: 600+ Questions for 200-301 v2.0 Networking Free CCNA Practice Test: 600+ Questions for 200-301 v2.0 Install Samba on Debian 13 / 12 and Create a File Share Debian Install Samba on Debian 13 / 12 and Create a File Share Configure Samba File Sharing on Linux Mint 22 Networking Configure Samba File Sharing on Linux Mint 22 Ollama Text to Speech: Run Phonon on a Mac (Tested) AI Ollama Text to Speech: Run Phonon on a Mac (Tested) Agentic AI in Network Operations: Tested on Cisco IOS Networking Agentic AI in Network Operations: Tested on Cisco IOS Install Android CLI on Linux, macOS, and Windows (Agent-Ready) AI Install Android CLI on Linux, macOS, and Windows (Agent-Ready)

Leave a Comment

Press ESC to close