How to syncronize time in Linux (using ntp, openntpd, systemd-timesyncd and chrony)

Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks. The protocol is usually described in terms of a client-server model, but can as easily be used in peer-to-peer relationships where both peers consider the other to be a potential time source. There are a number of implementations.

  • ntpd@man deamon implementation of NTP which sets and maintains the system time of day in synchronism with Internet standard time servers.
## install
$ sudo yum install ntp | sudo apt-get install ntp

## configure NTP server, add servers from http://www.pool.ntp.org/en/
$ cat /etc/ntp.conf
server 0.north-america.pool.ntp.org
server 1.north-america.pool.ntp.org
server 2.north-america.pool.ntp.org
server 3.north-america.pool.ntp.org 

## start service
$(el7) systemctl start ntpd ; systemctl enable ntpd ; sytemctl status ntp
$(el6) service ntpd start ; chkconfig ntpd on
$(deb) /etc/init.d/ntp start

## check logs '/var/log/ntp.log', verify date
$ ntpq -p ; date -R

## manually sync
$ ntpdate -u 0.north-america.pool.ntp.org

from ntp@centos and ntp@debian

  • OpenNTPD/OpenNTPD@wiki is an attempt by the OpenBSD team to produce an NTP daemon implementation that is secure, simple to security audit, trivial to set up and administer, and has small memory requirement that synchronizes local clock on the computer with remote NTP server with reasonable accuracy. It’s an alternative to default default NTPD client/server.
## install (from source)
$ wget ftp://ftp.openbsd.org/pub/OpenBSD/OpenNTPD/openntpd-3.9p1.tar.gz -O - | tar -xz
$ cd $1 ; ./configure ; make ; sudo make install
$ groupadd _ntp
$ useradd -g _ntp -s /sbin/nologin -d /var/empty -c 'OpenNTP daemon' _ntp
$ cp contrib/redhat/ntpd /etc/init.d/openntpd
$ chmod +x /etc/init.d/openntpd
$ cat /etc/init.d/openntpd
PATH=/usr/local/sbin:$PATH

## or install (from repo)
$ sudo apt-get install openntpd (deb/ubuntu)

## configure
$ cat /usr/local/etc/ntpd.conf
listen on 127.0.0.1
...
server ntp.isp.example.com
# Use a random selection of 8 public stratum 2 servers
servers pool.ntp.org
$ chkconfig openntpd on ; service openntpd start
$ tail -f /var/log/messages
$ netstat -tulpn | grep :123

from CentOS / Redhat Linux: Install OpenNTPD To Synchronize The Local Clock

  • systemd-timesyncd is a daemon that has been added for synchronizing the system clock across the network. It implements an SNTP client. In contrast to NTP implementations such as chrony or the NTP reference server this only implements a client side, and does not bother with the full NTP complexity.
## systemd-timesyncd service is available with systemd >= 213 (el7 uses 208), to start and enable it
$ timedatectl set-ntp true

## configuration
# to add time servers or change the provided ones, uncomment the relevant line and list their host name or IP separated by a space
$ cat /etc/systemd/timesyncd.conf
[Time]
NTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 0.fr.pool.ntp.org

from systemd-timesyncd@arch

  • chrony@arch is an alternative NTP client and server roaming friendly and designed specifically for systems that are not online all the time.
## install
$ sudo yum install chrony | sydo apt-get install chrony

## configure
'offline' only poll when chronyc is online
'auto_offline' assume server gone offline when 2 requests without reply
'iburst' on start make four measurements over short period (rather than the usual periodic measurements)
'minstratum' increase stratum of the source to a minimum, to avoid being selected; chronyd preferes lower stratum sources
'polltarget' num of measurements used in regression algorithm; higher target means shorter polling intervals
'prefer' prefer this source over sources without prefer option.
'noselect' never select this source

$ cat /etc/chrony.conf
server 1.2.3.4 offline
server 5.6.7.8 offline
server 9.10.11.12 offline
driftfile /etc/chrony.drift
keyfile /etc/chrony.keys
generatecommandkey
commandkey 1
rtconutc
rtcsync
$ systemctl restart chronyd

# show current sources
$ chronyc sources -v
# show rate and offset estimation performance of sources
$ chronyc sourcestats
# show system clock performance
$ chronyc tracking
# set offline/activity; also done automatically by 'networkmanager-dispatcher-chrony' or 'netctl-dispatcher-chrony'
$ chronyc -a activity

from Maintaining System clock using chrony

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s