Most often your server will default to the UTC time zone, as highlighted in the above output. UTC is Coordinated Universal Time, the time at zero degrees longitude. Consistently using Universal Time reduces confusion when your infrastructure spans multiple time zones.
If you have different requirements and need to change the time zone, you can use the timedatectl
command to do so.
First, list the available time zones:
A list of time zones will print to your screen. You can press SPACE
to page down, and b
to page up. Once you find the correct time zone, make note of it then type q
to exit the list.
Now set the time zone with timedatectl set-timezone
, making sure to replace the highlighted portion below with the time zone you found in the list. You'll need to use sudo
with timedatectl
to make this change:
You can verify your changes by running date
again:
The time zone abbreviation should reflect the newly chosen value.
Now that we know how to check the clock and set time zones, let’s make sure our time is being synchronized properly.
Controlling timesyncd with timedatectl
Until recently, most network time synchronization was handled by the Network Time Protocol daemon or ntpd. This server connects to a pool of other NTP servers that provide it with constant and accurate time updates.
Ubuntu's default install now uses timesyncd instead of ntpd. timesyncd connects to the same time servers and works in roughly the same way, but is more lightweight and more integrated with systemd and the low level workings of Ubuntu.
We can query the status of timesyncd by running timedatectl
with no arguments. You don't need to use sudo
in this case:
[email protected]:/home$ timedatectl
Local time: Tue 2018-11-06 09:06:47 WIB
Universal time: Tue 2018-11-06 02:06:47 UTC
RTC time: Tue 2018-11-06 02:06:48
Time zone: Asia/Jakarta (WIB, +0700)
Network time on: yes
NTP synchronized: no
RTC in local TZ: no
[email protected]:/home$
This prints out the local time, universal time (which may be the same as local time, if you didn't switch from the UTC time zone), and some network time status information. Network time on: yes
means that timesyncd is enabled, and NTP synchronized: yes
indicates that the time has been successfully synced.
If timesyncd isn't enabled, turn it on with timedatectl:
Run timedatectl
again to confirm the network time status. It may take a minute for the actual sync to happen, but eventually both Network time on:
and NTP synchronized:
should read yes
.
Switching to ntpd
Though timesyncd is fine for most purposes, some applications that are very sensitive to even the slightest perturbations in time may be better served by ntpd, as it uses more sophisticated techniques to constantly and gradually keep the system time on track.
Before installing ntpd, we should turn off timesyncd:
Verify that timesyncd is off:
[email protected]:~$ timedatectl
Local time: Tue 2018-11-06 09:46:57 WIB
Universal time: Tue 2018-11-06 02:46:57 UTC
RTC time: Tue 2018-11-06 02:46:58
Time zone: Asia/Jakarta (WIB, +0700)
Network time on: yes
NTP synchronized: no
RTC in local TZ: no
[email protected]:~$
Look for Network time on: no
in the output. This means timesyncd
has been stopped. We can now install the ntp
package with apt-get
:
[email protected]:~$ sudo apt-get install ntp
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libopts25
Suggested packages:
ntp-doc
The following NEW packages will be installed:
libopts25 ntp
0 upgraded, 2 newly installed, 0 to remove and 73 not upgraded.
Need to get 576 kB of archives.
After this operation, 1,792 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://id.archive.ubuntu.com/ubuntu xenial/main amd64 libopts25 amd64 1:5.18.7-3 [57.8 kB]
Get:2 http://id.archive.ubuntu.com/ubuntu xenial-updates/main amd64 ntp amd64 1:4.2.8p4+dfsg-3ubuntu5.9 [519 kB]
Fetched 576 kB in 14s (41.0 kB/s)
Selecting previously unselected package libopts25:amd64.
(Reading database ... 74018 files and directories currently installed.)
Preparing to unpack .../libopts25_1%3a5.18.7-3_amd64.deb ...
Unpacking libopts25:amd64 (1:5.18.7-3) ...
Selecting previously unselected package ntp.
Preparing to unpack .../ntp_1%3a4.2.8p4+dfsg-3ubuntu5.9_amd64.deb ...
Unpacking ntp (1:4.2.8p4+dfsg-3ubuntu5.9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu21.4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up libopts25:amd64 (1:5.18.7-3) ...
Setting up ntp (1:4.2.8p4+dfsg-3ubuntu5.9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21.4) ...
Processing triggers for ureadahead (0.100.0-19) ...
[email protected]:~$
ntpd will be started automatically after install. You can query ntpd for status information to verify that everything is working:
[email protected]:~$ sudo ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
[email protected]:~$
[email protected]:~$ timedatectl
Local time: Tue 2018-11-06 09:52:30 WIB
Universal time: Tue 2018-11-06 02:52:30 UTC
RTC time: Tue 2018-11-06 02:52:30
Time zone: Asia/Jakarta (WIB, +0700)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no
[email protected]:~$
ntpq
is a query tool for ntpd. The -p
flag asks for information about the NTP servers (or peers) ntpd has connected to. Your output will be slightly different, but should list the default Ubuntu pool servers plus a few others. Bear in mind that it can take a few minutes for ntpd to establish connections.
Conclusion
In this article we’ve shown how to view the system time, change time zones, work with Ubuntu's default timesyncd, and install ntpd. If you have more sophisticated timekeeping needs than what we’ve covered here, you might reference
the offical NTP documentation, and also take a look at
the NTP Pool Project, a global group of volunteers providing much of the world's NTP infrastructure.