Adam SMASH!

Setting up Ubuntu to automatically update your PC’s time

by Adam on Oct.12, 2009, under Linux, Ubuntu 9.04

In windows, you can use the w32time service to automatically sync your PC’s clock to an atomic clock somewhere on the planet. Well Ubuntu linux has that capability as well. The command is ntpdate, and it comes installed in Ubuntu 9.04 by default.

As a caveat, there may be a way to do this in gnome and/or KDE, but I’m all about the commandline and learning how linux works at the lowest level, so that’s what I’m going to teach you.

As I said the command is called ntpdate. The command to sync your time is fairly simple:

ntpdate -b pool.ntp.org

That’s all there is to it. As long as your PC can get out on port 123, your pc will sync the time with an atomic clock and tell you how off it was:

12 Oct 07:42:47 ntpdate[11438]: step time server 209.123.234.24 offset -0.429255 sec

This is nice and all, and our time is synced for now, but we want to work out how to make this update every day automatically, with no further input from you. For this we will need CRON.

Cron is a scheduler in linux, and ubuntu comes with vixie-cron, which from what I can tell is pretty much the standard. At any rate I think they all work mostly the same, but if you’re using Ubuntu 9.04, chances are you’ve got vixie-cron and this is exactly what you need to do.

First we want to create a script that will run this command. For most of this you’ll need to be root, or at the very least use sudo.

sudo nano /etc/cron.daily/timeupdate

This will start up nano and start editing a file called timeupdate in the /etc/cron.daily/ folder. In that, we want to enter the following text:

#!/bin/bash
ntpdate -b pool.ntp.org >> /var/log/ntptime.log 2>&1

The first line is just to tell linux which program interpreter to use. In this case we’re just using a bash command we can enter right on the bash terminal line, so /bin/bash will work. Later on if you get into writing scripts in other languages, say python for instance, you will change that line to #!/usr/bin/python. The meat of the script is almost exactly what we put on the command line to get our time updated, but now I’ve added a bit to create a log in the /var/log/ directory, so we know that this is happening. the >> part tells bash to redirect the output of a command to some other location. You could redirect it directly to a printer if you wanted, but in this case, we’re telling it to place the output in a file called /var/log/ntptime.log. the 2>&1 part tells bash to redirect error output to the same place. Otherwise cron throws away the errors, because they get output to STDERR, which is the screen. Since you won’t have access to cron’s STDERR when it’s running, we want it to log the errors too.

Ok once we save the file, we’ll need to make the script executable. In its current state it won’t execute.

sudo chmod +x /etc/cron.daily/timeupdate

This adds the X flag to the file, making it executable.

This is basically all you need to do. Now when your PC runs its cron.daily jobs (which for me is about 7:30am) it will also execute this step, updating your PC’s time every single day.

If you want to test out the script and make sure it is all working, you can simply execute the script. If you are in teh same directory as the script, type sudo ./timeupdate, if you are elsewhere in the system, call it by it’s full pathname. And then check the log that the script is supposed to make. If all is well, it will have an entry in there telling you that it corrected your PC’s time.

SIDENOTE:
You can place any script you want to run on a daily basis in the /etc/cron.daily directory and it will run with the rest of them. Pretty handy!

EDIT: Apparently I reinvented the wheel here, you can just install a daemon called ntpd and it’ll do wha tI just showed you. At any rate, it’s nice to have alternate ways to do stuff I guess. :)


1 Comment for this entry

  • Noriko Burtenshaw

    Nice article. I got to your website via google while i was looking for web space. I will turn over your site to other people and I am sure they will think the same about your efforts on this site.Best

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...