# Turn on/off location services on Mac using scripts

Location Services enable several macOS applications and services to gather and use information based on the current location of the Mac, which in turn enhances the user experience. There are certain apps like Maps, for instance, which require the current location of the device to function effectively. This information is collected and provided by Location Services. However, by enabling Location Services the possibility for security threats such as tracking online behavior, theft of identity and lack of privacy in general increases. It is the IT admin’s discretion whether or not to enable/disable Location Services on their fleet of devices.

Location Services can be enabled/disabled for the Mac altogether and if enabled, it can be enabled/disabled for each app/service individually. In this document, we provide scripts which IT admins can use to remotely enable/disable Location Services for macOS devices using Hexnode UEM’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action.

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

How to enable Location Services on Mac?
---------------------------------------

Script to enable location services on macOS devices

sudo /usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled –bool true 

   1

2

3

  sudo /usr/bin/defaults write

/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd 

LocationServicesEnabled –bool true 

   

 

  

Execute this command and restart the device to enable Location Services.

### What happens at the device end?

![Location services enabled at the device end under Security & Privacy preferences](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/03/Location-services-enabled-at-device-end.png "Location services enabled at device end")

On [restarting](https://www.hexnode.com/mobile-device-management/help/restart-a-device-using-hexnode-mdm/) the device, Location Services will be enabled. It will not affect the existing location services setting of individual applications and services.
That is, the apps/services whose checkboxes had previously been marked will be allowed access. Further changes can be made by the admin user at the device end.

How to disable Location Services on Mac?
----------------------------------------

Script to disable location services on macOS devices

sudo /usr/bin/defaults write /var/db/locationd/Library/Preferences/ByHost/com.apple.locationd LocationServicesEnabled –bool false

   1

2

3

  sudo /usr/bin/defaults write

/var/db/locationd/Library/Preferences/ByHost/com.apple.locationd 

LocationServicesEnabled –bool false

   

 

  

Execute this command and restart the device to disable Location Services.

### What happens at the device end?

![Location services disabled at the device end under Security & Privacy preferences](https:2023/03/Location-services-disabled-at-device-end-.png "Location services disabled at device end")

On [restarting](https://www.hexnode.com/mobile-device-management/help/restart-a-device-using-hexnode-mdm/) the device, Location Services will be disabled. None of the apps/services will be given access to the service.

 Notes:- The scripts given above are OS version dependent.
- The above scripts can only enable/disable Location Services. They cannot be used to specify the apps/services that should be given access to the location services.

 

How to view the current status of Location Services?
----------------------------------------------------

Script to check location service status

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

   1

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

   

 

  

Execute this command to check whether Location Services is currently enabled or not. The output can be viewed under the Action History tab of the device.

![enabling location services via execute custom script action](https:2023/03/output-of-script-to-check-if-location-services-is-enabled.png "output of script to check if location services is enabled")

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.