Accurate timing has become a key component of modern software deployment. Whether it is to ensure that logs are recorded in the correct order or to apply database updates correctly, out-of-sync time can lead to errors, data corruption, and other difficult-to-debug problems.
Ubuntu 18.04 has built-in time synchronization, which is activated by systemd's timesyncd service by default. In this article, we will introduce some basic time-related commands, verify that timesyncd is active, and learn how to install a backup network time service.
Before starting this tutorial, you will need an Ubuntu 18.04 server with a non-root user with sudo privileges. Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.
The most basic command to find the time on the server is date
. Any user can enter this command to print the date and time:
date
Tue Jul 1014:48:52 UTC 2018
In most cases, your server will default to the UTC time zone, as shown in the output above. UTC is Coordinated Universal Time, the time when the longitude is zero degrees. When your infrastructure spans multiple time zones, using universal time consistently can reduce confusion.
If you have different requirements and need to change the time zone, you can use the timedatectl
command to do this.
First, list the available time zones:
timedatectl list-timezones
The list of time zones will be printed to your screen. You can press SPACE
to page down, and press b
to page up. Once you find the correct time zone, write it down and type q
to exit the list.
Now set the time zone with timedatectl set-timezone
, making sure to replace the highlighted part below with the time zone you found in the list. You need to use sudo
of timedatectl
to make changes:
sudo timedatectl set-timezone America/New_York
You can run date
again to verify the changes:
date
Tue Jul 1010:50:53 EDT 2018
The time zone abbreviation should reflect the newly selected value.
Now that we know how to check the clock and set the time zone, let us make sure that our time is properly synchronized.
Until recently, most network time synchronization was handled by Network Time Protocol Daemon or ntpd. This service connects to other NTP server pools to provide continuous and accurate time updates.
The default installation of Ubuntu now uses timesyncd instead of ntpd. timesyncd connects to the same time server and works in roughly the same way, but is more lightweight and more integrated with the low-level work of systemd and Ubuntu.
We can query the status of timesyncd by running timedatectl
without parameters. In this case you don't need to use sudo
permissions:
timedatectl
Local time: Tue 2018-07-1010:54:12 EDT
Universal time: Tue 2018-07-1014:54:12 UTC
RTC time: Tue 2018-07-1014:54:12
Time zone: America/New_York(EDT,-0400)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no
This will print out the local time, universal time (if you did not switch from the UTC time zone, it may be the same as the local time), and some network time status information. System clock synchronized: yes
means that the time has been successfully synchronized, systemd-timesyncd.service active: yes
means that timesyncd is enabled and running.
If timesyncd is not activated, use timedatectl to turn it on:
sudo timedatectl set-ntp on
Run timedatectl
again to confirm the network time status. This may take one minute for the actual synchronization to occur, but in the end both Network time on:
and NTP synchronized:
should be yes
.
Although time synchronization is good for most purposes, some applications that are very sensitive to even the slightest time disturbance can be better served by ntpd because it uses more sophisticated techniques to keep the system gradually and continuously The normal operation of time.
Before installing ntpd, we should turn off timesyncd:
sudo timedatectl set-ntp no
Verify that timesyncd is closed:
timedatectl
Look for systemd-timesyncd.service active: no
in the output. This means that timesyncd
has stopped. We can now install the ntp
package with apt
:
sudo apt update
sudo apt install ntp
ntpd will start automatically after installation. You can query the status information in ntpd to verify that everything is normal:
ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================0. ubuntu.pool.n .POOL.16 p -6400.0000.0000.0001.ubuntu.pool.n .POOL.16 p -6400.0000.0000.0002.ubuntu.pool.n .POOL.16 p -6400.0000.0000.0003.ubuntu.pool.n .POOL.16 p -6400.0000.0000.000
ntp.ubuntu.com .POOL.16 p -6400.0000.0000.000+ec2-52-0-56-137216.239.35.02 u 166417.872-2.1371.485+66.220.10.2129.6.15.302 u 1264165.2043.7402.686+block.steinhoff 209.51.161.2382 u 1164133.3641.7103.586+eterna.binary.n 216.229.0.503 u 1164135.3302.8212.839+2604:a880:800:1209.51.161.2382 u 146410.3940.3862.462+ec2-52-6-160-3.130.207.244.2402 u 116418.1502.0503.053+mx.danb.email 127.67.113.922 u 1364163.8681.5392.240*hydrogen.consta 129.6.15.282 u 126412.9891.7552.563+ntp-3.jonlight.127.67.113.922 u 1064164.5612.1223.593+undef.us 45.33.84.2083 u 1264133.5081.6313.647+ntp-3.jonlight.127.67.113.922 u 864164.2532.6453.1742001:67c:1560:8145.238.203.142 u 2264171.155-1.0590.000+test.diarizer.c 216.239.35.42 u 1164164.3784.6483.2442001:67c:1560:8145.238.203.142 u 1864170.744-0.9640.000
alphyn.canonica 132.246.11.2312 u 176417.973-0.1700.000+vps5.ctyme.com 216.218.254.2022 u 1064165.8741.9022.608
ntpq
is a query tool for ntpd. The -p
flag requires information about the NTP server (or p EERS) that NTPD is connected to. Your output will be slightly different, but the default Ubuntu pool server and some other servers should be listed. Please keep in mind that it may take several minutes for ntpd to establish a connection.
In this article, we showed how to view the system time, change the time zone, use Ubuntu's default time synchronization, and install ntpd. If you have more complicated timing requirements than what we described here, you can refer to Official NTP Document, and you can also view NTP Pool Project, which is a global volunteer The team provides most of the NTP infrastructure in the world.
To learn more about setting time synchronization related tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Set Up Time Synchronization on Ubuntu 18.04"
Recommended Posts