2

I want to automate the creation of my network locations (say Home and Work) and was looking into networksetup -createlocation <location name> [populate]

Adding populate adds default services to the network location (USB LAN, Wi-Fi, Bluetooth PAN, Thunderbolt Bridge) and without leaves the location empty.

Is there a way to add specific services to a network location? networksetup -printcommands | grep -i location is not coming up with anything helpful.

1

networksetup uses a somehow hierarchical name system for the subcommands. location subcommands are used to manage locations itself only: you can either list, get (the current), create, delete or switch between locations.

Modifying the network services of a location is done in the networkservice "name space".

To manage the network services in one location you can either use "blacklisting" or a "whitelisting":

If you execute networksetup -createlocation <location name> with populate and remove unwanted network services it's blacklisting, without populate you have to add all required network services and it's rather whitelisting.

To remove a network service in case of blacklisting use:

... -removenetworkservice <networkservice>

To add a network service in case of whitelisting use:

...  -createnetworkservice <networkservice>
  • I wanted to go for a whitelisting approach but failed to see how to do it. As pointed out in the other answer, one has to switch to the new location as networksetup can only create services on the current one. This feels weird to me, as this means losing connectivity. Any way to do this while keeping connectivity? – oschrenk Dec 1 '17 at 13:48
  • @oschrenk Transfer a shell script (starting with networksetup -switchlocation (local) and containing all other networksetup commands for (local) and finally a networksetup -switchlocation (remote)) to the remote stdin! – klanomath Dec 1 '17 at 14:05
0

According to the man page for networksetup (man networksetup)

-createlocation location [populate] Create a set with the user-defined-name name and optionally populate it with the default services.

So, the behavior you are seeing is expected. You don't need to use the populate function; just add your services al la carte as needed.

That said, before you can modify a location, you have to switch to it first (make it active):

$ networksetup -listlocations             <------ Lists all locations configured
$ networksetup -getcurrentlocation        <------ Currently selected location
$ networksetup -switchlocation location  <------ Sets current location

Using your two locations (Home and Work), switch to the location you want then add/modify/remove the service(s) in question

For example, to create a "TestLAN" service using the Bluetooth PAN hardware device on the Home network, use the following:

$ networksetup -switchlocation Home
$ networksetup -createnetworkservice TestLAN "Bluetooth PAN"

Additional commands you may wish to explore with networksetup are:

-listallhardwareports
-listlocations
-listnetworkserviceorder
-listallnetworkservices

However, most importantly, you should explore the man page for more details.

  • Switching network location was the missing link for me. My brain got stuck on the idea to pass a service to a location. Switching to the new location and then creating the services still feels weird actually as that means losing connectivity. How is this done from remote? – oschrenk Dec 1 '17 at 13:42
  • Use a bash script on the remote so it will continue to execute commands when you get disconnected. I would probably do a PoC locally first, perfect it, then upload/run the script on the remote. You don't what to inadvertently kill access to your machine remotely by disabling a network adapter/service – Allan Dec 1 '17 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .