How To Set Up Time Synchronization on Debian 9

Introduction

Accurate timekeeping has become a critical component of modern software deployments. Whether it's making sure logs are recorded in the right order or database updates are applied correctly, out-of-sync time can cause errors, data corruption, and other hard to debug issues.

Debian 9 has time synchronization built in and activated by default using the standard ntpd time server, provided by the ntp package. In this article we will look at some basic time-related commands, verify that ntpd is active and connected to peers, and learn how to activate the alternate systemd-timesyncd network time service.

Prerequisites

Before starting this tutorial, you will need a Debian 9 server with a non-root, sudo-enabled user, as described in this Debian 9 server setup tutorial.

Navigating Basic Time Commands

The most basic command for finding out the time on your server is date. Any user can type this command to print out the date and time:

date

OutputTue Sep  4 17:51:49 UTC 2018

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 timedatectlcommand to do so.

First, list the available time zones:

timedatectl list-timezones

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:

sudo timedatectl set-timezone America/New_York

You can verify your changes by running date again:

date

OutputTue Sep  4 13:52:57 EDT 2018

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.

Checking the Status of ntpd

By default, Debian 9 runs the standard ntpd server to keep your system time synchronized with a pool of external time servers. We can check that it's running with the systemctl command:

sudo systemctl status ntp

Output● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; generated; vendor preset: enabled)
   Active: active (running) since Tue 2018-09-04 15:07:03 EDT; 30min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 876 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/ntp.service
           └─904 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 105:109
. . .

The active (running) status indicates that ntpd started up properly. To get more information about the status of ntpd we can use the ntpq command:

ntpq -p

Output     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 1.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 2.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 3.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
-eterna.binary.n 204.9.54.119     2 u  240  256  377   35.392    0.142   0.211
-static-96-244-9 192.168.10.254   2 u   60  256  377   10.242    1.297   2.412
+minime.fdf.net  83.157.230.212   3 u   99  256  377   24.042    0.128   0.250
*t1.time.bf1.yah 98.139.133.62    2 u   31  256  377   11.112    0.621   0.186
+x.ns.gin.ntt.ne 249.224.99.213   2 u  108  256  377    1.290   -0.073   0.132
-ord1.m-d.net    142.66.101.13    2 u  473  512  377   19.930   -1.764   0.293

ntpq is a query tool for ntpd. The -p flag asks for information about the NTP servers (or peers) ntpd is connected to. Your output will be slightly different, but should list the default Debian pool servers plus a few others. Bear in mind that it can take a few minutes for ntpd to establish connections.

Switching to systemd-timesyncd

It is possible to use systemd's built-in timesyncd component to replace ntpd. timesyncd is a lighter-weight alternative to ntpd that is more integrated with systemd. Note however that it doesn't support running as a time server, and it is slightly less sophisticated in the techniques it uses to keep your system time in sync. If you are running complex real-time distributed systems, you may want to stick with ntpd.

To use timesyncd, we must first uninstall ntpd:

sudo apt purge ntp

Then, start up the timesyncd service:

sudo systemctl start systemd-timesyncd

Finally, check the status of the service to make sure it's running:

sudo systemctl status systemd-timesyncd

Output● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Tue 2018-09-04 16:14:23 EDT; 1s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 3399 (systemd-timesyn)
   Status: "Synchronized to time server 198.60.22.240:123 (0.debian.pool.ntp.org)."
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/systemd-timesyncd.service
           └─3399 /lib/systemd/systemd-timesyncd

We can use timedatectl to print out systemd's current understanding of the time:

timedatectl

Output      Local time: Tue 2018-09-04 16:15:34 EDT
  Universal time: Tue 2018-09-04 20:15:34 UTC
        RTC time: Tue 2018-09-04 20:15:33
       Time zone: America/New_York (EDT, -0400)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

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.

Conclusion

In this article we’ve shown how to view the system time, change time zones, work with ntpd, and switch to systemd's timesyncd service. If you have more sophisticated timekeeping needs than what we’ve covered here, you might refer to 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.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,336评论 0 10
  • 一 如洗的碧空与苍茫的草海相连,竟让人分不清哪里是开始,哪里又是尽头。 万籁俱寂。 间或几声长鹰呼啸,似乎也为叨扰...
    阿一沐阅读 234评论 0 2
  • 杀人游戏开始了,准备好活到最后了吗?法官、杀手、警察、平民、恶女,究竟鹿死谁手? 01第七人 “天黑请闭眼。” 温...
    颜人青阅读 1,273评论 3 15
  • 小隐: 90后,河南姑娘,生活在江南。豆瓣专栏作者,中国散文学会会员,苏州吴中区作协会员,善古筝,少年习画,自由写...
    爱行走的90后大叔阅读 2,245评论 53 59
  • 越来越不会说话了,也越来越不喜欢说话了。 其实并不是不愿意张嘴,而是因为没有愿意支起的耳朵。 话总是要说给愿意的人...
    墩琦儿阅读 167评论 0 1