Enabling Location Services in Mountain Lion image

bloree
New Contributor II

Enabling Location Services in our Mountain Lion image has been driving me crazy. After some googling and playing with fseventer, I was able to throw together the following script that works in our image workflow at reboot.

#!/bin/bash

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
21 REPLIES 21

quedayone
Contributor

Sweet testing this now!

Lotusshaney
Contributor II

Nice, just need a way of adding an item into the Allowed Apps List

antoinekinch
New Contributor III

I am testing this now but an already getting the following error: Could not write domain /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.XXXXX.

Any luck with this?

mm2270
Legendary Contributor III

@ bajankinch, is the script running from a Casper policy or otherwise being run with root privileges? The "locationd" directory and everything underneath it in /var/bd/ is locked so unless it runs as root it will never be able to write to that location.

antoinekinch
New Contributor III

I tried running the commands with sudo, I am going to test deploying the script through Casper Remote and at reboot at the time of imaging today.

gokoudes
New Contributor III

Cheers for this, man - works great!

ronb
New Contributor II

I'm missing something. I tried this as a login script, no luck. I tried it in Self Service, got this error from the policy -
Running script EnableLocationServices.sh...
Script exit code: 0
Script result: usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U user name] [-u
user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
name|#uid] file ...

This is the script command -
#!/usr/bin/env bash

#enables Location Services
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
sudo uuid='/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57'
sudo defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.$uuid LocationServicesEnabled -int 1
sudo chown -R _locationd:_locationd /var/db/locationd
sudo launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

I've been having other problems with running "first run" scripts since moving to 10.8.4. Any ideas?

Kumarasinghe
Valued Contributor

please copy the script from the very first post and use it.
You don't need "sudo" when you runs scripts with Casper as Casper run them as root.

ronb
New Contributor II

Got this running for Mac OS 10.8, but no luck in 10.9.1. Anyone else having problems with the latest OS?

Kumarasinghe
Valued Contributor

You have to have an additional line to get it work with 10.9
Use this;

#!/bin/sh

# enable location services
/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist
uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd."$uuid" LocationServicesEnabled -int 1
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.notbackedup."$uuid" LocationServicesEnabled -int 1
/usr/sbin/chown -R _locationd:_locationd /var/db/locationd
/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

armando
New Contributor III
You have to have an additional line to get it work with 10.9 Use this; #!/bin/sh # enable location services /bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57) /usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd."$uuid" LocationServicesEnabled -int 1 /usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.notbackedup."$uuid" LocationServicesEnabled -int 1 /usr/sbin/chown -R _locationd:_locationd /var/db/locationd /bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

Can I use this for both 10.8 and 10.9?

Kumarasinghe
Valued Contributor

@armando][/url
Yes. Works on both OS X 10.8 and 10.9

geoffreykobrien
Contributor

I couldn't get this to work on 10.9.4, anyone out there enabling this with a never booted image?

tkimpton
Valued Contributor II

Anyone know how to do this in python?

tkimpton
Valued Contributor II

[edited] working in 10.9.4 :)

tkimpton
Valued Contributor II

I've used this to set the system time :)

#!/bin/bash

########################## SET SYSTEM TIME  ##################################################
#
# Written by Tim Kimpton
#
# using information from https://jamfnation.jamfsoftware.com/discussion.html?id=5336
#
# If the machine is 5 minutes out of the kdc the machine will not bind to the domain.
#
# This script does the folling to ensure the time is correct
#
# 1. Unload the launch daemon used for location services
#
# 2. Get the hardware UUID of the machine and put it in the location services db
#
# 3. Enable location services
#
# 4. Correct permissions on the database file used for location services
#
# 5. Set the time zone to update the time automatically
#
# 6. Set the network time to on
#
# For information see https://jamfnation.jamfsoftware.com/discussion.html?id=5336
###############################################################################################

######################### ENVIRONMENT VARIABLES #######################

# Get the Hardware UUID from system profiler
uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)

####################### DO NOT MODIFY BELOW THIS LINE #################

# Unload the launch daemon
/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.locationd.plist

# Write the UUID to the hidden plist file and initialise it
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd."$uuid" LocationServicesEnabled -int 1

# Enable Location Services
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.notbackedup."$uuid" LocationServicesEnabled -int 1

# Make sure the permissions on the database file is correct
/usr/sbin/chown -R _locationd:_locationd /var/db/locationd
/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

# Set time zone to update automatically
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

# Set network time to on
/usr/sbin/systemsetup -setusingnetworktime on > /dev/null 2>&1

exit 0

bmgsupport
New Contributor II

For me it seems the script doesnt work on OS 10.12.1
Does anyone have tested it on Sierra with any luck?

quedayone
Contributor

Yup not working on 10.12 for me as well.

sean
Valued Contributor

Try this:

#!/bin/bash

mac_uuid=$(defaults read /Library/Preferences/com.apple.apsd.plist MachineUniqueIdentifier)
ld_plist=/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.${mac_uuid}.plist
plist_buddy="/usr/libexec/PlistBuddy"


location_status=$(${plist_buddy} -c 'print :LocationServicesEnabled ' "$ld_plist" 2>/dev/null)

case $location_status in

        0)
                $plist_buddy -c 'set :LocationServicesEnabled 1' "$ld_plist"
                ;;
        1)
                echo "Nothing to do"
                ;;
        *)
                $plist_buddy -c 'add :LocationServicesEnabled integer 1' "$ld_plist"
                ;;
esac

launchctl kickstart -k system/com.apple.locationd

exit 0

Kumarasinghe
Valued Contributor

macOS 10.12 needed a little change with the script.
Working script:

#!/bin/sh

# Enable location services
/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
/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.notbackedup LocationServicesEnabled -int 1
/usr/sbin/chown -R _locationd:_locationd /var/db/locationd
/bin/launchctl load /System/Library/LaunchDaemons/com.apple.locationd.plist

rmanly
Contributor III

Been a long time since I've been here but I just wanted to say thanks for sharing!

Didn't have this issue in org but a buddy just bought a new MacBook Pro and it had this oob. Script worked great!

:D