一、timedatectl命令
1.timedatectl
[root@node1 ~]# timedatectl list-timezones ##查看所有时区
[root@foundation69 ~]# timedatectl set-timezone Asia/Shanghai ##设定世界地点为上海 [root@foundation69 ~]# timedatectl ##发现CST和utc时间不同 Local time: Wed 2018-10-17 21:56:32 CST ##本地时间 Universal time: Wed 2018-10-17 13:56:32 UTC ##国际标准时间 RTC time: Wed 2018-10-17 21:56:32 ##硬件时间 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: no RTC in local TZ: yes DST active: n/a
设定为UTC时间(通用时间) [root@node1 ~]# timedatectl set-local-rtc 0 ##将硬件时间与国际标时间 [root@node1 ~]# vim /etc/adjtime [root@node1 ~]# timedatectl set-local-rtc 1 ##将硬件时间与本地时间同步 [root@node1 ~]# vim /etc/adjtime
[root@node1 ~]# timedatectl set-time "2017-9-9 11:11:11" ##设定时间 [root@node1 ~]# date Sat Sep 9 11:11:13 CST 2017 [root@node1 ~]# timedatectl set-time "2018-10-14 09:45" ##查看时间 [root@node1 ~]# date Sun Oct 14 09:45:01 CST 2018
二、服务端和客户端的时间同步
在服务端:
[root@foundation69 ~]# vim /etc/chrony.conf 22 allow 172.25.254.0/24 ##允许网络位相同的所有客户端来访问本机共享的时间 29 local stratum 10 ##共享时间共享功能,并设定共享级别为10,即以自己的时间戳为基准 [root@foundation69 ~]# systemctl restart chronyd ##重新启动服务 [root@foundation69 ~]# systemctl stop firewalld ##关闭火墙
在客户端:
[root@node1 Desktop]# vim /etc/chrony.conf [root@node1 Desktop]# systemctl restart chronyd.service
[root@node1 Desktop]# chronyc sources -v 210 Number of sources = 1 .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined, | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. || .- xxxx [ yyyy ] +/- zzzz || / xxxx = adjusted offset, || Log2(Polling interval) -. | yyyy = measured offset, || \ | zzzz = estimated error. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 172.25.254.69 10 6 17 34 +138ns[-1000ns] +/- 961us
注意:只有底下出现*号时间,同步时间成功。