Posted on 02-10-2020 08:41 AM
Hi all,
I'm having difficulties to force this check through a terminal command. Attached a screenshot.
In the end I want to push it to all computers through JAMF, but I can't seem to find the right command for it.
I found this one in several articles:
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool true
But it seems to be deprecated.
After that I found this one and tried it as well, but it doesn't seem to check the mark.
/usr/sbin/systemsetup -setusingnetworktime on
Therefore I'm wondering if there is an option to force it somehow?
I tried on Mojave and Catalina.
Best Regards and thank you,
Posted on 02-10-2020 01:37 PM
The issue that ive been running into with this is by default it seem that location services are turned off to settings so this function is disabled. Maybe its the way ive setup prestage and DEP, but ive had to log in and do that manually.
Posted on 02-10-2020 02:21 PM
I'm currently using this and seems to be working fine... note WiFi must be active...
#!/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
Posted on 02-11-2020 05:31 AM
Immediately added this as a Self Service policy. Great stuff guys!
Posted on 02-12-2020 12:58 AM
Hi,
I tried it on Catalina and it doesn't work.
Any ideas?
Posted on 02-12-2020 05:17 AM
It works in Catalina. You could post the contents of your autotimezone.sh but the above code works just fine with a script and policy. In my example, I made it into a Self Service item, but you should be able to use other triggers. I have not tested on Mojave yet.
Posted on 04-24-2020 05:17 AM
I can confirm this is no longer working in Catalina. It will do everything except for check the box: Set time zone automatically using current location.
This thread HERE talks about how it hasn't been working since 10.13.
Posted on 04-24-2020 05:32 AM
100% works on 10.15.4. We are on Prem Jamf Pro.
Posted on 04-24-2020 05:44 AM
@Dominic.Jaeckel In Terminal, if you first elevate to root
and then execute your modified version of @larry_barrett's script, do you get different results than using sudo
with your account?
Posted on 04-24-2020 05:47 AM
@larry_barrett I think @tommillard is referencing the set time by Location rather than set network time. That being said, the above script does appear to work but a restart was required for it to be recognized on my test device.
Posted on 04-24-2020 05:56 AM
@smpotter's script does require WIFI to be on. The autotimezone.sh script referenced above doesn't base your time on location, its a static location. Here's the JAMF code he's referencing:
#!/bin/sh
####################################################################################################
#
# Copyright (c) 2010, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# SUPPORT FOR THIS PROGRAM
#
# This program is distributed "as is" by JAMF Software, LLC's Resource Kit team. For more
# information or support for the Resource Kit, please utilize the following resources:
#
# http://list.jamfsoftware.com/mailman/listinfo/resourcekit
#
# http://www.jamfsoftware.com/support/resource-kit
#
# Please reference our SLA for information regarding support of this application:
#
# http://www.jamfsoftware.com/support/resource-kit-sla
#
####################################################################################################
#
# ABOUT THIS PROGRAM
#
# NAME
# setTimeZone.sh -- Set the time zone
#
# SYNOPSIS
# sudo setTimeZone.sh
# sudo setTimeZone.sh <mountPoint> <computerName> <currentUsername> <timeZone>
#
# If the $timeZone parameter is specified (parameter 4), this is the time sone that will be set.
#
# If no parameter is specified for parameter 4, the hardcoded value in the script will be used.
#
# DESCRIPTION
# This script sets the system time zone as reflected in the Date & Time preference pane with the
# System Preferences application. It has been designed to work on Mac OS X 10.3 and higher.
#
# A list of supported time zone entries can be found by running the command:
#
# For Mac OS X 10.5 or later:
#
# /usr/sbin/systemsetup -listtimezones
#
# For Mac OS X 10.4 or earlier:
#
# /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup -listtimezones
#
# The system time zone will be set according to the value specified in the paramter $timeZone.
# It can be used with a hardcoded value in the script, or read in as a parameter. Since the
# Casper Suite defines the first three parameters as (1) Mount Point, (2) Computer Name and
# (3) username, we are using the forth parameter ($4) as the passable parameter. If no parameter
# is passed, then the hardcoded value will be used.
#
####################################################################################################
#
# HISTORY
#
# Version: 1.0
#
# - Created by Nick Amundsen on August 5th, 2008
#
# Version: 2.0
#
# - Updated by Brock Walters October 28 2014
#
####################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
####################################################################################################
#
# A HARDCODED VALUE FOR "timeZone" CAN BE SET BELOW.
#
# A list of accepted time zone values can be generated using the following command in Terminal:
#
# sudo systemsetup -listtimezones
#
# Delete the double quotes and replace with the desired time zone name, e.g. timeZone=Pacific/Honolulu
# If this script is to be deployed via policy using the JSS leave the next line as is.
#
####################################################################################################
timeZone=""
####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "timeZone"
if [ "$4" != "" ] && [ "$timeZone" == "" ]
then
timeZone=$4
fi
osx=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion)
maj=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,1,2)}')
ref=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,4,2)}')
if [ $maj -gt 10 ]
then
echo
echo "Check OS string format & OS X systemsetup utility for script compatibility with OS X version $osx"
echo
exit
fi
if [ "$timeZone" != "" ]
then
if [ $ref -lt 5 ]
then
echo
echo "Setting time zone for OS X $osx..."
/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup -settimezone "$timeZone"
/usr/bin/killall SystemUIServer
echo "Refreshing the clock in the Menu Bar..."
echo
else
echo
echo "Setting time zone for OS X $osx..."
/usr/sbin/systemsetup -settimezone "$timeZone"
/usr/bin/killall SystemUIServer
echo "Refreshing the clock in the Menu Bar..."
echo
fi
else
echo
echo "Error: The timeZone variable is not populated. Press the return key "
echo "to generate a list of valid time zones. Select & copy the desired "
echo "time zone from the list & paste into the script on the line reading "
echo
echo " timeZone="" "
echo
read -p ""
/usr/sbin/systemsetup listtimezones
exit
fi
Posted on 04-24-2020 10:47 AM
@andrew.nicholas is correct. I was speaking to the checkbox in the next one over. @larry_barrett your video shows what IS working, but the checkbox for this screenshot isn't working, which did before Catalina.
Posted on 04-24-2020 11:14 AM
I'm probably not adding anything here but a data point, however I just tested @smpotter 's script via Self Service on a non-T2 chip MacBook Pro running macOS 10.15.4 and it worked fine (after reboot). Perhaps the T2 security chip is causing different behavior? I don't have a T2 Mac to test this on right now.
Posted on 04-24-2020 11:26 AM
@macbrun I just tried running it and that checkbox does indeed become checked after a reboot. Thanks :) This is on a T2 mac.
Posted on 04-24-2020 12:15 PM
I am not on a T2 mac :)
Posted on 04-29-2020 03:59 AM
Hi,
sorry that I didn't reply anymore, but the script is working you just have to reboot after execution and the box is checked.
Works on Mojave and Catalina.
Best Regards,
Dominic
Posted on 06-15-2020 08:45 AM
Hello all, I'm trying to make this an "Available offline" self service item to help with some of my older MacBook Pros lose battery power totally and the time and date reset on them to like 2010, and wrong time etc. I tested it on a system and set the date back a year and it worked however when I set it to 2010, and tired to run it from self service which was no longer connecting to the JAMF server it fails. how does the make available offline checkbox work? I thought it would cache the policy locally
Posted on 06-15-2020 08:30 PM
I used the following script tied to a launch dameon that ran every 10 mins to help with the same issue you are having. It is based on a script by @mm2270. If it can reach your ntp server, it does nothing. If it can't it will default to apple to set time. It helped alot with Macbooks that were left in drawers or off network fro a bit. we saw an added benefit for users that travel in that when the time zone changed, Outlook prompted them to update their Calendars and meetings
launchd created using launchd
#!/bin/sh
#######################################################################################################################
# #
# This script is activated by a launchagent to set the correct time on an Apple device #
# Based on NetworktimeSwitcher Script by mm2270 adapated for macOS Mojave #
# #
#######################################################################################################################
ping -c 5 ntp.example.com > /dev/null
casTest=$?
if [[ $casTest == 0 ]]; then
ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
if [[ $ntpServer != "ntp.example.com" ]]; then
/usr/sbin/systemsetup -setusingnetworktime off
sudo sntp -sS ntp.example.com
/usr/sbin/systemsetup -setusingnetworktime on
else
exit 0
fi
else
ping -c 5 ntp1.example.com > /dev/null
casTest=$?
if [[ $casTest == 0 ]]; then
ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
if [[ $ntpServer != "ntp1.example.com" ]]; then
/usr/sbin/systemsetup -setusingnetworktime off
sudo sntp -sS ntp1.example.com
/usr/sbin/systemsetup -setusingnetworktime on
else
exit 0
fi
else
ntpServer=`/usr/sbin/systemsetup -getnetworktimeserver | awk '{print $4}'`
if [[ $ntpServer != "time.apple.com" ]]; then
/usr/sbin/systemsetup -setusingnetworktime off
sudo sntp -sS time.apple.com
exit 0
fi
fi
fi
Posted on 05-18-2022 05:58 PM
The example scripts here were no longer successfully enabling "set time zone automatically using current location setting" in my testing on latest macOS Monterey (not sure when these broke). Issue is that if you run `sudo defaults write` on preferences in `/private/var/db/timed` , macOS won't honor the settings since doing so will change the owner to root:wheel. Fix is to change ownership to _timed:_timed:
timedPrefs="/private/var/db/timed/Library/Preferences/com.apple.timed.plist"
dateTimePrefs="/private/var/db/timed/Library/Preferences/com.apple.preferences.datetime.plist"
# Set preferences.
/usr/bin/defaults write "$timedPrefs" TMAutomaticTimeZoneEnabled -bool TRUE
/usr/bin/defaults write "$dateTimePrefs" timezoneset -bool TRUE
/usr/sbin/chown "_timed:_timed" "$timedPrefs" "$dateTimePrefs"
As a bonus, the setting appears to be loaded automatically after running this script, and a restart is no longer required.
Posted on 05-19-2022 11:09 AM
The timing of your post is quite serendipitous, @mario
However, I ended up having to use `systemsetup` instead.
timedPrefs="/private/var/db/timed/Library/Preferences/com.apple.timed.plist"
defaults read $timedPrefs
{
NtpUseServicePort = 0;
TMAutomaticTimeOnlyEnabled = 1;
TMAutomaticTimeZoneEnabled = 1;
}
dateTimePrefs="/private/var/db/timed/Library/Preferences/com.apple.preferences.datetime.plist"
defaults read $dateTimePrefs
{
timezoneset = 1;
}
ls -lah $timedPrefs
-rw------- 1 _timed _timed 135B May 19 10:25 /private/var/db/timed/Library/Preferences/com.apple.timed.plist
ls -lah $dateTimePrefs
-rw------- 1 _timed _timed 59B Apr 18 10:43 /private/var/db/timed/Library/Preferences/com.apple.preferences.datetime.plist
defaults delete $timedPrefs TMAutomaticTimeZoneEnabled
defaults read $timedPrefs
{
NtpUseServicePort = 0;
TMAutomaticTimeOnlyEnabled = 1;
}
ls -lah $timedPrefs
-rw------- 1 root wheel 101B May 19 10:39 /private/var/db/timed/Library/Preferences/com.apple.timed.plist
/usr/sbin/chown "_timed:_timed" "$timedPrefs" "$dateTimePrefs"
ls -lah $timedPrefs
-rw------- 1 _timed _timed 101B May 19 10:39 /private/var/db/timed/Library/Preferences/com.apple.timed.plist
After all of the above, the time was still incorrect (i.e., the Mac had been in PDT and was back in MDT, but still showed as PDT).
systemsetup -gettimezone
Time Zone: America/Los_Angeles
systemsetup -settimezone 'America/Denver'
2022-05-19 17:43:43.895 systemsetup[10464:153418] ### Error:-99 File:/AppleInternal/Library/BuildRoots/66382bca-8bca-11ec-aade-6613bcf0e2ee/Library/Caches/com.apple.xbs/Sources/Admin/InternetServices.m Line:379
Set TimeZone: America/Denver
systemsetup -gettimezone
Time Zone: America/Denver
date
Thu May 19 11:43:59 MDT 2022
With the error, I was surprised this actually worked (and persisted after a reboot).
The Mac is running macOS `12.3.1` and if I observed the error in `12.4`, I would have filed feedback with Apple.