Mac OS X, 10.12.2, Command Line how to add Search Domains

dnsterminal

I do this all the time with the GUI and it does what I am looking for. No Problem.

System Preferences > Network > Select the LAN or Wi-Fi > Advanced > DNS > Search Domains and in that particular box field, I add all the search domains that I want. e.g. office, hq, ad.gwn, etc.

Question, how can I achieve the same process via command line with Terminal?

So far this is what I did:

Step #1 I run both these command line to get an overview of what I have:

  • networksetup -listallnetworkservices
  • networksetup -listnetworkserviceorder

and this is what I got:

  • List item
  • Apple USB Ethernet Adapter
  • Thinkpad USB LAN
  • Wi-Fi
  • Bluetooth PAN
  • Thunderbolt Bridge
  • (1) Apple USB Ethernet Adapter
    (Hardware Port: Apple USB Ethernet Adapter, Device: en4)
  • (2) Thinkpad USB LAN
    (Hardware Port: Thinkpad USB LAN, Device: en5)
  • (3) Wi-Fi
    (Hardware Port: Wi-Fi, Device: en0)
  • (4) Bluetooth PAN
    (Hardware Port: Bluetooth PAN, Device: en3)
  • (5) Thunderbolt Bridge
    (Hardware Port: Thunderbolt Bridge, Device: bridge0)

Step #2 I tried to add my search domains (hq, office, ad.gwn) but I received the following error message:
– sudo networksetup -setsearchdomains en4 hq office ad.gwn
– en4 is not a recognized network service
– Error: The parameters were not valid

I am connected only with Ethernet cable. No WiFi. I tried en4 and en5 same issue.

Best Answer

First list all network services to get an overview:

networksetup -listallnetworkservices

Output example:

An asterisk (*) denotes that a network service is disabled.
en0
en1

I renamed my two network services from "Ethernet" to en0 and "Ethernet Adapter (1)" to en1. You have to use your actual network service names of course! If the network service name contains spaces you have to use quotes (e.g. "Apple USB Ethernet Adapter") in the commands below.

To set search domains for a network service use:

sudo networksetup -setsearchdomains networkservice domain1 [domain2] [...]

Example:

sudo networksetup -setsearchdomains en0 example.home example.net example

Check with networksetup -getsearchdomains en0:

example.home
example.net
example

If you want to add search domains to already existing domains use:

SEARCHDOMAIN=$(networksetup -getsearchdomains en0)
sudo networksetup -setsearchdomains en0 $SEARCHDOMAIN domain1 domain2 ...