SkillPilotAI

networksetup

networksetup is the command-line version of System Settings > Network on macOS. It configures IPv4, IPv6, DNS, proxies, Wi-Fi, service order, MTU, VLANs, bonds, PPPoE and locations, which makes it the tool for scripting Mac network changes.

30.1 Where it runs and syntax

  • Built in only on macOS; on Windows netsh does this job, on Linux ip or nmcli.
  • -get / -list commands work as a normal user; -set commands need sudo.
  • Syntax: networksetup -<command> [service | device] [args]. Quote service names with spaces ("Wi-Fi", "Thunderbolt Bridge").

30.2 How it works: location, service, device

TermMeaningExample
Device (hardware port)The real interfaceen0 (Wi-Fi), en1 (Ethernet)
Network serviceSettings on top of a port: IP, DNS, proxy"Wi-Fi", "Ethernet"
LocationA full set of services you can switch betweenOffice, Home
  • Most commands take the service name; Wi-Fi power and join commands (setairportpower, setairportnetwork) take the device en0.
  • Service order decides which service is used first (e.g. Ethernet, then Wi-Fi).
Service order: Ethernet first, then Wi-Fi
  1. 1.MacBook, location Office: Ethernet (en1) is order 1, Wi-Fi (en0) order 2

  2. 2.DNS query goes out via the Ethernet switch

  3. 3.Router 192.168.1.1 forwards it to DNS server 1.1.1.1

  4. 4.Ethernet cable is pulled out

  5. 5.macOS automatically uses the next service: Wi-Fi

  6. 6.Same query now goes via the access point and router to 1.1.1.1

Change the order with -ordernetworkservices

30.3 Info, help and services

  • -listallhardwareports: port, device, MAC; -listallnetworkservices (* = disabled); -listnetworkserviceorder: order + device.
  • -getinfo SERVICE: IP, mask, router, IPv6 mode; -getmacaddress, -get/-setcomputername, -detectnewhardware, -printcommands, -help, -version.
Terminal
networksetup -listallhardwareportsnetworksetup -getinfo "Wi-Fi"networksetup -listallnetworkservicesnetworksetup -listnetworkserviceordernetworksetup -getmacaddress en0networksetup -getcomputernamesudo networksetup -setcomputername "Office-Mac"
Output
$ networksetup -listallhardwareportsHardware Port: Wi-FiDevice: en0Ethernet Address: 3c:22:fb:12:34:56 Hardware Port: EthernetDevice: en1Ethernet Address: 3c:22:fb:ab:cd:ef$ networksetup -getinfo "Wi-Fi"DHCP ConfigurationIP address: 192.168.1.10Subnet mask: 255.255.255.0Router: 192.168.1.1IPv6: AutomaticWi-Fi ID: 3c:22:fb:12:34:56

30.4 IPv4 and IPv6

CommandWhat it does
-setmanual S IP MASK ROUTERStatic IPv4
-setdhcp S [clientid | Empty]IPv4 via DHCP (optional client ID)
-setbootp S / -setv4off SBOOTP / IPv4 off
-setmanualwithdhcprouter S IPManual IP, router from DHCP
-get/-setadditionalroutes S [NET MASK GW]Extra static routes (no args = clear)
-setv6off | -setv6automatic | -setv6linklocal SIPv6 off / SLAAC + DHCPv6 / link-local only
-setv6manual S ADDR PREFIX ROUTERStatic IPv6
-create6to4service, -set6to4automatic | manual6to4 tunnel service and relay
Terminal
sudo networksetup -setmanual "Wi-Fi" 192.168.1.10 \  255.255.255.0 192.168.1.1sudo networksetup -setdhcp "Wi-Fi"sudo networksetup -setmanualwithdhcprouter "Wi-Fi" 192.168.1.10sudo networksetup -setadditionalroutes "Wi-Fi" 10.0.0.0 \  255.0.0.0 192.168.1.254networksetup -getadditionalroutes "Wi-Fi"sudo networksetup -setv6manual "Wi-Fi" 2001:db8::10 64 2001:db8::1sudo networksetup -setv6automatic "Wi-Fi"

30.5 DNS and proxies

  • -getdnsservers / -setdnsservers S IP... (any number); 'empty' goes back to DHCP DNS.
  • -get/-setsearchdomains S DOMAIN... | empty.
  • Proxies per service: web (HTTP), securewebproxy (HTTPS), socksfirewallproxy; each has a ...state on|off command; set takes HOST PORT [on USER PASS].
  • -setproxybypassdomains, -setproxyautodiscovery on (WPAD), -setautoproxyurl URL (PAC).
Terminal
networksetup -getdnsservers "Wi-Fi"sudo networksetup -setdnsservers "Wi-Fi" 1.1.1.1 8.8.8.8sudo networksetup -setdnsservers "Wi-Fi" emptysudo networksetup -setsearchdomains "Wi-Fi" company.com corp.comsudo networksetup -setwebproxy "Wi-Fi" proxy.corp.com 8080sudo networksetup -setwebproxystate "Wi-Fi" offsudo networksetup -setproxybypassdomains "Wi-Fi" localhost 127.0.0.1 "*.local"sudo networksetup -setautoproxyurl "Wi-Fi" http://wpad.corp.com/proxy.pac

30.6 Wi-Fi (AirPort)

  • Wi-Fi commands take the device: -getairportnetwork en0 (SSID), -setairportnetwork en0 SSID PW (join), -get/-setairportpower en0 on|off.
  • Saved networks: -listpreferredwirelessnetworks, -addpreferredwirelessnetworkatindex en0 SSID INDEX SECURITY PW, -removepreferredwirelessnetwork, -removeallpreferredwirelessnetworks.
  • Security types: OPEN, WPA, WPA2, WPA/WPA2, WPAE, WPA2E, WPAE/WPA2E, WEP, 8021XWEP.
Terminal
networksetup -getairportnetwork en0sudo networksetup -setairportnetwork en0 MyWiFi secret123networksetup -getairportpower en0sudo networksetup -setairportpower en0 offnetworksetup -listpreferredwirelessnetworks en0sudo networksetup -addpreferredwirelessnetworkatindex \  en0 MyWiFi 0 WPA2 secret123sudo networksetup -removepreferredwirelessnetwork en0 OldCafe

30.7 Services, order, MTU, media, VLANs and bonds

AreaCommands
Services-get/-setnetworkserviceenabled, -createnetworkservice NAME DEV, -renamenetworkservice, -duplicatenetworkservice, -removenetworkservice
Order-ordernetworkservices S1 S2 ... (all services, in order)
MTU-getMTU, -setMTU DEV N, -listValidMTURange
Media-getMedia, -setMedia DEV autoselect | 100baseTX half-duplex, -listValidMedia, -setMTUAndMediaAutomatically
VLAN-createVLAN NAME DEV TAG, -deleteVLAN, -listVLANs, -listdevicesthatsupportVLAN
Bond-isBondSupported, -createBond NAME DEV..., -addDeviceToBond, -removeDeviceFromBond, -deleteBond, -listBonds, -showBondStatus
Terminal
sudo networksetup -setnetworkserviceenabled "Wi-Fi" offsudo networksetup -createnetworkservice "Ethernet 2" en5sudo networksetup -renamenetworkservice "Ethernet 2" "Dock LAN"sudo networksetup -ordernetworkservices "Ethernet" \  "Wi-Fi" "Thunderbolt Bridge"networksetup -getMTU en0sudo networksetup -setMTU en0 1500networksetup -getMedia en0sudo networksetup -setMedia en0 autoselectsudo networksetup -createVLAN Office en0 10sudo networksetup -createBond MyBond en0 en1networksetup -showBondStatus bond0

30.8 PPPoE and locations

  • PPPoE: -createpppoeservice DEV NAME ACCOUNT PASSWORD SERVICE, -showpppoestatus, -connect / -disconnectpppoeservice, -setpppoeaccountname, -setpppoepassword NAME - < file.
  • Locations: -listlocations, -getcurrentlocation, -createlocation NAME [populate], -deletelocation, -switchtolocation (sudo for changes).
Terminal
sudo networksetup -createpppoeservice en0 MyPPPoE \  user@isp secret ISPnetworksetup -showpppoestatus MyPPPoEsudo networksetup -connectpppoeservice MyPPPoEnetworksetup -listlocationsnetworksetup -getcurrentlocationsudo networksetup -createlocation Home populatesudo networksetup -switchtolocation Office

30.9 Practical: the same jobs on Linux

JobmacOS networksetupLinux (real lab)
Static IP + router-setmanual S IP MASK ROUTERip addr add ... ; ip route add default via ...
DNS-setdnsservers S 1.1.1.1/etc/resolv.conf: nameserver 1.1.1.1
MTU-setMTU en0 1400ip link set eth0 mtu 1400
Speed / duplex-getMedia en0ethtool eth0
VLAN-createVLAN Office en0 10ip link add link eth0 name eth0.10 type vlan id 10
Bond-createBond MyBond en0 en1ip link add bond0 type bond; ip link set d1 master bond0
Terminal
ip -4 addr show eth0ip addr flush dev eth0ip addr add 192.168.50.20/24 dev eth0ip route add default via 192.168.50.1ip -4 addr show eth0echo nameserver 1.1.1.1 > /etc/resolv.confdig +short example.com
Output
$ ip -4 addr show eth09: eth0@if480: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500    inet 192.168.50.10/24 brd 192.168.50.255 scope global eth0$ ip addr flush dev eth0$ ip addr add 192.168.50.20/24 dev eth0$ ip route add default via 192.168.50.1$ ip -4 addr show eth09: eth0@if480: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500    inet 192.168.50.20/24 scope global eth0$ echo nameserver 1.1.1.1 > /etc/resolv.conf$ dig +short example.com104.20.23.154172.66.147.243
Terminal
ip link set eth0 mtu 1400ip link show eth0ethtool eth0 | grep -E 'Speed|Duplex'ip link add link eth0 name eth0.10 \  type vlan id 10ip -d link show eth0.10ip link add bond0 type bond \  mode active-backupip link add d1 type dummyip link add d2 type dummyip link set d1 master bond0ip link set d2 master bond0ip link set bond0 upcat /proc/net/bonding/bond0
Output
$ ip link set eth0 mtu 1400$ ip link show eth09: eth0@if480: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400$ ethtool eth0 | grep -E 'Speed|Duplex'    Speed: 10000Mb/s    Duplex: Full$ ip -d link show eth0.1010: eth0.10@eth0: <BROADCAST,MULTICAST> mtu 1400    vlan protocol 802.1Q id 10 <REORDER_HDR>$ cat /proc/net/bonding/bond0Bonding Mode: fault-tolerance (active-backup)Currently Active Slave: d1MII Status: up Slave Interface: d1MII Status: up Slave Interface: d2MII Status: up

30.10 Common mistakes

  • Passing the service name 'Wi-Fi' to setairportpower: Wi-Fi power/join commands take the device (en0).
  • Hard-coding en0 as Wi-Fi: on an iMac or Mac mini en0 can be Ethernet; find it with -listallhardwareports first.
  • Forgetting sudo for -set commands, or not quoting service names with spaces.
  • Forgetting to roll back: -setdhcp S and -setdnsservers S empty.

30.11 Interview tips

  • Static IP + DNS on a Mac: get the service name (-listallnetworkservices), sudo -setmanual S IP MASK ROUTER, -setdnsservers S ..., verify with -getinfo and -getdnsservers.
  • Rollback: -setdhcp S and -setdnsservers S empty.
  • Turn Wi-Fi off: sudo networksetup -setairportpower en0 off (device, not service).
  • Equivalents: Windows netsh, Linux ip / nmcli.