January 6, 2007 · General ·

Sponsored Link
NTP, the Network Time Protocol, is used to keep computer clocks accurate over the Internet, or by following an accurate hardware receiver which interprets GPS, DCF-77, NIST or similar time signals.

ntpdate is a simple NTP client which allows a system's clock to be set to match the time obtained by communicating with one or more servers.

ntpdate is optional (but recommended) if you're running an NTP server, because initially setting the system clock to an almost-correct time will help the NTP server synchronize faster.

The ntpdate client by itself is useful for occasionally setting the time on machines that are not on the net full-time, such as laptops.

Install ntp client in Ubuntu

sudo apt-get install ntpdate

ntpdate will automatically run while bboting your system. Ubuntu stores script at /etc/network/if-up.d/ntpdate location.

If you wish to just run script again just type command

sudo /etc/network/if-up.d/ntpdate

sudo ntpdate pool.ntp.org

If you want to use you own ntp servers you need to edit the /etc/default/ntpdate file using the following command

sudo vi /etc/default/ntpdate

File looks like below

# servers to check. (Separate multiple servers with spaces.)
NTPSERVERS="0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org"
#
# additional options for ntpdate
#NTPOPTIONS="-v"
NTPOPTIONS="-u"
NTPSERVERS="ntp.ubuntu.com"

If you wan to use your own NTP server enter the list of NTP servers under NTPSERVERS

To avoid stepping the clock you must run ntpdate every 1 or 2 hours using cronjob

crontab -e

#Setup NTPDATE

@hourly /etc/network/if-up.d/ntpdate

Save and close the file.

You can also use the following script to run every hour from your cronjob

#!/bin/bash

/usr/sbin/ntpdate -s

/sbin/hwclock --adjust

/sbin/hwclock --systohc

Sponsored Link

6 Comments to “Keeping your System clock current, automatically via Network Time Protocol (NTP)”

  1. Name says:

    Isn’t this a better way of doing it:

    http://www.cs.wcupa.edu/~rkline/Linux/odds-ends.html

  2. Name says:

    “Setting and maintaining the time
    The ntpdate utility can be used to set time to the current time using a clock server. The one I use is clock.psu.edu as follows:
    # ntpdate -u clock.psu.edu

    Theoretically, you should be able to run the ntpd daemon to connect to a server and keep your machine clock correctly synchronized at all times. However, I’ve never been quite successful in finding a server willing to support this.
    Using cron to maintain correct time
    The cron daemon runs services hourly, daily, weekly and monthly. These services are executable script files maintained in these directories:
    /etc/cron.hourly
    /etc/cron.daily
    /etc/cron.weekly
    /etc/cron.monthly

    Here is the file I use: /etc/cron.weekly/setclock.cron
    #!/bin/bash

    clock=clock.psu.edu

    /usr/sbin/ntpdate -u $clock >& /dev/null

    The reason for the >& /dev/null redirection is that you want to make sure that cron scripts send nothing to standard output or standard error. If you want to use this script, make sure it is executable:
    # chmod +x /etc/cron.weekly/setclock.cron”

  3. jj says:

    You can do the above graphically by:
    #1 Selecting System>Administration>Time&Date OR Rt clicking the Time & Date in the Upper Rt corner of the screen OR at the CLI enter “time-admin”
    #2 enter your password
    #3 In the resulting configuration drop-down box select “Keep Synchronized w/Internet Servers” in lieu of “Manual”
    #4 Press the install button to install the alternate /etc/ntp.conf download package.
    #5 Confirm your NTP server by double-clicking the “Select Server” button. On Feisty the Ubuntu server is set by default and should be enough for the desktop.

  4. Richard says:

    COMMENT: On Ubuntu 8.04 the graphical configuration of ntp does not work! Also, ntp is a bit of overkill for what most users need, since it runs continuously in the background, keeping the clock in sync with millisecond accuracy. Hence, remove ntp, in favour of ntpdate, and set things up following the comment of “January 8th, 2007 at 2:50 am” above.

    NOTE: Ubuntu 8.04 seems to have problems with cron, so things may still not work as expected.

  5. Shane Bishop says:

    ntp is still the best solution. It takes into account other factors to keep your clock time stable all the time, whereas ntpdate is accurate only at the time you run it. Most of the time, that doesn’t matter, but if you have a system that gets heavily loaded, or has some other odd factors, your time can really get messed up by the time you run ntpdate again. Besides, ntp uses no resources, and is still easier to setup, even if the gui doesn’t work on 8.04:
    apt-get install ntp
    That’s it, it’s already configured, it gets started automatically when you install it, and it will always be running.
    If you want proof on the resources, check out http://jabber.shanebishop.net/eluna/ … The memory graph doesn’t really tell you much, but it’s currently using 300k of RAM, and serving from 30-100 ntp clients at any given time.

  6. Shane Bishop says:

    Just because I’ve noticed hits on the url above (which no longer works), here’s the proper one:
    http://ntp.shanebishop.net/eluna/
    The ‘server’ is now processing over 200 clients, and still uses virtually no resources on a P3 1.0 Ghz with 368MB RAM.

Leave a Reply

  • Recent comments