一,centos8中默認使用chronyd來做時間服務
1,查看chronyd服務的狀態
[root@blog ~]# systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-01-10 10:03:49 CST; 2 months 19 days ago Docs: man:chronyd(8) man:chrony.conf(5) Main PID: 671 (chronyd) Tasks: 1 (limit: 26213) Memory: 2.8M CGroup: /system.slice/chronyd.service └─671 /usr/sbin/chronyd
2,如果找不到chronyd服務,可以用yum安裝:
[root@blog ~]# yum install chrony
3,查看chronyd服務是否設置為了自啟動
[root@blog ~]# systemctl is-enabled chronyd
enabled
如果chronyd服務啟動狀態是disabled,用下面的命令設置為自啟動
[root@blog ~]# systemctl enable chronyd Created symlink /etc/systemd/system/multi-user.target.wants/chronyd.service → /usr/lib/systemd/system/chronyd.service. [root@blog ~]# systemctl is-enabled chronyd enabled
4,用下面的命令啟動chronyd
[root@blog ~]# systemctl start chronyd
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看timedatectl所屬的包:
[root@blog ~]# whereis timedatectl timedatectl: /usr/bin/timedatectl /usr/share/man/man1/timedatectl.1.gz [root@blog ~]# rpm -qf /usr/bin/timedatectl systemd-239-13.el8_0.5.x86_64
默認情況下應該已安裝到centos系統內
三,timedatectl的用法例子:
1,查看時間的配置狀態
[root@blog ~]# timedatectl status Local time: Mon 2020-03-30 12:37:07 CST Universal time: Mon 2020-03-30 04:37:07 UTC RTC time: Mon 2020-03-30 12:37:06 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: yes
說明:只用 timedatectl 不加后面的status效果也是一樣
2,各時間項的說明:
1.Local time:電腦上的本地時間
2.Universal time:通用協調時(UTC):
整個地球分為二十四時區,每個時區都有自己的本地時間。
在國際無線電通信場合,為了統一起見,使用一個統一的時間,
稱為通用協調時(UTC, Universal Time Coordinated)
3.RTC time:RTC時間(實時時鍾(Real-Time Clock))
RTC(Real-Time Clock)或CMOS時間,一般在主板上靠電池供電,
服務器斷電后也會繼續運行。
僅保存日期時間數值,無法保存時區和夏令時設置
4.Time zone:時區
5. NTP service:NTP(網絡時間協議{Network Time Protocol})啟用
6.System clock synchronized:NTP同步情況
7.RTC in local TZ:RTC是否使用本地時間
3,列出所有可用的時區
[root@blog ~]# timedatectl list-timezones
4,修改時區
下面的例子中,我們修改時區為美國的紐約
[root@blog ~]# date Mon Mar 30 12:46:43 CST 2020 [root@blog ~]# timedatectl set-timezone America/New_York [root@blog ~]# date Mon Mar 30 00:47:01 EDT 2020 [root@blog ~]# timedatectl Local time: Mon 2020-03-30 00:47:14 EDT Universal time: Mon 2020-03-30 04:47:14 UTC RTC time: Mon 2020-03-30 00:47:14 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: yes
再改回上海:
[root@blog ~]# timedatectl set-timezone Asia/Shanghai [root@blog ~]# date Mon Mar 30 12:48:47 CST 2020 [root@blog ~]# timedatectl Local time: Mon 2020-03-30 12:48:49 CST Universal time: Mon 2020-03-30 04:48:49 UTC RTC time: Mon 2020-03-30 12:48:49 Time zone: Asia/Shanghai (CST, +0800)
因為ntp是生效狀態,所以修改完時區后時間立刻生效
5,設置是否啟用ntp
[root@blog ~]# timedatectl set-ntp no [root@blog ~]# timedatectl Local time: Mon 2020-03-30 12:51:22 CST Universal time: Mon 2020-03-30 04:51:22 UTC RTC time: Mon 2020-03-30 12:51:22 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: inactive
注意: NTP service: 的值變成了 inactive
而且它還把chronyd給關閉了:
[root@blog ~]# systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:chronyd(8)
再次啟用ntp
[root@blog ~]# timedatectl set-ntp yes [root@blog ~]# systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2020-03-30 12:51:45 CST; 2s ago Docs: man:chronyd(8)
...
[root@blog ~]# timedatectl Local time: Mon 2020-03-30 12:53:15 CST Universal time: Mon 2020-03-30 04:53:15 UTC RTC time: Mon 2020-03-30 12:53:15 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active
這個操作有點危險,因為會把chronyd服務關閉,大家操作時要注意
說明:目前的server基本上都會連接到公網,所以建議時間同步服務一定要打開
6,不連接到公網的情況下,可以手動修改日期時間
說明; set-time該命令同時更新系統時間和硬件時鍾
ntp服務(chronyd)打開的情況下,手動修改時間會提示失敗
看下面的例子:
[root@blog ~]# date Mon Mar 30 12:58:24 CST 2020 [root@blog ~]# timedatectl set-time 11:58:30 Failed to set time: NTP unit is active
我們先手動關閉chronyd服務再修改時間:
[root@blog ~]# systemctl stop chronyd [root@blog ~]# timedatectl Local time: Mon 2020-03-30 13:01:26 CST Universal time: Mon 2020-03-30 05:01:26 UTC RTC time: Mon 2020-03-30 13:01:26 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: inactive
此時timedatectl也會輸出NTP service的狀態為inactive
再次修改時間:
[root@blog ~]# date Mon Mar 30 13:02:50 CST 2020 [root@blog ~]# timedatectl set-time 11:02:30 [root@blog ~]# date Mon Mar 30 11:02:31 CST 2020 [root@blog ~]# timedatectl Local time: Mon 2020-03-30 11:02:36 CST Universal time: Mon 2020-03-30 03:02:36 UTC RTC time: Mon 2020-03-30 11:02:36 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: inactive RTC in local TZ: yes
此時的時間修改會成功
也可以同時修改日期時間:
[root@blog ~]# date Mon Mar 30 11:04:53 CST 2020 [root@blog ~]# timedatectl set-time '2020-03-30 12:04:30' [root@blog ~]# date Mon Mar 30 12:04:33 CST 2020
也可只修改日期:
[root@blog ~]# timedatectl set-time 2020-03-29 [root@blog ~]# date Sun Mar 29 00:00:02 CST 2020
上面的修改在啟動時間同步服務chronyd后會自動同步回來
[root@blog ~]# systemctl start chronyd [root@blog ~]# date Mon Mar 30 13:07:37 CST 2020
7,設置rtc是否使用本地時間或utc時間
[root@blog ~]# timedatectl Local time: Mon 2020-03-30 13:28:43 CST Universal time: Mon 2020-03-30 05:28:43 UTC RTC time: Sun 2020-03-29 00:21:50 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: yes Warning: The system is configured to read the RTC time in the local time zone. This mode cannot be fully supported. It will create various problems with time zone changes and daylight saving time adjustments. The RTC time is never updated, it relies on external facilities to maintain it. If at all possible, use RTC in UTC by calling 'timedatectl set-local-rtc 0'.
注意這個末尾的warning信息,系統建議rtc使用utc時間,
所以我們應該按這個要求去做
設置rtc使用utc時間
[root@blog ~]# timedatectl set-local-rtc 0 [root@blog ~]# timedatectl Local time: Mon 2020-03-30 13:29:10 CST Universal time: Mon 2020-03-30 05:29:10 UTC RTC time: Mon 2020-03-30 05:29:10 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
設置rtc使用本地時間
[root@blog ~]# timedatectl set-local-rtc 1 [root@blog ~]# timedatectl Local time: Mon 2020-03-30 13:29:25 CST Universal time: Mon 2020-03-30 05:29:25 UTC RTC time: Mon 2020-03-30 13:29:25 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: yes Warning: The system is configured to read the RTC time in the local time zone. This mode cannot be fully supported. It will create various problems with time zone changes and daylight saving time adjustments. The RTC time is never updated, it relies on external facilities to maintain it. If at all possible, use RTC in UTC by calling 'timedatectl set-local-rtc 0'.
警告信息又回來了,所以建議rtc使用utc時間
四,查看centos的版本:
[root@blog ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)