Linux的时间同步及命令
一、timedatectl命令
1.timedatectl
[[email protected] ~]# timedatectl list-timezones ##查看所有时区
![]()
[[email protected] ~]# timedatectl set-timezone Asia/Shanghai ##设定世界地点为上海 [[email protected] ~]# 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时间(通用时间) [[email protected] ~]# timedatectl set-local-rtc 0 ##将硬件时间与国际标时间 [[email protected] ~]# vim /etc/adjtime [[email protected] ~]# timedatectl set-local-rtc 1 ##将硬件时间与本地时间同步 [[email protected] ~]# vim /etc/adjtime
![]()
![]()
![]()
[[email protected] ~]# timedatectl set-time "2017-9-9 11:11:11" ##设定时间 [[email protected] ~]# date Sat Sep 9 11:11:13 CST 2017 [[email protected] ~]# timedatectl set-time "2018-10-14 09:45" ##查看时间 [[email protected] ~]# date Sun Oct 14 09:45:01 CST 2018
二、服务端和客户端的时间同步
在服务端:
[[email protected] ~]# vim /etc/chrony.conf 22 allow 172.25.254.0/24 ##允许网络位相同的所有客户端来访问本机共享的时间 29 local stratum 10 ##共享时间共享功能,并设定共享级别为10,即以自己的时间戳为基准 [[email protected] ~]# systemctl restart chronyd [[email protected] ~]# systemctl stop firewalld
在客户端:
[[email protected] ~]# vim /etc/chrony.conf [[email protected] ~]# systemctl restart chronyd
[[email protected] ~]# 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 0 6 0 10y +0ns[ +0ns] +/- 0ns [[email protected] ~]# date Wed Oct 17 09:47:23 EDT 2018