Home > Bash scripting, Mac administration, Mac OS X > Setting multiple network time servers from the command line

Setting multiple network time servers from the command line

I recently had the realization (spurred by a co-worker who noticed the problem) that I was Doing Something Wrong and in fact had been doing it wrong for over a year. The problem had to do with how my first boot script was setting the multiple network time servers in use on our work network. I had been setting it like this:


#!/bin/sh

#Primary Time server for Company Macs
TimeServer1=ns0.time.server
#Secondary Time server for Company Macs
TimeServer2=ns1.time.server

/usr/sbin/systemsetup -setnetworktimeserver $TimeServer1,$TimeServer2

I’d thought this would tell the Macs to use both network time server addresses. Instead, the Macs were actually seeing it as one server with the address of ns0.time.server,ns1.time.server. Naturally, this address didn’t resolve to squat, which meant that the Macs weren’t getting the correct time.

Fortunately, there’s a number of ways to fix this. See below the jump for three of them.

The first and probably most straightforward way is to configure the /etc/ntp.conf file (which stores the network time server addresses) and then push that file out to your machines. Greg Neagle has a good write-up on how this works.

You can also set the server settings with a script. One way to do this is the following:


#!/bin/sh

#Primary Time server for Company Macs
TimeServer1=ns0.time.server
#Secondary Time server for Company Macs
TimeServer2=ns1.time.server
#Tertiary Time Server for Company Macs, used outside of Company network
TimeServer3=time.apple.com

# Set the primary network server with systemsetup -setnetworktimeserver
# Using this command will clear /etc/ntp.conf of existing entries and
# add the primary time server as the first line.
/usr/sbin/systemsetup -setnetworktimeserver $TimeServer1

# Add the secondary time server as the second line in /etc/ntp.conf
echo "server $TimeServer2" >> /etc/ntp.conf

# Add the tertiary time server as the third line in /etc/ntp.conf
echo "server $TimeServer3" >> /etc/ntp.conf


Another way to do this (hat tip to Peter Bukowinski) is to let cat feed your time server settings into /etc/ntp.conf:

#!/bin/sh

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


In this example, though the magic of redirects, cat is feeding everything that is entered before the NEW_NTP_CONF marker into /etc/ntp.conf.

  1. Sean Alexander
    January 31, 2013 at 10:18 pm

    Rich,
    Which option do you use?

    If you use the script option, are the time servers separated by spaces, commas, or both?

    When I use the script option, the time servers are separated by a space and a comma (not a single space). From Greg’s example and my research with Apple, I am led to believe that the time servers need to be separated by a space in order for it all to work.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 159 other followers

%d bloggers like this: