1. 程式人生 > >Linux的時間同步及命令

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] Desktop]# vim /etc/chrony.conf 
[[email protected] Desktop]# systemctl restart chronyd.service 

[[email protected] 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

注意:只有底下出現*號時間,同步時間成功。