Set NTP Server in varies linux
在各个linux发行版中设置NTP服务器

0x01 Fedora

chrony

Fedora use chrony, just edit /etc/chrony.conf;

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
#pool 2.fedora.pool.ntp.org iburst
server time.apple.com iburst

Then restart chronyd with systemd.

[root@crazy ~]# systemctl restart chronyd.service

Use chronyc to check sync status:

[root@crazy ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* hkhkg1-ntp-002.aapl>          3   6    37    32    +32us[ +451us] +/- 5801us
^? 106.55.184.199                2   6     3    38   -834us[ -414us] +/-   42ms

0x02 Ubuntu

systemd-timesyncd

Edit /etc/systemd/timesyncd.conf;

[Time]
NTP=time1.tencentyun.com

Then restart systemd-timesyncd;

root@crazy:~# systemctl restart systemd-timesyncd.service

Check sync status with timedatectl:

root@crazy:~# timedatectl timesync-status
       Server: 169.254.0.79 (time1.tencentyun.com)
Poll interval: 2min 8s (min: 32s; max 34min 8s)   
         Leap: normal                             
      Version: 4                                  
      Stratum: 2                                  
    Reference: 914B85C                            
    Precision: 1us (-25)                          
Root distance: 20.522ms (max: 5s)                 
       Offset: -129us                             
        Delay: 2.881ms                            
       Jitter: 49us                               
 Packet count: 2                                  
    Frequency: -1.536ppm                          

0x03 Alpine Linux

ntpd + crontab

Create /etc/periodic/daily/do-ntp for cron.

#!/bin/sh
ntpd -d -q -n -p time.apple.com

Test it in bash:

crazy:~# bash /etc/periodic/daily/do-ntp
ntpd: 'time.apple.com' is 2403:300:a0c:4000::1f2
ntpd: sending query to 2403:300:a0c:4000::1f2
ntpd: reply from 2403:300:a0c:4000::1f2: offset:+0.028085 delay:0.001039 status:0x24 strat:1 refid:0x004d4853 rootdelay:0.000000 reach:0x01
ntpd: sending query to 2403:300:a0c:4000::1f2
ntpd: reply from 2403:300:a0c:4000::1f2: offset:+0.028060 delay:0.001038 status:0x24 strat:1 refid:0x004d4853 rootdelay:0.000000 reach:0x03

Chrony

But ntpd not works well, so i switched to Chrony.

crazy:~# setup-ntp 
Which NTP client to run? ('busybox', 'openntpd', 'chrony' or 'none') [chrony] 
 * service chronyd added to runlevel default
 * Caching service dependencies ...     [ ok ]
 * Starting chronyd ...                 [ ok ]

Then edit /etc/chrony/chrony.conf as well.

pool time.apple.com iburst
#initstepslew 10 pool.ntp.org
driftfile /var/lib/chrony/chrony.drift
rtcsync
cmdport 0

Restart chrony service:

crazy:~# service chronyd restart
 * Stopping chronyd ...                 [ ok ]
 * Starting chronyd ...                 [ ok ]

Check chrony status:

crazy:~# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- ussjc2-ntp-001.aaplimg.c>     1   6    17    51   +108us[ +108us] +/- 3869us
^- uslax1-ntp-001.aaplimg.c>     1   6    17    52    +88us[  +88us] +/-  340us
^* uslax1-ntp-002.aaplimg.c>     1   6    17    52    +21us[  +17us] +/-  389us
crazy:~# chronyc tracking
Reference ID    : 11FD1AFD (uslax1-ntp-002.aaplimg.com)
Stratum         : 2
Ref time (UTC)  : Fri Mar 03 05:10:54 2023
System time     : 0.000000001 seconds fast of NTP time
Last offset     : -0.000004359 seconds
RMS offset      : 0.000004359 seconds
Frequency       : 34.442 ppm fast
Residual freq   : -4.737 ppm
Skew            : 46.485 ppm
Root delay      : 0.000530292 seconds
Root dispersion : 0.003259220 seconds
Update interval : 1.6 seconds
Leap status     : Normal

Test connection:

lax6:~# chronyd -Q -t 3 'server time.cloudflare.com iburst maxsamples 1'
2023-03-04T03:34:19Z chronyd version 4.3 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP -SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 -DEBUG)
2023-03-04T03:34:19Z Disabled control of system clock
2023-03-04T03:34:20Z System clock wrong by -0.002741 seconds (ignored)
2023-03-04T03:34:20Z chronyd exiting

参考资料:

  1. https://chrony.tuxfamily.org/doc/4.3/chronyc.html
  2. https://wiki.archlinuxcn.org/zh-hans/Systemd-timesyncd
  3. https://wiki.alpinelinux.org/wiki/Alpine_Linux:FAQ

最后修改于 Wed, 01 Mar 2023