9
June
2020
Linux time zone, computer clock synchronization using NTP protocol
10:18

Linux time zone, computer clock synchronization using NTP protocol

9 June 2020 10:18

The system time zone is selected when installing the distribution. In the future, you can configure time synchronization (NTP protocol) for more accurate time and organize the display of information about other time zones. For example, when it is morning in Moscow, it is deep night in the central part of the United States. This article is about how to improve time synchronization, set the time zone and display the time of other time zones on the screen.

Introduction

The time service in Linux can be operated in two ways, which are not mutually exclusive:

  • client (synchronizes the time of this PC with other exact time servers)
  • server (for distribution accurate time to other computers on the network).

I'm considering a time client service.

Goals:
a) obtain high time accuracy (less than 0.5 seconds) on the local computer
b) implement the display of time in different time zones.

Action plan:

  1. Installation
  2. Launch
  3. Check

Enabling ntp client

To synchronize time over the network, you need to install an ntp client and the ntpdate utility:

su -
apt-get update
apt install ntp

Specifying the time zone

First of all, you need to check what time zone is set in the system.

ls -l /etc/localtime

For Moscow, St. Petersburg and the European part of the Russian Federation, a link will be displayed:

/etc/localtime -> /usr/share/zoneinfo/Europe/Moscow

that's right. But you can reconfigure your computer to a different time zone (if the PC has moved to another region).

In Linux Mint, the program for changing the time zone is called time-admin (which must be run with rights of superuser su). This program is also available in the "Start" - "Options" - "Date and Time" menu.
Time-admin program

You can also change the time zone using the command line. First, let’s find out what time zones exist, for example, in Europe:

ls /usr/share/zoneinfo/Europe/

Amsterdam Busingen Kiev Monaco Sarajevo Vaduz
Andorra Chisinau Kirov Moscow Saratov Vatican
Astrakhan Copenhagen Kyiv Nicosia Simferopol Vienna
Athens Dublin Lisbon Oslo Skopje Vilnius
Belfast Gibraltar Ljubljana Paris Sofia Volgograd
Belgrade Guernsey London Podgorica Stockholm Warsaw
Berlin Helsinki Luxembourg Prague Tallinn Zagreb
_Bratislava Isle_ofMan Madrid Riga Tirane Zaporozhye
Brussels Istanbul Malta Rome Tiraspol Zurich
Bucharest Jersey Mariehamn Samara Ulyanovsk
_Budapest Kaliningrad Minsk SanMarino Uzhgorod
now we make a soft symbolic link, for example, to Minsk:

We look at the current time zone

ls -l /etc/localtime

The text "/etc/localtime -> /usr/share/zoneinfo/Europe/Moscow"

We change the time zone, for example, to Minsk (for example only):

sudo ln -sf /usr/share/zoneinfo/Europe/Minsk /etc/localtime

Setting up time synchronization

For the ntp client to work, the following conditions are required: the presence of the configuration file /etc/ntp.conf

permission to launch the ntp client and directly launch the client.

  1. Configuration file /etc/ntp.conf

    driftfile /var/lib/ntp/ntp.drift

    leapfile /usr/share/zoneinfo/leap-seconds.list

    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

    pool 0.ubuntu.pool.ntp.org iburst

    pool 1.ubuntu.pool.ntp.org iburst

    pool 2.ubuntu.pool.ntp.org iburst

    pool 3.ubuntu.pool.ntp.org iburst

    pool ntp.ubuntu.com

    restrict -4 default kod notrap nomodify nopeer noquery limited

    restrict -6 default kod notrap nomodify nopeer noquery limited

    restrict 127.0.0.1

    restrict ::1

    restrict source notrap nomodify noquery

  2. Starting the ntp client service is allowed by default.

You can, but it is not necessary to give commands

 systemctl enable ntp
 systemctl start ntp
 systemctl status ntp

In order for the service to re-read the updated configuration (if you edited the /etc/ntp.conf file), you need to run

sudo systemctl restart ntp

Monitoring the time synchronization client

ntpq -pw

A list of ntp servers with which the client performs time synchronization will be displayed. (You have to wait until the program output finishes).

How to find out the time deviation between the local computer and the remote time server

Using the ntpq utility.

Or clockdiff

su -
apt-get update
apt install iputils-clockdiff

Find out the time deviation from another computer - NTP server:

sudo clockdiff 130.149.17.21

or

sudo clockdiff 1.ru.pool.ntp.org

It can be seen that the deviation, i.e. PC time inaccuracy is 34 milliseconds:

..................................................

host=130.149.17.21 rtt=34(0)ms/34ms delta=-1ms/-1ms Tue Jun 9 11:38:44 2020

When comparing with another server the situation is the same, deviation is 35 ms:

clockdiff 91.206.16.3

..................................................

host=91.206.16.3 rtt=35(0)ms/35ms delta=0ms/0ms Tue Jun 9 11:44:57 2020

It should be kept in mind that clockdiff does not take into account network delays (ping), it only allows you to compare the time with another computer without taking into account the delay of packets on the network.

The command displays a more accurate deviation value

ntpq -p

or

ntpq -pw

where the difference (offset +- jitter) is visible in just a couple of milliseconds.

Another way to find out the time deviation

https://time.is

Displaying world time on screen

World Time Program (globaltime) for Xfce Desktop Environment allows you to make a clock with time for multiple time zones.
Installing the GlobalTime program:

sudo apt install xfce4-datetime-plugin

Sources:



Related publications