一,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)