Enabling Location Services in Catalina Programatically?

rick_cohen
New Contributor II

I have a number of computers that I manage and previously I've been able to enable Location Services by running the following command in terminal or via a script:

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

With Catalina however, that command doesn't work anymore. Does anyone have any ideas or suggestions on another method of managing this setting?

15 REPLIES 15

dgreening
Valued Contributor II

From the talk at the latest Apple/Jamf Boston meetup, it doesn't sound like this is manageable anymore for privacy reasons. Unknown if management of it will come to supervised T2 devices...

sdagley
Esteemed Contributor II

@rick.cohen It wouldn't surprise me that it'd break under Catalina, but I don't think the example you posted works under Mojave either. Here's what I've been using to modify the ByHost entry specific to the machine:

#!/bin/bash

# enabling location services
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

G_M_
New Contributor III

@sdagley This was on Mojave or Catalina?

Here's what I've been using to modify the ByHost entry specific to the machine:

!/bin/bash

enabling location services

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

sdagley
Esteemed Contributor II

@G.M. That was for Mojave. I think it works for Catalina, but could use independent verification.

P.S. if you're pasting a script into a discussion, please use the code block tag (triple backtick ```) before and after it so the forum software doesn't mangle the display.

rmgmedia
New Contributor III

I just tested this in Catalina and it didn't work for me. Has anybody found a solution that works?

alexduffner
New Contributor III

Under Catalina it seems there are 2 spots where the key "LocationServicesEnabled" is stored. I have experienced that both spots must have the same values to have an effect on the LocationServices under macOS Catalina.
If you read the key out via sudo -u "_locationd" defaults -currentHost read "com.apple.locationd.<UUID>" your result will be:

{
    LocationServicesEnabled = 1;
}

And via sudo defaults read "/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd" you will get following:

{
    "CLSilo.Version" = 1;
    LastSystemVersion = "Mac OS X10.15.3/19D76";
    LocationServicesEnabled = 1;
    ObsoleteDataDeleted = 1;
}

So we have to write to both spots to enable the LocationServices. I have tested it under macOS 10.15 and 10.15.3.

#!/bin/bash
uuid=$("/usr/sbin/system_profiler" SPHardwareDataType | grep "Hardware UUID" | awk '{ print $3 }')
prefDomain="com.apple.locationd.$uuid"
byHostPath="/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd"
# read booleans
ls_enabled_uuid=$(sudo -u "_locationd" defaults -currentHost read "${prefDomain}" LocationServicesEnabled)
ls_enabled_byhost=$(sudo defaults read "${byHostPath}" LocationServicesEnabled)
# process booleans
if [[ $ls_enabled_uuid && $ls_enabled_byhost ]]; then
    echo "Location Services are already enabled."
else
    # set booleans
    sudo -u "_locationd" defaults -currentHost write "${prefDomain}" LocationServicesEnabled -int 1
    sudo defaults write "${byHostPath}" LocationServicesEnabled -int 1
fi

Unfortunately, the change only applies after a restart as long as SIP is enabled.
Otherwise the Launch Daemon could simply be unloaded and reloaded after the change.
Test yourself in Terminal:

$ sudo launchctl unload "/System/Library/LaunchDaemons/com.apple.locationd.plist"
Password:
/System/Library/LaunchDaemons/com.apple.locationd.plist: 
Operation not permitted while System Integrity Protection is engaged

@rmgmedia @rick.cohen Did that help you?

rmgmedia
New Contributor III
So we have to write to both spots to enable the LocationServices. I have tested it under macOS 10.15 and 10.15.3.

!/bin/bash

uuid=$("/usr/sbin/system_profiler" SPHardwareDataType | grep "Hardware UUID" | awk '{ print $3 }') prefDomain="com.apple.locationd.$uuid" byHostPath="/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd"

read booleans

ls_enabled_uuid=$(sudo -u "_locationd" defaults -currentHost read "${prefDomain}" LocationServicesEnabled) ls_enabled_byhost=$(sudo defaults read "${byHostPath}" LocationServicesEnabled)

process booleans

if [[ $ls_enabled_uuid && $ls_enabled_byhost ]]; then echo "Location Services are already enabled." else # set booleans sudo -u "_locationd" defaults -currentHost write "${prefDomain}" LocationServicesEnabled -int 1 sudo defaults write "${byHostPath}" LocationServicesEnabled -int 1 fi Unfortunately, the change only applies after a restart as long as SIP is enabled. Otherwise the Launch Daemon could simply be unloaded and reloaded after the change. Test yourself in Terminal: $ sudo launchctl unload "/System/Library/LaunchDaemons/com.apple.locationd.plist" Password: /System/Library/LaunchDaemons/com.apple.locationd.plist: Operation not permitted while System Integrity Protection is engaged @rmgmedia @rick.cohen Did that help you?

Hi @alex.duffner
I tried running the script you posted above and I saw the following in the logs after running it.

Script result: 2020-03-06 07:01:39.086 defaults[38860:1401723]
The domain/default pair of (/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd, LocationServicesEnabled) does not exist

Any idea what I'm doing wrong?

JSVRone
New Contributor II

This is what I use as a script. I have not had any issues with Catalina but have not tested in about a month.

#!/bin/sh

sudo /usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled -int 1
sudo /usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true

sbabulal
New Contributor

This has been a futile battle for a while now, some alternatives are showing the Location page on Pre-staged enrollment and allow the owner to enable location services and/or prompt the users with a message and open the system preference page. The Plist approach only works on low version operating systems.

alexduffner
New Contributor III

@rmgmedia What do you get if you execute the following?

sudo -u "_locationd" defaults -currentHost read "/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd"

rmgmedia
New Contributor III

@alex.duffner I get this:

{
    "CLSilo.Version" = 1;
    LastSystemVersion = "Mac OS X10.15.3/19D76";
    LocationServicesEnabled = 1;
    ObsoleteDataDeleted = 1;
}

alexduffner
New Contributor III

@rmgmedia So if you get what you posted you should also be able to read the specific key (LocationServicesEnabled) out, via:

sudo -u "_locationd" defaults -currentHost read "/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd" LocationServicesEnabled
# you should get a return of "1" (without the double quotes)

But I believe that the following code will work on most systems, including Catalina:
My detailed script above (#1 post of mine on this thread) is just in case this wont work.

sudo -u "_locationd" defaults -currentHost write "/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd" LocationServicesEnabled -int 1

rmgmedia
New Contributor III

@alex.duffner After seeing the results to the previous command you posted, I decided to try the script again and it seems to be working now. The reboot is required but that is not a problem for me.
Thanks again for your help.

davidi4
Contributor

I just tested @jsuski two command lines in Big Sur and they worked. Did require a reboot.

GabeShack
Valued Contributor III

@jsuski @davidi4 Looks like the first command works to enable location services after a restart, but I'm not getting the 2nd command to enable the timezone based on location to work.
Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools