Archive for the ‘ntp’ Category

Changing the timezone after installation

May 6, 2010

If the timezone is not set or is wrong, the superuser can run tzconfig to configure it after the operating system is installed and with latest releases it is actually –

dpkg-reconfigure tzdata

If there are other users, it is a good idea to notify then that the system Timezone has changed.

Setting the TZ environment variable.

If you do not have root privileges or want to set for yourself a different timezone than the one the system uses, you can set the environment variable TZ. Use the command tzselect to show what value to use for TZ and place it on your .bashrc or .profile file.

If you want everyone will see the changes then export that value on /etc/profile and /etc/timezone file.

To change the computer to use UTC after installation, edit the file /etc/default/rcS, change the variable UTC to no. If you happened to install your system to use local time, just change the variable to yes to start using UTC. It is best to reboot after editing /etc/default/rcS to get the changes effective.

Syncing time, rdate/ntpdate and NTP

If you have another Unix computer which you know keeps the correct time, with root privileges you can set the time with the command rdate. For example:

rdate somehost.domain.com

Even if you do not have an accurate time source, it is still a good idea to set all your computers to the same time, so that you can compare time stamps between hosts. To keep the clocks synchronized, You can start rdate once daily from cron.

Setting time at system boot

If the hardware clock does not keep the correct time, it is possible to set the correct time when the operating system boots. For this, there must be some other host in the LAN where the time can be received. Here is an example:

1.

#!/bin/bash
case “$1” in
start)
ntpdate/rdate ntp.test.com
;;
stop)
;;
esac
exit 0

2.
chmod 751 /etc/init.d/rdate

3.
update-rc.d setdate defaults
OR Create a symbolic link to that file in directory /etc/rc2.d
ln -s /etc/init.d/rdate /etc/rc2.d/S19rdate

Setting time using NTP

If you are connected to the Internet, you can install an NTP client, for example ntp or xntp3 in Debian version 2.1 and older. This uses the Network Time Protocol RFC 1305 to synchronise clocks to a few tens of milliseconds precision.

See the excellent documentation that comes with ntp, in the Debian package ntp-doc. It is very thorough and thus long. If you think reading documents is a waste of time, just ask your Internet Service Provider or system administrator for NTP server names, or look up the nearest one in “List of Public NTP Servers” in http://www.eecis.udel.edu/~mills/ntp/servers.html . If you start using an NTP server, it is usually polite to notify the server’s administrator of the fact.

Do not configure your system to query level 1 NTP servers! If you think you need to do this, you are almost certainly wrong!

Once you know an NTP server, edit the file /etc/ntp.conf to add at least one server line. Here is an example:

# /etc/ntp.conf, configuration for xntpd

logfile /var/log/xntpd
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

server ntp.somedomain.something
server ntp.something.else

Note, that I changed the actual server name in the above example, to prevent all Debian GNU/Linux users from blindly using that one server. Everything else except the server lines was there after xntp3 installation.

If you do not have a permanent Internet connection, then running NTP client is not a good solution. NTP client syncronizes relatively often, and needs the Internet connection to be always on. If you have a dial up Internet connection, you can run the command ntpdate (also in ntp package) to syncronize each time you connect.

Another possibility is to set cron to run ntpdate once daily, this gives good accuracy for most uses. The following is an example script that can be started from cron and runs ntpdate. This scripts assumes the /etc/ntp.conf is correctly set up, since it gets the server names from that file.

dilbert# cat /etc/cron.nightly/ntpdate
#!/bin/sh
#
# Last modification: Sat Aug 8 05:27:07 EEST 1998
# ntpdate cron nightly

NTPDATE=/usr/sbin/ntpdate
LOGFILE=/var/log/xntpd
NTPCONFFILE=/etc/ntp.conf
NTPSERVERS=
GREP=/usr/bin/grep
CUT=/usr/bin/cut

if [ -f $NTPCONFFILE ] ; then
echo “===========================” >> $LOGFILE
echo “<<> $LOGFILE
for i in `grep ^server $NTPCONFFILE | $CUT –fields 2 –delimiter \ `
do
NTPSERVERS=”$NTPSERVERS $i”
done
$NTPDATE $NTPSERVERS >> $LOGFILE
###echo $NTPDATE $NTPSERVERS $LOGFILE
echo “>>> `date` ” >> $LOGFILE
fi

exit 0
dilbert#

Advertisements

NTP – A insight look

July 13, 2009
NTP (Network Time Protocol) provides accurate and syncronised time across the Internet. This introductory article will try to show you how to use NTP to control and synchronize your system clock.
The timing signal utilised by most NTP serveris a UTC time source. UTC (Coordinated Universal Time) is a global time scale based on the time told by atomic clocks. By utilising UTC a NTP server can in affect, synchronise a network to the same time as millions of other computer networks from around the world. This has made possible many online global transactions that just simply wouldn’t be possible without UTC.

The timing signal is received by the NTP server (or time server) via a number of ways; the Internet, national time and frequency transmission (long wave) or the GPS (global positioning system) network. Once received the time server (NTP server) checks the authenticity of this signal (except from Internet sources where authentication is not possible), evaluates its accuracy then distributes it amongst the network.

To prevent a possible overload of time requests to the time server, machines that receive a time signal from the NTP server, can themselves be used as a time reference and the machines that receive a time signal from those can again be used as a reference. This hierarchy is called stratum levels. A NTP server is a stratum 1 device, a machine that receives a signal directly from the time server is a stratum 2 device and if a machine receives a signal from that it becomes stratum 3.

NTP server Stratum level explain

NTP is a hierarchical protocol and is divided into stratum which define the distance from the reference clock. A reference clock source that relays UTC (Coordinated Universal Time) time and has little or no delay is known as a stratum-0 device. Stratum-0 servers cannot be used on the network, instead, they are directly connected to computers which then operate as primary.

A primary server that receives a time signal from a stratum 0 device either through the GPS network or national time and frequency transmission is known as a stratum-1 device. On a network a stratum 1 time server supplies the time to other devices on the network which are known as stratum-2 devices. These also can be used as a time source and equipment that connects to a stratum-2 device to receive it become stratum-3 and so on.

NTP can handle up to 16 different stratum levels, although the lower down the hierarchy you go the less accurate the devices become. However, to make the system more reliable, each client can receive a time source from multiple servers. Stratum 2 devices and below can also synchronise with each other. The NTP software monitors continuously the figures of stability and accuracy of all the servers and always chooses a server with the best figures.

Multiple stratum are used an in larger networks because to bombard a single stratum-1 time server with NTP requests from thousands of machines could cause it to overload or block the network itself with workstations/routers etc repeatedly waiting for their time synchronisation requests to go through.

ntp-hierarchy

Telling the NTP time

Network Time Protocol (NTP) is an Internet based protocol designed to distribute and synchronise time across a network.

NTP is in fact one of the oldest Internet protocols having been developed in the late 1980’s at Delaware University when the Internet was still in its infancy. It was devised by Professor David Mills and his team when they realised the need for accurate time synchronisation if computers were needed to communicate with each other.

A NTP server is a dedicated device that receives a single timing source and then distributes it amongst all network devices. A NTP server will receive the timing information through a number of ways but normally it is a UTC source (coordinated universal time) a global timescale based on the time as told by ATOMIC CLOCKS.

NTP handles the time in a different way to how humans perceive and deal with it. While we may split a time into seconds, minutes, hours, days, months and years; NTP regards time  as a single number which is the number of seconds since the ‘prime epoch’.

The prime epoch is a date set for when NTP began counting seconds. For NTP the prime Epoch is 00.01 on 1 January 1900 so that means on 1 January 2008 the time according to NTP will be 3405888000, which is the number of seconds since 1900.

Finding a Public NTP server

A public NTP Server is a time server on the Internet that, as the name suggests, members of the public can use as a timing source. The best location on the Internet to find a list of public NTP servers is the home of NTP – www.ntp.org

Setting local date and time using NTP

The ntpdate command sets the local date and time by polling the NTP servers specified to determine the correct time. It obtains a number of samples from each server specified and applies the standard NTP clock filter and selection algorithms to select the best of the samples.

The ntpdate command makes time adjustments in one of the following ways:

  • If it determines that the clock is off by more than 0.5 seconds, it steps the clock’s time by calling the settimeofday subroutine. This is the preferred method at boot time.
  • If it determines that the clock is off by less than 0.5 seconds, it slews the clock’s time by calling the adjtime subroutine with the offset. This method tends to keep a badly drifting clock more accurate, though at some expense to stability. When running the ntpdate command on a regular basis from the cron command instead of running a daemon, doing so once every hour or two results in precise enough timekeeping to avoid stepping the clock.

    Notes:

    1. The ntpdate command’s reliability and precision improves dramatically with a greater number of servers. Although you can use a single server, you obtain better performance by providing at least three or four servers.
    2. If an NTP server daemon like the xntpd daemon is running on the same host, the ntpdate command will decline to set the date.
    3. You must have root authority on the local host to run this command.