# 校時與時區設定

#### Tutorials

- [How to Sync Linux Time With NTP Server - Make Tech Easier](https://www.maketecheasier.com/sync-linux-time-with-ntp-server/)

#### Chrony

Built-in on CentOS/RedHat

- [CHAPTER 18. CONFIGURING NTP USING THE CHRONY SUITE](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-configuring_ntp_using_the_chrony_suite)
- [CentOS / RHEL 7 : Configuring NTP using chrony](https://www.thegeekdiary.com/centos-rhel-7-configuring-ntp-using-chrony/)
- [How do I check Linux system’s current NTP configuration](https://www.nixcraft.com/t/how-do-i-check-linux-systems-current-ntp-configuration/3519)

Install

```
yum install chrony
```

Config: /etc/chrony.conf

```
# NTP Public Servers for Taiwan
server tw.pool.ntp.org
server time.stdtime.gov.tw
server clock.stdtime.gov.tw
server jp.pool.ntp.org

# Allow NTP client access from local network.
#allow 192.168.0.0/16
```

Starting

```
systemctl start chronyd
```

Verify

```
[root@localhost ~]# chronyc tracking
Reference ID    : 7A75FDF6 (static.home.twn.sciuridae.cloud)
Stratum         : 3
Ref time (UTC)  : Sun Aug 16 03:35:36 2020
System time     : 0.000073180 seconds slow of NTP time
Last offset     : -0.001259724 seconds
RMS offset      : 0.001259724 seconds
Frequency       : 21.057 ppm slow
Residual freq   : -0.000 ppm
Skew            : 28.166 ppm
Root delay      : 0.032856792 seconds
Root dispersion : 0.002069760 seconds
Update interval : 64.3 seconds
Leap status     : Normal    <===

[root@tpemimtst99 ~]# timedatectl
               Local time: Tue 2021-05-25 09:09:09 CST
           Universal time: Tue 2021-05-25 01:09:09 UTC
                 RTC time: Tue 2021-05-25 01:09:09
                Time zone: Asia/Taipei (CST, +0800)
System clock synchronized: yes    <===
              NTP service: active
          RTC in local TZ: no

[root@tpemimtst99 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.21.86                 3  10   377   754  +1543ns[+4123ns] +/-   63ms

```

#### systemd-timesyncd

Build-in on Ubuntu

- [Control your computer time and date with systemd](https://opensource.com/article/20/6/time-date-systemd)

Installation

```bash
sudo apt install systemd-timesyncd
```

The configuration file for systemd-timesyncd is `/etc/systemd/timesyncd.conf`.

```
[Time]
#NTP=
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
```

Specify the time server as follows

```
NTP=<your-time-server-ip>
```

Start timesync

```shell
systemctl enable systemd-timesyncd.service
systemctl start systemd-timesyncd.service
```

timedatectl

```shell
# See the information of the datetime
sudo timedatectl
               Local time: Mon 2021-11-01 14:37:09 UTC
           Universal time: Mon 2021-11-01 14:37:09 UTC
                 RTC time: Mon 2021-11-01 14:37:10    
                Time zone: UTC (UTC, +0000)           
System clock synchronized: yes                        
              NTP service: active   <====                    
          RTC in local TZ: no 

# List timezones supported
sudo timedatectl list-timezones

# Change the system’s timezone
sudo timedatectl set-timezone Asia/Taipei

# Check the status
sudo timedatectl timesync-status
       Server: 91.189.91.157 (ntp.ubuntu.com)   
Poll interval: 17min 4s (min: 32s; max 34min 8s)
         Leap: normal                           
      Version: 4                                
      Stratum: 2                                
    Reference: 11FD227B                         
    Precision: 1us (-24)                        
Root distance: 72.295ms (max: 5s)               
       Offset: +5.311ms                         
        Delay: 67.290ms                         
       Jitter: 2.211ms                          
 Packet count: 6                                
    Frequency: +17.421ppm               
```

#### ntpd

Install

```shell
# Ubuntu
sudo apt install ntp
```

`vi /etc/ntp.conf`

```
#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

# Added the local time server
server 192.168.21.86 prefer iburst
```

Restart the ntpd

```
# Ubuntu 16.04
systemctl stop ntp
systemctl start ntp

# Check the timeserver
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
+eterna.binary.n 216.218.192.202  2 u   32   64    3   45.986   -0.081   0.021
+pacific.latt.ne 193.187.181.6    3 u   30   64    3    8.205    1.151   0.026
-mis.wci.com     216.218.192.202  2 u   32   64    3   37.928    4.692   0.176
-europa.ellipse. 209.180.247.49   2 u   31   64    3   51.792    4.175   0.027
#38.229.56.9     172.16.21.35     2 u   30   64    3   68.917   -4.451   0.190
#time.richiemcin 97.183.206.88    2 u   29   64    3   69.281   -3.073   0.248
*t1.time.gq1.yah 208.71.46.33     2 u   30   64    3   31.630   -0.081   0.011
-lofn.fancube.co 220.181.254.66   2 u   30   64    3   37.871   -1.088   0.009
#2606:6680:8:1:: 107.46.198.112   2 u   26   64    3  198.284   66.201   0.061
-titan.crash-ove 129.7.1.66       2 u   30   64    3   32.708   -1.474   0.200
-2620:1d5:100:43 47.187.174.51    2 u   29   64    3   70.418    0.592   0.197
 2001:67c:1560:8 17.253.34.123    2 u   32   64    3  133.490   -2.818   0.030
#38.229.57.9     172.16.21.35     2 u   30   64    3  177.896    4.081   6.291
#ntp2i.versadns. 217.180.209.214  2 u   28   64    3   62.795   -2.897   0.017
-104.194.8.227   192.12.19.20     2 u   25   64    3    0.979   -0.737   0.029
-pacific.latt.ne 193.187.181.6    3 u   26   64    3    8.770    1.212   0.050
 2001:67c:1560:8 17.253.34.123    2 u   33   64    3  133.602   -2.792   0.007

```

#### Change Timezone  


##### RedHat 7+

```bash
timedatectl
timedatectl list-timezones
timedatectl set-timezone Asia/Taipei
```

##### CentOS 6  


```bash
# List of timezone
ls /usr/share/zoneinfo

# Check the current timezone set
cat /etc/sysconfig/clock
```

Edit: /etc/sysconfig/clock

```
ZONE="Asia/Taipei"
UTC=true
```

Localtime

```bash
mv /etc/localtime /etc/localtime.bak
ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
```

##### Debian 13

```bash
sudo timedatectl set-timezone Asia/Taipei
```

#### Manually Set the date-time

```bash
date -s "6 April 2023 15:11:00"
```