怎樣設置 CentOS 8 的時區

最近更新時間 2020-11-24 19:44:51

時區是地球上的區域使用同一個時間定義。以前,人們通過觀察太陽的位置(時角)決定時間,這就使得不同經度的地方的時間有所不同(地方時)。1863年,首次使用時區的概念。時區通過設立一個區域的標準時間部分地解決了這個問題。

查看當前時區

timedatectl命令可以查看當前系統的時間和時區信息。如下所示,當前機器使用 Asia/Shanghai 東八區。

timedatectl
               Local time: 二 2020-11-24 19:16:17 CST
           Universal time: 二 2020-11-24 11:16:17 UTC
                 RTC time: 二 2020-11-24 11:16:18
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

系統時區位於 /etc/localtime 這是一個符號鏈接,鏈接到 /usr/share/zoneinfo 目錄下面的時區信息,如下所示。

ls -l /etc/localtime
lrwxrwxrwx. 1 root root 35 11月 28 2019 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

設置時區

設置時區前可以查看系統中支持的時區。使用 list-timezones 參數可以查看支持的時區列表,如下所示,列表可能比較長,可以通過 grep 查找需要設置的時區

ls -l /etc/localtime
...
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
...

設置時區為 America/Toronto,設置時區後運行 timedatectl 命令,顯示時區已經更改。

timedatectl set-timezone America/Toronto
timedatectl
               Local time: 二 2020-11-24 06:35:12 EST
           Universal time: 二 2020-11-24 11:35:12 UTC
                 RTC time: 二 2020-11-24 11:35:12
                Time zone: America/Toronto (EST, -0500)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

通過符號鏈接修改時區

如果系統不支持 timedatectl 命令,可以直接修改 /etc/localtime 鏈接修改時區,可以查看 /usr/share/zoneinfo 目錄下支持的時區信息。

修改系統時區為 America/Toronto。通過 date 命令查是否修改成功。

ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime
date
2020年 11月 24日 星期二 19:40:11 CST
rss_feed