![]()
在 Linux 系統下的時間可以區分為兩個:
- 第一是 Hardware Clock: 這是由主機板所提供的 RTC (Real-time clock) 時間,他是由系統的硬體所提供的時間,因為也不是十分精準所以只要時間久了這個時鐘的精準就會出現問題了,所以會出現另外一種系統計時方式.
- 第二是系統時間: 由系統利用 Linux kernel 以及 timer interrupt 所計算出來的方式去計時,不過會因為系統的附載過大而造成過快或過慢的時間,所以還需要 NTP(Network Time Protocol 網路時間校正) 的借助去調整到正確的時間.
date
傳統的方式可以用指令 #date 來設定時間(系統時間).
[root@localhost ~]# date 080811082019 Thu Aug 8 11:08:00 CST 2019
看不懂吧!!!他相對應的數字所代表的時間順序 MMDDhhmmYYYY 如下:
- MM : 08月
- DD : 08日
- hh : 11時
- mm : 08分
- YYYY:2019年
另外一種格式.
[root@localhost ~]# date 08081108 Thu Aug 8 11:08:00 CST 2019
- MM : 08月
- DD : 08日
- hh : 11時
- mm : 08分
設定時間久久才一次,格式很難記,這時候我們可以透過 -s 後面 DATE STRING 參數格式就比較自由.
[root@localhost ~]# date -s "2019.08.08" date: invalid date '2019.08.08' [root@localhost ~]# date -s "2019-08-08" Thu Aug 8 00:00:00 CST 2019 [root@localhost ~]# date -s "2019/08/08" Thu Aug 8 00:00:00 CST 2019 [root@localhost ~]# date -s "11:08" Thu Aug 8 11:08:00 CST 2019 [root@localhost ~]# date -s "11:08:20" Thu Aug 8 11:08:20 CST 2019 [root@localhost ~]# date -s "2019-08-08 11:06:42" Thu Aug 8 11:06:42 CST 2019
顯示其他時區的時間.
[root@localhost ~]# TZ='Asia/Tokyo' date Thu Aug 8 12:30:35 JST 2019 [root@localhost ~]# TZ='Asia/Taipei' date Thu Aug 8 11:30:43 CST 2019
hwclock
通常我們設定好時間後會希望 Hardware Clock 一樣同步,不希望下次開機還要重新再設定,所以透過指令 # hwclock [-rw]
[root@localhost ~]# hwclock -w
- -r : (read) 讀出 BIOS 的時間參數
- -w : (write)將目前的系統時間寫入 BIOS
timedatectl
使用 systemd 的系統,如 CentOS 7 / CentOS 8 還可以使用 timedatectl 來設定與檢視日期時間.
[root@localhost ~]# timedatectl
Local time: Thu 2019-08-08 11:49:20 CST
Universal time: Thu 2019-08-08 03:49:20 UTC
RTC time: Thu 2019-08-08 03:49:20
Time zone: Asia/Taipei (CST, +0800)
NTP enabled: no
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
設定日期時間.
[root@localhost ~]# timedatectl set-time "2019-08-08 11:50:00"
設定時區 (TimeZone).
[root@localhost ~]# timedatectl list-timezones | grep -i taipei Asia/Taipei
[root@localhost ~]# timedatectl set-timezone "Asia/Taipei"
[root@localhost ~]# timedatectl
Local time: Thu 2019-08-08 11:55:49 CST
Universal time: Thu 2019-08-08 03:55:49 UTC
RTC time: Thu 2019-08-08 03:55:49
Time zone: Asia/Taipei (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
改時區需把 rsyslog – https://benjr.tw/34103 日誌 log 服務重啟,避免時間與 log 的時間不相同.
[root@localhost ~]# systemctl restart rsyslog
設定 NTP (Network Time Protocol) 網路校時.
[root@localhost ~]# timedatectl set-ntp no
[root@localhost ~]# timedatectl
Local time: Thu 2019-08-08 11:54:31 CST
Universal time: Thu 2019-08-08 03:54:31 UTC
RTC time: Thu 2019-08-08 03:53:38
Time zone: Asia/Taipei (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@localhost ~]# timedatectl set-ntp yes
[root@localhost ~]# timedatectl
Local time: Thu 2019-08-08 11:54:53 CST
Universal time: Thu 2019-08-08 03:54:53 UTC
RTC time: Thu 2019-08-08 03:54:00
Time zone: Asia/Taipei (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
其 NTP 服務是由 chronyd.service 所提供,請參考 https://benjr.tw/102661 .
[root@localhost ~]# systemctl status chronyd.service
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since 六 2020-02-15 16:57:54 CST; 6s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 2751 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 2745 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2749 (chronyd)
Tasks: 1
CGroup: /system.slice/chronyd.service
└─2749 /usr/sbin/chronyd
2月 15 16:57:54 localhost.localdomain systemd[1]: Starting NTP client/server...
2月 15 16:57:54 localhost.localdomain chronyd[2749]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK...UG)
2月 15 16:57:54 localhost.localdomain chronyd[2749]: Initial frequency -400.000 ppm
2月 15 16:57:54 localhost.localdomain systemd[1]: Started NTP client/server.
Hint: Some lines were ellipsized, use -l to show in full.
沒有解決問題,試試搜尋本站其他內容
One thought on “Linux command – date , hwclock & timedatectl”