Installing and Configuring NTP on Linux - gHacks Tech News

Installing and Configuring NTP on Linux

NTP is the Network Time Protocol. This protocol allows servers (or desktops) to communicate to very reliable sources to keep their time synchronized. There are two reasons why you would want to employ NTP: 1) You're a geek and you want your time to be perfect. 2) You need your servers all synchronized with the correct time.

Naturally the second reason is far more important than the first. But either way, you might very well be driven to keep your machine on the exact time. To that end you can either constantly monitor the time on your machine (manually changing it when necessary) or you can set up NTP to handle your time corrections for you.

Installing and configuring NTP is actually quite easy. It will, however, require the use of the command line. With that in mind, lets' get busy.

Installing

You could open up your Add/Remove Software utility, do a search for "ntp" (no quotes) and install the matching results. Or you can open up a terminal window and install ntp with one of the following commands (remember, you will need either root access or sudo):

  • apt-get install ntp
  • yum install ntp
  • urpmi ntp

One of the above commands will install the ntp daemon and the configuration file /etc/ntp.conf.

Before you fire up the daemon you will need to take a look at the /etc/ntp.conf to make sure you have the ntp servers you want to use configured.

Configuring

Most likely your NTP installation will already have a configuration file that is ready to go. My Debian-based NTP install had such a .conf file. All I had to do was start the daemon. But you might have special needs or your install might not have been as complete.

The section of the ntp.conf that you will want to take a look at is the server section. Each line in this section is set up like so:

server IP_ADDRESS OPTION(S)

Where IP_ADDRESS is the actual address of the server you want to use and OPTION(S) is/are the option(s) you want to use.

There are two options that you might want to use for every server. These are:

iburst: This option is used when the configured server is unreachable. When your machine can not contact its NTP server it will send out bursts of eight packets (instead of just one).

dynamic: This option allows a server to be configured even if the server is not reachable during configuration. This option assumes that at some point the server will be reachable.

If you look at my /etc/ntp.conf file you will see the following in the server section:

server 0.debian.pool.ntp.org iburst dynamic
server 1.debian.pool.ntp.org iburst dynamic
server 2.debian.pool.ntp.org iburst dynamic
server 3.debian.pool.ntp.org iburst dynamic

If you are not sure what servers to use visit the official NTP organization NTP.org to find a list of trusted servers.

Starting the Daemon

Once you have everything up and running go back to the root terminal and issue the command:

/etc/init.d/ntp start

or

/etc/rc.d/init.d/ntp start

which will start the ntp daemon. NTP will now slowly start to adjust the time on your server. But don't worry, it makes the adjustment slowly. First the daemon waits for at least ten packets of information before trusting a source.

Now test to make sure your installation is working by issuing the following command:

ntpq -p

which should give you a listing like:
remote refid st t when poll reach delay offset jitter =========================================================
+point2.adamants 64.202.112.75 2 u 44 64 377 75.955 -7.045 2.992
*station.mars.or 209.81.9.7 2 u 10 64 377 75.477 -1.144 0.977
-www.broadbandja 64.34.180.101 3 u 56 64 377 72.764 3.766 0.977
+pxe.lax-noc.com 209.81.9.7 2 u 25 64 377 65.686 -7.753 2.539

If you see zeros for the values you know ntp is not connecting. As you can see above my setup is connecting.

Final thoughts

NTP is an easy way to ensure your servers or your desktop is always using the correct time. In mission-critical machines, this can be quite important. Employing NTP will give you the security of knowing your machine's right "on time".

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. Paul(us). said on March 17, 2009 at 1:37 pm
    Reply

    I realy liking this, christal clear, Jack explenation.

  2. Peter said on April 1, 2011 at 11:47 am
    Reply

    Hello,

    I am a newbie on linux. I am trying to fix this for 4 days but still unable to sync with the Internet time server.
    I am trying to do synchronize to the internet time server using my internal server (running on redhat) but it does not work at all.
    After configuring the ntp.conf, i stop the ntpd and start again and still did not work. i able to update the Internet time server using ntpdate commad.

    Please comment on my configuration and show me show direction. :(

    [[email protected] ~]# ntpq -p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    LOCAL(0) .LOCL. 10 l 8 64 1 0.000 0.000 0.001

    [[email protected] ~]# /etc/init.d/ntpd stop
    Shutting down ntpd: [ OK ]

    Configuration file:

    Running on:
    [[email protected] /]# /bin/bash –version
    GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
    Copyright (C) 2005 Free Software Foundation, Inc.

    I am able to update using ntpdate:
    [[email protected] /]# ntpdate sg.pool.ntp.org
    1 Apr 16:42:22 ntpdate[4812]: adjust time server 202.150.213.154 offset 0.036360 sec

    Configuration file on /etc/ntp.conf

    restrict default ignore
    restrict 127.0.0.1

    #allow synchronization sources from the internet servers
    restrict server 3.sg.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict server 0.asia.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery
    restrict server 2.asia.pool.ntp.org mask 255.255.255.255 nomodify notrap noquery

    #allow our internal server to synch to this server
    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap noquery notrust

    server 127.127.1.0 #local system clock
    fudge 127.127.1.0 stratum 10
    driftfile /etc/ntp/drift
    logfile /var/log/ntp.log

  3. Bikash Gyawali said on February 5, 2012 at 5:44 pm
    Reply

    After issuing the command /etc/init.d/ntp start does the ntp server keep running even after the restart? If not, how to make the service run at startup automatically?

    1. LHelms said on January 24, 2014 at 3:07 am
      Reply

      To Insure that ntpd starts AFTER a reboot:

      $ chkconfig –add ntpd
      $ chkconfig ntpd on

      You can verify whether it will or not with:

      $ find /etc/rc*.d -name ‘*ntp*’

  4. felipe prado said on September 14, 2012 at 5:48 am
    Reply

    this works perfectly.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.