Emidio Planamente 's Homepage

Home GNU/Linux Programming Technology Sport Others Contact

Search

  Google linux


Debian

  Apt-get
  Debian Anatomy
  Debian Backports
  Debian Help
  Debian Planet
  Debian Swiss
  History of Debian
  Getdeb
  Kernel
  Kernel 2.6
  Packages
  Refcard for newbie
  Reference book
  SATA raid
  Weekly News


Official Docs

  Distrowatch
  Firewire
  Gimp
  Gimp Photo Archive
  Linuxdoc
  Linuxdoc Reference
  Linux Focus
  Linux From Scratch
  Linux Hacks
  Linux Hardware
  Linux Printing
  MySQL
  O'Reilly
  Rute
  Source Forge
  USB guide


Installation

  Instalinux
  Preseed


Laptop

  Linux on Laptops
  Linux Toshiba
  Tux Mobil


Live-CDs

  Create it Your self
  Knoppix
  Kororaa XGL 3


Pictures

  Bay13
  Gnomelook
  Kuznetsov


Security

  GNU/Linux
  PortsDB


Linux based product

  Dreambox


Free web service

  S5 presents


Against Micro$oft

  Combatti Micro$oft
  Microsuck


HTML validator CSS validator

 

NTP howto


1. NTP Introduction

2. NTP client / server introduction

3. Showing the time

4. Setting the time manually

5. Setting the time automatically

6. Maintening the NTP server

7. Installing NTP tools

8. Related documents

9. About


1. NTP Introduction


1.1. Scope


This howto briefly describes the main possibilities to keep your system time up-to-date using NTP.


1.2. NTP


The Network Time Protocol (NTP), has been created to transport time information, so that hosts can easily synchronize their clocks to thrusted time servers.


It has a high resolution: about 2^-32 seconds, which correpons to less than 1 ns.


Before proceeding, it is better to clarify some important points.


1.3. Hardware clock


This is the time provided by the on board RTC.


When your PC is turned off, there is a chip on the motherboard that still provides the time. It is powered by a little battery and still works if you unplug the power cabel.


1.4. Software clock


This is the time provided by the kernel, also known as system clock.


During the boot, it reads the correct time from the hardware clock and while the system has beeing shutting down it writes the up-to-date time to the hardware clock.


1.5. GMT


The Greenwich Meridian Time (GMT) is the time at the Royal Observatory in Greenwich (London).


It is based on the heart rotation around the sun, which introduces a little error every year.


In the past, it was the main reference for a world time, but due to this drift error, it has been replaced by UTC.


1.6. UTC


The Universal Time Coordinated (UTC) is an atomic time scale used to indicate a time valid in the whole world at the same moment. In fact, it is a world's standard time.


It is called "coordinated" because there are several institutions that estimate the current time and UTC is the combination of these.


It is not subject to Daylight Saving Time or other changes.


Setting the hardware clock to UTC is highly recommended. You can change this behaviour setting the variable


  UTC


in the file


  /etc/default/rcS


and rebooting the system.


Using UTC your system clock is calculated based on your timezone, which is stored in the file


  /etc/timezone


and automatically changed to Daylight Saving Time and back to normal time on the correct moment.


You can change your timezone by calling


  tzconfig


1.7. SNTP


The Simple Network Time Protocol (SNTP) is an easier implementation of the NTP.


It misses some internal algorithms that are not needed for some kind of server.


1.8. XNTP


XNTP was an experimental version of NTP, which has been dropped in NTPv4.


2. NTP client / server introduction


2.1. How to they work


Both NTP client and NTP server ask for the time to a remote NTP server and automatically update their system clock (inclusive the hardware clock!). There is just a difference about how they do that.


Optionally, a NTP daemon, once synchronized, is able to provide the time to other clients. These clients can be either NTP clients or NTP servers.


2.2. Sync possibilities


Basically, there are two ways to keep your system clock up-to-date using NTP:


- using a client

- using a server


The best solution is to install a server, also if you will NOT provide time information to other hosts!


To understand why, please read the following chapters.


2.3. NTP client


This solution does not use a daemon on your host. There are many tool to accomplish this task. Some of theme are:


- ntpdate

- rdate

- ...


The principle is simple: somebody has to call one of these clients in order to synchronize the clock. This can be done either manually (by command line) or with a cron job, which regularly calls them.


Also if you have a laptop, I suggest you to install a NTP server, which has more advantages (see next chapter).


2.4. NTP server


This solution uses a daemon on your host.


The daemon has a lot of advantages (see next chapters).


2.4.1. No any sudden change


If the daemon has to correct the system clock, it does it slowly. This because there could be some service or application which looks for the time to accomplish some task and if the system clock changes suddenly they could have unexpected behaviour.


One problem was for example with the cronjob. If the system clock was suddenly changed from 2000-01-01 to 2007-05-28, it had to call 7 years of cron jobs, which could be fatal for the host!

This problem has been fixed in the cronjob, but could be present in other services or applications.


Therefore, using a daemon the risk of unexpected behaviour of the system is reduced.


2.4.2. Permanently in sync


The daemon, at the contrary of a cron job, can spontaneously ask for the current time.


Therefore, using a daemon the time will be permanently in sync.


2.4.3. Smart correction


Each hardware clock has a different drift error. The daemon tries to calculate this drift and in case of no connection with remote time servers it can estimate the current time considering this drift error.


Therefore, using a daemon the time will be more accurate in case of connection lost.


3. Showing the time


3.1. Command "date"


3.1.1. Installing


This command is provided by the Debian package coreutils. Therefore, to install it run


  apt-get update

  apt-get install coreutils


3.1.2. Showing local time


This command can be used to display the current system time.


To use it, simply call


  date


It returns something like


  Mon May 28 11:01:01 CEST 2007


CEST means, my system time is set to Central Europe.


3.1.3. Showing universal time


This command can be used to display the current UTC time (which should normally be used by your hardware clock):


  date -u


It returns something like


  Mon May 28 09:01:01 UTC 2007


How you can see, at the 28th of May, my system time (local time) is set to my UTC time + 2 hours.


3.2. Command "rdate"


3.2.1. Installing


This command is provided by the Debian package rdate. Therefore, to install it run


  apt-get update

  apt-get install rdate


3.2.2. Showing remote time


This command can be used to display the current system time of a remote host.


To use it, call somethink like:


  rdate -p tempo.ien.it


It returns something like


  Mon 28 May 2007 11:13:38 AM CEST


4. Setting the time manually


4.1. Command "date"


4.1.1. Installing


This command is provided by the Debian package coreutils. Therefore, to install it run


  apt-get update

  apt-get install coreutils


4.1.2. Setting system time


This command can be used to set the system time.


To use it, simply call something like


  date --set 2007-05-28

  date --set 19:02:30


4.2. Command "rdate"


4.2.1. Installing


This command is provided by the Debian package coreutils. Therefore, to install it run


  apt-get update

  apt-get install coreutils



This command can be used to synchronize the system time with a remote NTP server.


To use it, call something like:


  rdate tempo.ien.it


Note that rdate has an accuracy of only 1 second.


4.3. Command "ntpdate"


This command can be used to synchronize the system time with a remote NTP server.


To use it, simply call


  ntpdate ipaddr


where ipaddr is the address of a NTP server.


To display some more information, call


  ntpdate -d ipaddr


where ipaddr is the address of a NTP server.


4.4. Command "clock"


This command is obsolete. Use hwclock instead (see next chapter).


This command can be used to synchronize the hardware time with the system time.


To use it, simply call


  clock -w


If hardware clock is in UTC (like it should be!), call:


  clock -w -u


4.5. Command "hwclock"


This command can be used to synchronize the hardware time with the system time.


To use it, simply call


  hwclock --systohc


If hardware clock is in UTC (like it should be!), call:


  hwclock --systohc --utc


4.6. Command "time"


This is a stopwatch and has nothing to do with the clock!


I have reported it, just because it is simple to think that this command could have something to do with the system/hardware clock.


To see what it can do, try this command:


  time sleep 5


You will get something like


  real    0m5.008s

  user    0m0.000s

  sys     0m0.000s


that indicates how many seconds have been necessary to execute the specified command (in this case the "sleep" command).


5. Setting the time automatically


5.1. Introduction


This chapter explains how to change the time automatically using a NTP server.


How already explained, there are other possibilities to do this, but since they are not the best choice, I don't treat them in this document.


5.2. Installing a NTP server


In Debian, the NTP server can be installed in this way:


  apt-get update

  apt-get install ntp


Once installed, it is automatically started.


5.3. Configuring a NTP server


In Debian, the default configuration file will already synchonize your NTP server with several remote NTP servers and provide time to every body asks it for.


The server has just one configuration file called


  /etc/ntp.conf


I have reported here the default config file:


  # /etc/ntp.conf, configuration for ntpd


  driftfile /var/lib/ntp/ntp.drift

  statsdir /var/log/ntpstats/


  statistics loopstats peerstats clockstats

  filegen loopstats file loopstats type day enable

  filegen peerstats file peerstats type day enable

  filegen clockstats file clockstats type day enable



  # You do need to talk to an NTP server or two (or three).

  #server ntp.your-provider.example


  # pool.ntp.org maps to more than 300 low-stratum NTP servers.

  # Your server will pick a different set every time it starts up.

  #  *** Please consider joining the pool! ***

  #  *** <http://www.pool.ntp.org/join.html>; ***

  server 0.debian.pool.ntp.org iburst

  server 1.debian.pool.ntp.org iburst

  server 2.debian.pool.ntp.org iburst

  server 3.debian.pool.ntp.org iburst


  # By default, exchange time with everybody, but don't allow configuration.

  # See /usr/share/doc/ntp-doc/html/accopt.html for details.

  restrict -4 default kod notrap nomodify nopeer noquery

  restrict -6 default kod notrap nomodify nopeer noquery


  # Local users may interrogate the ntp server more closely.

  restrict 127.0.0.1

  restrict ::1


  # Clients from this (example!) subnet have unlimited access,

  # but only if cryptographically authenticated

  #restrict 192.168.123.0  mask  255.255.255.0 notrust


  # If you want to provide time to your local subnet, change the next line.

  # (Again, the address is an example only.)

  #broadcast 192.168.123.255


  # If you want to listen to time broadcasts on your local subnet,

  # de-comment the next lines. Please do this only if you trust everybody

  # on the network!

  #disable auth

  #broadcastclient


If you change something in the configuration file, remember to restart the daemon:


  /etc/init.d/ntp restart


6. Maintening the NTP server


6.1. Introduction


There are a lot of tools that can be used to analyze the status of a NTP daemon.


6.2. Checking log file


The first place to look at in case of problem is the configuration file.


By default, NTP logs to syslog. Therefore, to filter NTP informations:


  grep ntpd /var/log/syslog


6.2.1. Logging to /var/log/ntpd.conf


In case of NTP problem, I suggest you to log to a separate file. This case be done by adding the following line in your configuration file:


  logfile /var/log/ntpd.log


and restarting the daemon


  /etc/init.d/ntp restart


Now, to look for NTP logs, call


  tail -f -n1000 /var/log/ntpd.log


6.2.2. Messages for stopping, starting and restarting the daemon


If the daemon has been stopped, you should see something like:


  May 28 12:09:18 toby ntpd[13623]: ntpd exiting on signal 15


If the daemon has been started, you should see something like:


  May 28 12:09:43 toby ntpd[14049]: ntpd 4.2.2p4@1.1585-o Sun Mar  4 13:21:35 UTC 2007 (1)

  May 28 12:09:43 toby ntpd[14050]: precision = 1.000 usec

  May 28 12:09:43 toby ntpd[14050]: Listening on interface wildcard, 0.0.0.0#123 Disabled

  May 28 12:09:43 toby ntpd[14050]: Listening on interface wildcard, ::#123 Disabled

  May 28 12:09:43 toby ntpd[14050]: Listening on interface lo, ::1#123 Enabled

  May 28 12:09:43 toby ntpd[14050]: Listening on interface eth1, fe80::20e:a6ff:fe31:aacd#123 Enabled

  May 28 12:09:43 toby ntpd[14050]: Listening on interface lo, 127.0.0.1#123 Enabled

  May 28 12:09:43 toby ntpd[14050]: Listening on interface eth1, 192.168.128.110#123 Enabled

  May 28 12:09:43 toby ntpd[14050]: kernel time sync status 0040

  May 28 12:09:43 toby ntpd[14050]: frequency initialized 58.971 PPM from /var/lib/ntp/ntp.drift

  May 28 12:09:53 toby ntpd[14050]: synchronized to 216.168.105.34, stratum 3

  May 28 12:09:53 toby ntpd[14050]: kernel time sync enabled 0001


Note that the server has not been synchronized until you see the following line:


  kernel time sync enabled 0001


and this can take up to 15 minutes!


The NTP server will not provide time to NTP clients until its system time has been synchronized with at least 1 remote NTP server!


@EMI: add here error message of this case


6.3. Command "ntpdc"


This is a NTP query program.


It can be used on your NTP server to check if it has been synchronized with one ore more remote NTP servers.


Type the following command:


  ntpdc -p


You will get something like


       remote           local      st poll reach  delay   offset    disp

  =======================================================================

  *chiccp.net      192.168.128.110  2   64  377 0.12799  0.000470 0.05133

  =168.75.65.20    192.168.128.110  3   64  377 0.11221  0.000187 0.04698

  =osource.org     192.168.128.110  3   64  377 0.14583 -0.002432 0.06522

  =192.135.48.21   192.168.128.110  2   64  377 0.14502  0.002252 0.06416


The sign before the remote NTP server name or IP address (first column) indicates if the NTP server is in sync or not.


The sign "=" indicates not in sync.

The sign "*" indicates in sync.


I guess, there is always only one remote NTP server to which your NTP server is synchronized.


You can use this command to get some additional information. Check these:


  ntpdc -c loopinfo


and


  ntpdc -c kerninfo


6.4. Command "ntpdate"


This command, already explained in the previous chapters, can be used to check if your NTP server provides the time.


Unfortunately, you cannot run it on the same host running your NTP server, because both NTP server and client use the same socket and the socket is already locked by the NTP server.


Therefore, to make this test, you must have a second GNU/Linux host where you can run this command.


For information about how to use it, refer you to the previous chapters.


6.5. Command "ntptrace"


This command follows the chain starting from the given server back to the the stratum 1 server. In this ways, it is possible to know from what base server your time is indirectly synchronized.


To use it, symple call


  ntptrace NtpServerUsedByYourNtpServer


For example, supposing you get your NTP information from pool.ntp.org, calling


  ntptrace pool.ntp.org


will give you something like


  pool.ntp.org: stratum 2, offset 0.036120, synch distance 0.078492

  time-C.timefreq.bldrdoc.gov: stratum 1, offset 0.000000, synch distance 0.002650, refid 'ACTS'


@EMI: check if really so!


7. Installing NTP tools


This chapter explains how to install in Debian GNU/Linux all the tools described in this document.


7.1. Installing command "clock"


This command is obsolete and is not anymore provided by Debian. Use hwclock instead.


7.2. Installing command "date"


This command is provided by the Debian package coreutils. Therefore, to install it run


  apt-get update

  apt-get install coreutils


7.3. Installing command "hwclock"


This command is provided by the Debian package util-linux. Therefore, to install it run


  apt-get update

  apt-get install util-linux


7.4. Installing the daemon "ntp"


This daemon (correspondig to the NTP server/client) is provided by the Debian package ntp. Therefore, to install it run


  apt-get update

  apt-get install ntp


7.5. Installing command "ntpdate"


This command is provided by the Debian package ntpdate. Therefore, to install it run


  apt-get update

  apt-get install ntpdate


7.6. Installing "rdate"


This command is provided by the Debian package rdate. Therefore, to install it run


  apt-get update

  apt-get install rdate


8. Related documents


http://www.ntp.org


http://tldp.org/LDP/sag/html/index.html


http://www.debian.org/doc/manuals/system-administrator/ch-sysadmin-time.html


9. About


9.1. Author


Emidio Planamente <eplanamente@gmx.ch>


9.2. Feedback


Any feedback is welcome.


9.3. History


Version 1.0

  First public release



Emidio Planamente

Last modified on 2007-05-28