# 設定時區與 NTP 網路對時

## OpenWRT 中的時區設定

在 OpenWRT 中，關於功能的設定，都放在 `/etc/config` 資料夾之下:

```
/etc/config# ls
dhcp              glconfig          network           samba             system1           uhttpd            wireguard
dropbear          luci              openvpn           ssids             ubootenv          unbound           wireguard_server
firewall          mwan3             rpcd              system            ucitrack          vpn_service       wireless
```

其中， dropbear 是 OpenWRT 的 SSH server，fstab、mountd 負責檔案系統部分，dhcp、firewall 如其名稱所示，負責 IP 位置請求以及防火牆。network 為有線網路的設定，wireless 為無線網路的設定。system 為一些系統上的設定，例如所在時區。以 system 為例，原本內容為:

```
config system
        option timezone 'UTC'
        option ttylogin '0'
        option log_size '64'
        option urandom_seed '0'
        option hostname 'GL-AR750S'

config timeserver 'ntp'
        option enabled '1'
        option enable_server '0'
        list server '0.openwrt.pool.ntp.org'
        list server '1.openwrt.pool.ntp.org'
        list server '2.openwrt.pool.ntp.org'
        list server '3.openwrt.pool.ntp.org'

config led 'led_wlan2g'
        option name 'WLAN2G'
        option sysfs 'gl-ar750s:white:wlan2g'
        option trigger 'phy1tpt'

config led 'led_wlan5g'
        option name 'WLAN5G'
        option sysfs 'gl-ar750s:white:wlan5g'
        option trigger 'phy0tpt'
```

其中，設定了主機名稱 (hostname)，所在時區 (UTC)，以及 NTP (network time protocol) 對時的主機。至於最下方 'led' 的部分，則是用以控制 WiFi AP 的 LED 燈號變化。以此檔案為例，可以更改 option 'timezone' 'UTC' 為 'CST-8'，改成台灣時間。之後再重新啟動，就可以利用 `date` 指令看到時間的改變。

```
root@GL-AR750S:~# date
Wed Jan 30 05:31:45 UTC 2019
root@GL-AR750S:~# reboot
[...]
root@GL-AR750S:~# date
Wed Jan 30 13:36:36 CST 2019
```

更多關於 system 的設定, 可以參考: <https://wiki.openwrt.org/doc/uci/system>

## NTP 同步演算法

NTP 為一個基礎的網路對時演算法，簡單來說，該演算法透過一個來回的封包，來計算封包在網路中的偏移時間，並藉由估計此飄移時間來效正從 NTP 伺服器得到的標準時間。我們可以用下圖來表示 NTP 演算法對時的機制:

![來自: https://en.wikipedia.org/wiki/Network\_Time\_Protocol](/files/-LXSJNxoLZIyWXPscYcW)

其中，\delta 就是封包的飄移時間。NTP 演算法可以達到數十毫秒 (ms) 的同步誤差，但是在實務上由於網路的不對稱性 (上行和下行不一致) 以及網路壅塞 (在路由器中的封包延遲)，NTP 演算法誤差最多會到 100 毫秒等級。為了取得比較精確地同步時間，我們可以將同步伺服器設定到較近的位置，如上面設定中:

```
config timeserver 'ntp'
        option enabled '1'
        option enable_server '0'
        list server '0.openwrt.pool.ntp.org'
```

可以改成:

```
config timeserver 'ntp'
        option enabled '1'
        option enable_server '0'
        list server 'tw.pool.ntp.org'
```

甚至可以自行架設 NTP server 來取得較佳的同步成果，更多關於 NTP 的設定, 可以參考: <https://oldwiki.archive.openwrt.org/doc/howto/ntp.client>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openwrt-nctu.gitbook.io/project/openwrt-functions/openwrt-ntp-and-sync.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
