Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Turning on location services via command line

It is possible in Mountain Lion or Mavericks to turn on location services via a shell script as might be used via Apple Remote Desktop or DeployStudio or similar. An example script to do this is listed below.


#!/bin/sh
/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/grep "Hardware UUID" | /usr/bin/cut -c22-57`
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled -int 1
chown -R _locationd:_locationd /var/db/locationd
/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist
exit 0


The script turns location services off, finds the Macs unique hardware UUID number, sets the preference file for this Mac which has a file name unique to this Mac because the file name includes the UUID number, restores ownership of the file as the defaults command will have possibly changed the owner, and then turns location services back on.


As mentioned the above will work for Mountain Lion and Mavericks. Unfortunately Lion even though it also has location services its method of confuguring is totally different.


Does anyone have a similar command line solution to enable location services for Lion (aka OS X 10.7).

Posted on Mar 6, 2014 7:29 AM

Reply
12 replies

May 11, 2017 8:18 AM in response to Bearzooka

For sake of accuracy, the following command is what's being stopped by SIP:

launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist

You could enable (or disable) Location Services and after a reboot the change would take effect; unfortunately, if LocServs is required by the next piece of your process (e.g. setting the time & date), this won't work.

Nov 6, 2015 9:53 AM in response to Upshot IT

I am using basically the same script as part of my DeployStudio workflow and it still works with Yosemite, I have not yet tested it with El Capitan but see no reason it will not continue to work.


There is one issue that I did come across which maybe a problem for you. Apart from simply turning it on which the script does, the way OS X works is that applications needs to also be authorised via the Privacy tab to be allowed to use Location Services. This involves adding them to a plist file at


/var/db/locationd/clients.plist


Unfortunately the structure of this file does not make it modifiable via the standard defaults command. I ended up building a running system, enabling location services, and manually authorising the application(s) I wanted, then copying that pre-configured file during my DeployStudio workflow along with turning on Locations Services as detailed previously.

Nov 6, 2015 10:18 AM in response to John Lockwood

Hi John,


I just tried your script on three test computers (two Yosemites, and one El Capitan), and I can confirm that Location Services checks ON.


On one of my Yosemite machines, I logged in as a Standard user (non-admin), and opened the Notification Area. When I saw the little window that Weather would like to use location services, I clicked OK. I then looked in the Security & Privacy > Privacy pane, and Weather is checked ON.


I'm hoping that if any of my users (who are not admins) launch an app that would like to use the service, that it will also check ON for them.

Nov 6, 2015 10:25 AM in response to Upshot IT

Upshot IT wrote:


Hi John,


I just tried your script on three test computers (two Yosemites, and one El Capitan), and I can confirm that Location Services checks ON.


On one of my Yosemite machines, I logged in as a Standard user (non-admin), and opened the Notification Area. When I saw the little window that Weather would like to use location services, I clicked OK. I then looked in the Security & Privacy > Privacy pane, and Weather is checked ON.


I'm hoping that if any of my users (who are not admins) launch an app that would like to use the service, that it will also check ON for them.

It may depend on whether you have also configured your Mac to require admin access to enable various System Preferences. Also waiting for a user to do the above relies on them remembering to do so and also means until they do so it is not active.


For most cases e.g. the Weather one, this will hardly be critical, when I was testing this I was doing something a lot more sneaky. 👿 As a test I setup a command line service which would allow me to query the physical location of a Mac using location services and then report it to Casper Suite. I don't do this for real live systems but merely to see if it could be done which the answer was yes.

May 23, 2016 7:52 AM in response to John Lockwood

This seem to work on all systems up to 10.11.5:

#!/bin/shlaunchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid
=`/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57`defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled-int1chown -R _locationd:_locationd /var/db/locationd
launchctl load
/System/Library/LaunchDaemons/com.apple.locationd.plistexit0

Dec 9, 2016 12:26 PM in response to John Lockwood

For El Capitan and Sierra, this had to be modified a bit.

It appears the need to specify the UUID is gone.


#!/bin/sh


/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist

/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1

chown -R _locationd:_locationd /var/db/locationd

/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist



exit 0

Turning on location services via command line

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.