Enable Location Services Programmatically - 10.13+

bash
New Contributor II

Is there no way to do this? As far as I'm aware this is being stopped by SIP.

There was this script to enable them but it's no longer working.

#!/bin/bash

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

## Write enabled value to locationd plist
defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1

## Fix Permissions for the locationd folder
chown -R _locationd:_locationd /var/db/locationd

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

exit 0
35 REPLIES 35

PatCMP
New Contributor III

Looking for something that works with SIP enabled in HighSierra, does not look promising though..

gda
Contributor

I simply use this script. No load or unload of the LaunchDaemon is necessary:

#!/bin/bash

# write enabled key
sudo -u _locationd /usr/bin/defaults -currentHost write com.apple.locationd LocationServicesEnabled -int 1

# enable icon in menu bar
/usr/bin/defaults /Library/Preferences/com.apple.locationmenu "ShowSystemServices" -bool YES

exit 0

rqomsiya
Contributor III

Hi @gda.. your script is working except in the "enable icon menu bar" you forgot the write command.. but yes.. this works....

Well i've only tested on 10.13.3. Does it work with 10.12.x?

#!/bin/bash

# write enabled key
sudo -u _locationd /usr/bin/defaults -currentHost write com.apple.locationd LocationServicesEnabled -int 1

# enable icon in menu bar
/usr/bin/defaults write /Library/Preferences/com.apple.locationmenu "ShowSystemServices" -bool YES

exit 0

gda
Contributor

@rqomsiya Thx for fixing it. 😆Being in a WebEx and filtering scripts is not a good combination.

But yes, I use this script since 10.12.3.

rqomsiya
Contributor III

Not a problem! haha..

And awesome.. thanks for verifying its working on 10.12.6. Do you push this out as part of an imaging workflow or part of a login hook?

gda
Contributor

I used it in the imaging workflow. But you can use it as a checkin policy also.

rqomsiya
Contributor III

Awesome.. Thanks for confirming!

loza8454
New Contributor

@rqomsiya When I run the script I get this on enabling the icon on the menu bar

Could not write domain /Library/Preferences/com.apple.locationmenu; exiting

salesloftcb
Release Candidate Programs Tester

@loza8454 Running this with elevated permissions with sudo made this successful for me.

@rqomsiya Thanks for that script! Can confirm it works on 10.12.6 as well as 10.13.6. Saves us a step in the First Run Setup process, now we can hide the option for Location Services!

sateshb
New Contributor III

Getting "Could not write domain com.apple.locationd; exiting" as well.

executed the following: sudo -u _locationd /usr/bin/defaults -currentHost write com.apple.locationd LocationServicesEnabled -int 1

sshort
Valued Contributor

This works for me in High Sierra + Mojave

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

sateshb
New Contributor III

Thank you @sshort, finally got it to work with the line you provided. Confirm the preference can be set on 10.13.6 and 10.14

csa
New Contributor III

@sateshb Can you share what you did to get it to work? I have tried "/usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1" with sudo and sudo -u _locationd with no luck, Still getting 2018-10-27 17:31:33.409 defaults[5798:171447] Could not write domain /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd; exiting

dgreening
Valued Contributor II

Why this is not part of the MDM spec BOGGLES THE MIND.

sateshb
New Contributor III

@csa, not sure why its not working for you, here is a snippet from my script

#!/bin/sh

#Turning on location services
if [[ $locationStatus = 1 ]]; then
    echo "Location Service is turned on"
    else
        echo "Enabling location Service"
        sudo /usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1
        if [[ $locationStatus = 1 ]]; then
            echo "Location Service has been turned on"
        fi
fi

sateshb
New Contributor III

a7e5aeb0cb3144b5874a11cf52a56ffc
I'm going to pose an additional question here as well. I am working with my Jamf Buddy but can anyone share how to enable Set time zone automatically using current location in System PreferencesData & TimeTime Zone?

sshort
Valued Contributor

@sateshb you can use this:

/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

sateshb
New Contributor III

Thanks for getting back to me Steve, I tried that but didn't make a difference. Ran Composer to take a before and after snapshot and don't see the com.app.timezone.auto plist in the list. Can't imagine where this preference is hiding.

fabianhartmann
New Contributor II

Hi, I was able to configure Location Services and configure automatic timezone using the following lines.
A restart is required because restarting the related launchdaemons is no longer possible because of SIP.

enabling location services

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

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

configure automatic timezone

/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeOnlyEnabled -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeZoneEnabled -bool YES

/usr/sbin/systemsetup -setusingnetworktime on
/usr/sbin/systemsetup -gettimezone
/usr/sbin/systemsetup -getnetworktimeserver

tnielsen
Valued Contributor

Hey Fabian, thank you for this. One minor tweak is on your line with uuid=

uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)

added $( )

Works great, thanks!

sdagley
Esteemed Contributor II

My attempt at fixing the formatting on @fabianhartmann's script...

#!/bin/bash

# enabling location services

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

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

# configure automatic timezone

/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeOnlyEnabled -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeZoneEnabled -bool YES

/usr/sbin/systemsetup -setusingnetworktime on
/usr/sbin/systemsetup -gettimezone
/usr/sbin/systemsetup -getnetworktimeserver

CorpIT_eB
Contributor II

@sdagley This worked Flawlessly! Mojave 10.14.5

Thank You!

sdagley
Esteemed Contributor II

@CorpIT_eB Thanks for the confirmation. Credit goes to @fabianhartmann though. I just put his script into a code block to make it readable on the forum.

CorpIT_eB
Contributor II

@fabianhartmann Thanks for the code and @sdagley thanks for adding it to a proper block.

franton
Valued Contributor III

Suggestion: don't use system_profiler command inside a script. It takes a ridiculously long time to work and outputs in a not nice way to parse later. The cut command used is evidence of that. What you can do instead is this:

uuid=$( /usr/sbin/ioreg -d2 -c IOPlatformExpertDevice | awk -F" '/IOPlatformUUID/{print $(NF-1)}' )

This runs in a fraction of the time.

CorpIT_eB
Contributor II

@franton to be clear the only thing that would change would be the variable correct everything else would stay the same.

From:

uuid=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Hardware UUID" | cut -c22-57)

To:

uuid=$( /usr/sbin/ioreg -d2 -c IOPlatformExpertDevice | awk -F" '/IOPlatformUUID/{print $(NF-1)}' )

franton
Valued Contributor III

@CorpIT_eB correct! that'd be the only change. (I just have this thing about not running system_profiler in scripts)

KyleEricson
Valued Contributor II

@sdagley Does this script require a reboot before it works?

Read My Blog: https://www.ericsontech.com

sdagley
Esteemed Contributor II

@kericson I believe it does. I run this script with several others as part of my initial configuration process which ends in a restart to enable FileVault so I haven't tried it without rebooting.

GregE
Contributor

@sateshb would you mind sharing how you read the value for location services? I'm trying to get an Extension Attribute so I can scope a smart group around it, but my scripting isn't good enough to grab that value.

#!/bin/bash
locationservices=(/usr/bin/defaults read /private/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled)

if [[ $locationservices = 1 ]]; then
    result=Enabled
else
    result=Disabled
fi
echo <result>$result</result>

Edit: Got it sorted, I missed the space after the '1' and the ']]'.

beeboo
Contributor

i get a "does not exist" even though location services is enabled and the timezone is set to auto on my machine, albeit manually.
im reading as i dont want to commit and am testing to see if it sees anything before i make changes, just i case i want to make an EA to scope things

C02YJ17VJHD4:~ ME$ /usr/bin/defaults read /private/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled
2020-01-08 12:14:37.504 defaults[88421:6521570] 
The domain/default pair of (/private/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd, LocationServicesEnabled) does not exist

also this

C02YJ17VJHD4:~ ME$ /usr/bin/defaults read /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.2322ECE6-78B2-52A1-8F95-59C31793488A LocationServicesEnabled
2020-01-08 12:16:52.069 defaults[88432:6537060] 
The domain/default pair of (/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd.2322ECE6-78B2-52A1-8F95-59C31793488A, LocationServicesEnabled) does not exist

any suggestions?

EDIT:
it seems to be a permission issue.

tested by giving myself permission to the locationd to test and i got a "1" result.
removed access and it got the error.
ran same command with sudo and i get a result of "1"

will continue to test.

GregE
Contributor

Jamf is running the command as root hence no sudo in anything else above.

bwoods
Valued Contributor

Create a blank package with the script below. Then add it to your prestage enrollment.

#!/bin/bash


## configure ntp server

/bin/cat > /etc/ntp.conf << 'NEW_NTP_CONF'
server time.apple.com
NEW_NTP_CONF

## configure automatic timezone

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

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


## Set date and time automatically
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeOnlyEnabled -bool YES
/usr/bin/defaults write /private/var/db/timed/Library/Preferences/com.apple.timed.plist TMAutomaticTimeZoneEnabled -bool YES
/usr/sbin/systemsetup -setusingnetworktime on
/usr/sbin/systemsetup -gettimezone
/usr/sbin/systemsetup -getnetworktimeserver

### Restart location services daemon (locationd)
/usr/bin/killall locationd

exit 0;     ## Success
exit 1;     ## Failure

KyleEricson
Valued Contributor II

@bwoods Does this require a reboot to apply?

Read My Blog: https://www.ericsontech.com

bwoods
Valued Contributor

@KyleEricson No reboot required, it works immediately. Perfect for DEP/ADE deployments.