NTP Time sync between computers - githeim/windheim_archive GitHub Wiki

Time sync between computers

Like a robot system, it can contain multiple processors in it. In that case the time sync should be achieved to communicate each others. In this page, introduce how to get time sync between multiple processors by using NTP server.

What we do here

  • The master connects to NTP server through internet, master system will synchronize to the internet time.
  • In the system there are master and slave(s). Slaves synchronize to the master.

Install NTP server

$ sudo apt install ntp  
$ sudo apt install ntpdate

Version check of NTP

$ sntp --version
sntp [email protected] (1)

NTP server(master) settings

edit /etc/ntp.conf

$ sudo vim /etc/ntp.conf

in the /etc/ntp.conf
...
# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Additional servers
server 0.kr.pool.ntp.org
server 3.asia.pool.ntp.org
server 0.asia.pool.ntp.org

server 127.127.1.0 minpoll 3 maxpoll 3 prefer
fudge 127.127.1.0 stratum 10
...

์œ„์—์„œ

server 127.127.1.0 minpoll 3 maxpoll 3 prefer

mipoll 3 / maxpoll 3 ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์ง€ ์•Š์œผ๋ฉด client๊ฐ€ ๋™๊ธฐํ™”ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค. ๋กœ๋ด‡๊ณผ ๊ฐ™์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ๋ถ€ํŒ…์‹œ๊ฐ„์ด ๋Š˜์–ด๋‚˜๋Š”๋“ฑ์˜ ๋‹จ์ ์ด ์žˆ์œผ๋‹ˆ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

NTP server(slave) settings

$ sudo vim /etc/ntp.conf

in the /etc/ntp.conf
...
# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 192.168.10.100 prefer iburst   # <---your master IP
or
server 192.168.10.100 minpoll 3 maxpoll 3 iburst # <---your master IP

minpoll / maxpoll ์— ๋Œ€ํ•ด์„œ๋Š” ์œ„์—์„œ๋Š” 2^3 = 8์ดˆ ์ด๋‚ด๋กœ polling ์„ ์‹œ๋„ํ•ด ๋™๊ธฐํ™”๋ฅผ ๋งž์ถ˜๋‹ค. ntp์—์„œ๋Š” 3๊ฐ’์ด ์ตœ์†Œ๊ฐ’.(chrony์—์„œ๋Š” ์ข€ ๋” ์ž‘์€ ๊ฐ’ ๊ฐ€๋Šฅ)

man page-ntp.conf

๋งŒ์•ฝ slave๊ฐ€ ์ธํ„ฐ๋„ท์ด ์•ˆ๋˜๊ณ  master์—๋งŒ ์˜์กดํ•  ๊ฒฝ์šฐ ntp.conf์˜ pool ํ•ญ๋ชฉ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์„์ฒ˜๋ฆฌํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ master์™€ ๋™๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค

# pool 0.ubuntu.pool.ntp.org iburst
# pool 1.ubuntu.pool.ntp.org iburst
# pool 2.ubuntu.pool.ntp.org iburst
# pool 3.ubuntu.pool.ntp.org iburst
# 
# # Use Ubuntu's ntp server as a fallback.
# pool ntp.ubuntu.com

check the delay & offset

in the slave console

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+xxx.aaa.com     133.243.238.164  2 u  565 1024  377   21.171    1.178   1.587
-yyy.yyy.yyy.yyy 220.73.142.66    3 u  545 1024  377   27.889   -0.713   1.114
*xxx.xxx.xxx.xxx 17.253.114.253   2 u  996 1024  277   48.699   11.319   4.876

you can check the delay / offset /jitter the * mark in the first raw is the master ntp server

check the ntp pool servers for your time zone

https://support.ntp.org/bin/view/Servers/NTPPoolServers

https://www.pool.ntp.org/zone/asia

In case of (south) Korea

https://www.pool.ntp.org/zone/kr

restart & check ntp server state

$ sudo systemctl restart ntp
$ sudo systemctl status ntp
โ— ntp.service - Network Time Service
     Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-08-29 10:22:53 KST; 6s ago
       Docs: man:ntpd(8)
    Process: 8089 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
   Main PID: 8097 (ntpd)
      Tasks: 2 (limit: 36425)
     Memory: 804.0K
     CGroup: /system.slice/ntp.service
             โ””โ”€8097 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 116:134

Aug 29 10:22:53 udr-62149 ntpd[8097]: Listen normally on 5 eth0 [fe80::f58:6b2c:7525:4d29%3]:123
Aug 29 10:22:53 udr-62149 ntpd[8097]: Listening on routing socket on fd #22 for interface updates
Aug 29 10:22:53 udr-62149 ntpd[8097]: kernel reports TIME_ERROR: 0x2041: Clock Unsynchronized
Aug 29 10:22:53 udr-62149 ntpd[8097]: kernel reports TIME_ERROR: 0x2041: Clock Unsynchronized
Aug 29 10:22:54 udr-62149 ntpd[8097]: Soliciting pool server 185.125.190.57
Aug 29 10:22:55 udr-62149 ntpd[8097]: Soliciting pool server 91.189.91.157
Aug 29 10:22:56 udr-62149 ntpd[8097]: Soliciting pool server 91.189.94.4
Aug 29 10:22:57 udr-62149 ntpd[8097]: Soliciting pool server 185.125.190.58
Aug 29 10:22:58 udr-62149 ntpd[8097]: Soliciting pool server 185.125.190.56
Aug 29 10:22:59 udr-62149 ntpd[8097]: Soliciting pool server 2620:2d:4000:1::40
$ 

How to sync ntp immediately

sudo service ntp stop
sudo ntpd -gq
sudo service ntp start