macbook air

Setting the Network Time Server from the Command Line

Hi All, here’s a short and sweet blog post to kick off the new year!

I often get asked about setting up NTP configurations on client devices in a better way than manually. You can do this from the command line simply enough:

NTPServer="time.euro.apple.com"
	/usr/sbin/systemsetup -setnetworktimeserver "$NTPServer"
	/usr/sbin/systemsetup -setusingnetworktime on

Swap the “time.euro.apple.com” with your desired NTP server and run the commands as root (using ‘sudo’ in front of them). The first command sets the NTP server address (viewable in the GUI in ‘System Preferences’ under ‘Date & Time’). The second command enables the use of the NTP.

Trick 1: Scripting

As this is a bash command, you can chuck it into a Bash / Shell script. Typically I will put this into a ‘first boot’ script to configure a device during imaging.

Trick 2: AD Domain

Another cool trick is if you’re in an AD domain scenario, you can usually set the domain as the NTP rather than a specific NTP and the client/s will use AD / DNS to find a specific NTP!

E.g. if your NTP servers are ‘time1.example.com’ and ‘time2.example.com’, and your domain is ‘example.com’, set the NTP to the ‘example.com’ address and it should find the NTPs automatically.

Summary

There you go, short and sweet.

As always, if you have any questions, queries or comments, let us know below and I’ll try to respond to and delve into as many as I can.

2 Replies to "Setting the Network Time Server from the Command Line"

  • Jeff Madson

    Darren,

    I am trying to add two additional Time Servers to the drop down list in System Preferences/Date & Time/Set date and time automatically using a Terminal command or Bash script. So far I have only been able to add one additional Time Server by modifying /etc/ntp.conf and that doesn’t even stay. If you go back into the GUI and select one of the other Apple Servers, the nap.conf file changes back to the default.

    Is there anyway to change the list permanently so that the change stays if a Apple Server is selected? Is there anyway to add two servers to the list? Thanks for any help!

    • Darren Wallace

      Hi Jeff,
      You’ll need to use the command line / a script for this to add the items to the ntp.conf file. For example see this. This will have the OS use the first NTP server, unless it can’t be contacted, at which point it will work through any others in the list until one works.

      As for the GUI overwriting the file, this is a common thing through the OS. In this specific case it’s because the GUI can only accept one value. To be honest, if you’re setting the NTP servers manually, then you shouldn’t need to use the GUI at all for this.

      Hope that helps!

      Darren

Leave a Reply

Your email address will not be published. Required fields are marked *