John Lockwood

Q: 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

Close

Q: Turning on location services via command line

  • All replies
  • Helpful answers

  • by tausifkhan1324,

    tausifkhan1324 tausifkhan1324 Apr 12, 2014 12:24 AM in response to John Lockwood
    Level 1 (0 points)
    Apr 12, 2014 12:24 AM in response to John Lockwood

    THis should work:

    #!/bin/sh

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

    defaults write /System/Library/LaunchDaemons/com.apple.locationd Disabled -bool false

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

     

    exit 0

  • by Upshot IT,

    Upshot IT Upshot IT Nov 6, 2015 9:15 AM in response to John Lockwood
    Level 1 (0 points)
    Nov 6, 2015 9:15 AM in response to John Lockwood

    Hi John,

     

    Thanks for posting this. Do you know if your script will work in Yosemite and El Capitan?

     

    We image our computers with Casper Suite, but location services is off at the end of the imaging process. I want to enable it, post-imaging, with a script such as yours.

  • by John Lockwood,

    John Lockwood John Lockwood Nov 6, 2015 9:53 AM in response to Upshot IT
    Level 6 (9,379 points)
    Servers Enterprise
    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.

  • by Upshot IT,

    Upshot IT Upshot IT Nov 6, 2015 10:18 AM in response to John Lockwood
    Level 1 (0 points)
    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.

  • by John Lockwood,

    John Lockwood John Lockwood Nov 6, 2015 10:25 AM in response to Upshot IT
    Level 6 (9,379 points)
    Servers Enterprise
    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.

  • by Upshot IT,

    Upshot IT Upshot IT Nov 6, 2015 10:49 AM in response to John Lockwood
    Level 1 (0 points)
    Nov 6, 2015 10:49 AM in response to John Lockwood

    These three machines have the "Require an administrator password to access system-wide preferences" checked ON.

  • by prometheon123,

    prometheon123 prometheon123 May 23, 2016 7:52 AM in response to John Lockwood
    Level 1 (9 points)
    May 23, 2016 7:52 AM in response to John Lockwood

    This seem to work on all systems up to 10.11.5:


    #!/bin/sh

    launchctl 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 -int 1
    chown
    -R _locationd:_locationd /var/db/locationd
    launchctl load
    /System/Library/LaunchDaemons/com.apple.locationd.plist

    exit 0