Skip to content

NTP on Linux (update clock)

Linux maintains two clocks: one of them is battery powered, known as a “Real Time Clock”, RTC, “CMOS clock”, “Hardware clock” or machine clock. It counts the time when the system is turned off and is not used when the system is active. NTP on Linux is an implementation that keeps the clock up to date.

The other clock called “system clock”, “kernel clock”, “software clock” or system clock is software that counts time based on processor interruptions. It needs to be initialized by the RTC when the system loads.

Generally there may be differences between the time marked between these two clocks and the actual time and date. We will see the commands to adjust the system clock.

Time on Earth is measured in seconds, which is a unit of the International System of Units. Since 1967, the second has been defined based on the measurement of atomic clocks, which is the duration of 9,192,631,770 periods of radiation corresponding to the transition between two hyperfine levels of the ground state of the cesium 133 atom.

An important characteristic of time is its monotonicity, which means that time always moves forward. This characteristic is difficult to maintain by clocks implemented in software, such as in Operating Systems.

Thus, computer systems lack to have their clocks adjusted frequently.

To solve this problem, the NTP - Network Time Protocol was created, which implements, in addition to a protocol, a network of servers with atomic clocks that serve to adjust the clocks of other computers in the world.

These clocks are coordinated by the Bureau International des Poids et Mesures (BIPM), which comprises more than 260 atomic clocks located at metrology institutes and observatories around the world, including Brazil.

NTP is a protocol for synchronizing computer clocks based on a reliable source of time, such as the atomic clocks of the National Observatory, which define the Brazilian Legal Time. With NTP it’s easy to keep your computer’s clock at the right time at all times.

In this way, NTP accesses one or more time servers, and adjusts the system’s local time based on these servers.

The latest version of the NTP software can be obtained at http://www.ntp.org/downloads.html or downloaded as a package from the Linux distribution.

The NTP package installs a service called ntpd, which runs in the background and makes adjustments to the time and times in stoppers, keeping the system clock up to date.

ntp.conf file

NTP has a configuration file called ntp.conf that usually resides in the /etc. directory.

See the CentOS 7 configuration file ntpd.conf:

# “memory” file for the computer's frequency slip
driftfile /var/lib/ntp/drift
# restricts access to localhost only
Restrict 127.0.0.1
Restrict: :1
# servers
0.centos.pool.ntp.org iburst server
1.centos.pool.ntp.org iburst server
2.centos.pool.ntp.org iburst server
3.centos.pool.ntp.org iburst server

In addition to synchronizing the local system clock with the NTP server clock selected in the configuration file, NTP also regulates the clock through the system phase and frequency, continuously, even during periods when it is not possible to consult time servers.

In this way, the characteristics of the local clock are measured and become known to the NTP, which makes it possible for it to make corrections in time independently. The file indicated by the driftfile key stores the expected frequency error for the watch.

The restrict key makes it possible to restrict access to NTP only to localhost or local network addresses.

It is not necessary for all computers to connect remotely to an external time server. A Linux operating as a network gateway can connect to an external NTP server and serve as a synchronization source for other computers on the network.

The server parameter followed by the server name or address indicates which servers the NTP will fetch the time from. It is also recommended to additionally use the iburst parameter in the command, which serves to speed up the initial synchronization.

It is common to indicate more than one server with the server parameter, so that the client can simultaneously search for the time on more than one server.

By default, the NTP configuration file already contains a list of NTP servers. You don’t need to change the file unless you want to.

Once installed and configured, to use the NTP service it is necessary to start the service with the command:

# systemctl start ntpd

And then enable the service to start at the system load:

# systemctl enable ntpd

The ntpd service uses port 123 of the UDP transport protocol to talk to its clients or connect to the servers. The “ss” command with the “-lup” option can view the listening status of port 123:

# ss -lup | grep ntp UNCONN *:ntp *: * users :( (“ntpd”, pid=7744, fd=16))

Time Zone

Even though the NTP fetches the time from a particular server, from a country or region with the time in another time zone, Linux still adjusts the local time zone using one of the methods below:

**Adjustment Form** **Note**
/etc/localtime Symbolic link to some time zone in /usr/share/zoneinfo. Generally used to configure the Time Zone of Red Hat-based distributions.
/etc/timezone File containing the name of the Time Zone used by Linux. Generally used to configure the Timezone of Debian-based distributions.
TZ Environmental variable containing the name of the Time Zone. It works on any Linux and can be customized for each user.
timedatectl Systemd service that can be used to configure the Time Zone in distributions that use Systemd.
### pool.ntp.org

The pool.ntp.org project is a large virtual cluster of time servers providing a reliable and easy-to-use NTP service for millions of customers.

Pool.ntp.prg is being used by tens of millions of systems around the world. It is the standard “time server” for most Linux distributions and many network appliances.

Its use is very simple, just use your servers as a reference in the configuration:

server 0.pool.ntp.org server 1.pool.ntp.org server 2.pool.ntp.org server 3.pool.ntp.org

ntpdate command

The ntpdate command can also be used to synchronize the clock at a given time, even if the NTP service is not active:

$ ntpdate -q a.ntp.br server 2001:12 ff: :8, stratum 2, offset 0.004601, delay 0.04297 server 200.160.0.8, stratum 2, offset 0.003794, delay 0.04475 20 Oct 14:50:02 ntpdate [10902]: adjust time server 2001:12 ff: :8 offset 0.004601 sec

The “-q” option only queries the server without changing the local clock. To change the local clock with ntpdate, the ntpd service cannot be running.

Ntpq command

The ntpq utility can be used to query NTP servers. It can be used in interactive mode where a prompt will be presented, or using parameters.

The “-p” option shows the list of peer servers and the status of each one:

# ntpq -p <br></br>remote st t when poll reach delay offset jitter <br></br>============================================================================= <br></br>+gps.ntp.br 2 u 86 256 217 19,648 -1,463 1059.39 <br></br>*b.st1.ntp.br 1 u 198 256 307 31,605 4,667 828,577 <br></br>2001:440:1880:5 16 - 52m 1024 0 0.000 000,000 <br></br>b.ntp.br 2 u & nbsp; 63 256 237 55,025 0.950 732561. <br></br>+a.ntp.br 2 u 81 256 217 20,002 -1.437 1057.36 <br></br>+a.st1.ntp.br 1 u 56 256 217 21,052 -1,931 1065.28 <br></br>+gps.jd.ntp.br 1 u 96 256 217 21,140 -1,688 1055.66

+d.st1.ntp.br 1 u 121 256 217 23,940 0.083 1058.75 ### Hwclock command

The hwclock command can be used to read the machine clock or update the machine clock with the system clock and vice versa.

As stated at the beginning of the chapter, computers maintain two clocks. A so-called machine clock (hardware), is turned on when the system is inactive, and is held by a quartz crystal and battery when the computer is turned off. This battery is capable of keeping the watch even when disconnected from the electrical network for a long time.

When the computer is turned on, Linux reads the machine clock to update its system clock. It is common for there to be a small difference between these two watches.

The hwclock command has the following options:

  • -r: enter the hardware clock times with the local time zone;
  • -s: set the system clock with the hardware clock times;
  • -w: configure the hardware clock with the system clock times;
  • -c: compares the hardware clock and system clock times every 10 seconds;
  • —adjust: add or subtract time from the hardware clock to adjust it to frequency slippage;

Examples:

# hwclock -c <br></br>hw-time system-time freq-offset-ppm tick <br></br>1571594417 1571594417.011423 <br></br>1571594427 1571594427.012649 123

1 To set the Hardware clock with the system clock:

# hwclock -w

The Chrony Bundle

Chrony is a versatile implementation of the Network Time Protocol (NTP). It can synchronize the system clock with NTP servers.

It can also operate as an NTPv4 server (RFC 5905) and ready to provide a time service to other computers on the network.

It can be installed through the Linux distribution packages:

$ sudo apt install chrony

chrony.conf file

Chrony uses the configuration file called chrony.conf, which can reside in the /etc/ or /etc/chrony directory.

Its configuration is similar to ntpd.conf, with the only difference being that the server key is called pool:

$ cat /etc/chrony/chrony.conf <br></br>pool ntp.ubuntu.com iburst maxsources 4 <br></br>pool 0.ubuntu.pool.ntp.org iburst maxsources 1 <br></br>pool 1.ubuntu.pool.ntp.org iburst maxsources 1 <br></br>pool 2.ubuntu.pool.ntp.org iburst maxsources 2 <br></br>driftfile /var/lib/chrony/ Chrony.Drift

The chrony service can be started and enabled with the systemctl command:

$ sudo systemctl start chrony

To enable the service on system load:

$ sudo systemctl enable chrony Executing: /lib/systemd/systemd-sysv-install enable chrony

Chronyc command

Chronyc is a utility used to monitor the performance of chronyd and to change its operation while it is running.

If no command is specified on the command line, chronyc will wait for user input via the chronyc> prompt.

It accepts several commands, both in interactive mode and directly as an argument:

$ chronyc <br></br>chronyc> sources <br></br>210 Number of sources = 8 <br></br>MS name/IP address Stratum Poll Reach LastRx Last sample <br></br>============================================================================= <br></br>^- golem. canonical.com 2 6 373 70 -9241us [-9241us] +/- 143ms

^- pugot.canonical.com 2 6 377 77 -11ms [-11ms] +/- 135ms
chronyc> quit The sources command does the same thing as the “ntpq -p” command.

The tracking command shows the status of the system clock:

$ chronyc tracking <br></br>Reference ID: C814BA4C (d.st1.ntp.br) <br></br>Stratum: 2 <br></br>Ref time (UTC): Sun Oct 20 20:15:10 2019 <br></br>System time: 0.000793245 seconds slow of NTP time <br></br>Last offset: -0. 000647185 seconds <br></br>RMS offset: 0.001815075 seconds <br></br>Frequency: 4.009 ppm slow <br></br>Residual freq: -0.191 ppm <br></br>Skew: 3.915 ppm <br></br>Root delay: 0.031462338 seconds <br></br>Root dispersion: 0.002590907 seconds

Update interval: 129.0 seconds
Leap status: Normal The communication between the chronyd server and the chronyc client is carried out via the 323 UDP port:

$ sudo ss -lunp | grep chrony UNCONN [::1] :323 [::] :* users :( “chronyd”, pid=18869, fd=6))

timedatectl command

The timedatectl command is able to identify that Linux is using a system clock update service, such as ntpd or chronyd:

$ timedatectl <br></br>Local time: Sun 2019-10-20 17:17:34 -03 <br></br>Universal time: Sun 2019-10-20 20:17:34 UTC <br></br>RTC time: Sun 2019-10-20 20:17:34

Time zone: America/Buenos_Aires (-03, 15:00)
Synchronized clock system: yes
NTP service: active
RTC in local TZ: no Since chrony is faster to update the date and time in the system, it is ideal for desktop and notebook systems, which are turned off frequently. NTP, on the other hand, is suitable for computers that are permanently connected.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share