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.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子庄撮,更是在濱河造成了極大的恐慌,老刑警劉巖韭畸,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件笛质,死亡現(xiàn)場(chǎng)離奇詭異炼邀,居然都是意外死亡陆错,警方通過(guò)查閱死者的電腦和手機(jī)灯抛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)音瓷,“玉大人对嚼,你說(shuō)我怎么就攤上這事∩鳎” “怎么了纵竖?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵漠烧,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我靡砌,道長(zhǎng)已脓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任通殃,我火速辦了婚禮摆舟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘邓了。我一直安慰自己,他們只是感情好媳瞪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布骗炉。 她就那樣靜靜地躺著,像睡著了一般蛇受。 火紅的嫁衣襯著肌膚如雪句葵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天兢仰,我揣著相機(jī)與錄音乍丈,去河邊找鬼。 笑死把将,一個(gè)胖子當(dāng)著我的面吹牛轻专,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播察蹲,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼请垛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了洽议?” 一聲冷哼從身側(cè)響起宗收,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎亚兄,沒(méi)想到半個(gè)月后混稽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡审胚,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年匈勋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片菲盾。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡颓影,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出懒鉴,到底是詐尸還是另有隱情诡挂,我是刑警寧澤碎浇,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站璃俗,受9級(jí)特大地震影響奴璃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜城豁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一苟穆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧唱星,春花似錦雳旅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至哎榴,卻和暖如春型豁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尚蝌。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工迎变, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人飘言。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓衣形,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親姿鸿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子泵喘,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評(píng)論 0 10
  • 一 如洗的碧空與蒼茫的草海相連,竟讓人分不清哪里是開始般妙,哪里又是盡頭纪铺。 萬(wàn)籟俱寂。 間或幾聲長(zhǎng)鷹呼嘯碟渺,似乎也為叨擾...
    阿一沐閱讀 234評(píng)論 0 2
  • 殺人游戲開始了鲜锚,準(zhǔn)備好活到最后了嗎?法官苫拍、殺手芜繁、警察、平民绒极、惡女骏令,究竟鹿死誰(shuí)手? 01第七人 “天黑請(qǐng)閉眼垄提±拼” 溫...
    顏人青閱讀 1,273評(píng)論 3 15
  • 小隱: 90后凰兑,河南姑娘妥粟,生活在江南。豆瓣專欄作者吏够,中國(guó)散文學(xué)會(huì)會(huì)員勾给,蘇州吳中區(qū)作協(xié)會(huì)員,善古箏锅知,少年習(xí)畫播急,自由寫...
    愛(ài)行走的90后大叔閱讀 2,245評(píng)論 53 59
  • 越來(lái)越不會(huì)說(shuō)話了,也越來(lái)越不喜歡說(shuō)話了售睹。 其實(shí)并不是不愿意張嘴旅择,而是因?yàn)闆](méi)有愿意支起的耳朵。 話總是要說(shuō)給愿意的人...
    墩琦兒閱讀 167評(píng)論 0 1