chrony服務配置


chrony軟件使用說明

  chrony簡介

  chrony是一個開源的自由軟件,它能保持系統時鍾與時間服務器(ntp)同步,讓時間保持精確。

  它由兩個程序組成:chrongd和chronyc。

  chronyd是一個后台運行的守護進程,用於調整內核運行的系統時鍾和時間服務器同步。

  它確定計算機增減時間的比率,並對此進行補償。

  chrony是CentOs7.x上自帶的時間同步軟件
  1. 安裝及配置

  • 本地服務器作為時間服務器同步時間
  • 本地服務器不同步互聯網時間服務器的時間
 1 #yum install chrony --RHEL7默認已安裝chrony,而沒有安裝ntpd。
 2 
 3 #systemctl status chronyd --查看chronyd服務狀態
 4 
 5 #systemct enable chronyd --開機啟動
 6 
 7 #vim /etc/chrony.conf --修改配置文件
 8 
 9 #server 0.rhel.pool.ntp.org iburst --注釋這4行,表示的是外網時間服務器的網址。
10 
11 #server 1.rhel.pool.ntp.org iburst 
12 
13 #server 1.rhel.pool.ntp.org iburst 
14 
15 #server 1.rhel.pool.ntp.org iburst 
16 
17  server 192.168.56.112 iburst --添加這一行,表示與本機同步時間
18 
19  local stratum 10 -->>該行注釋取消掉不然NTP synchronized: 為no 取消掉后變為 NTP synchronized:yes
20 
21 #Allow NTP client access from local network.
22 
23  allow 192.168.56.0/24 -->>允許哪些服務器或客戶端到這台時間服務器來同步時間。必須配置
24 
25 #systemctl restart chronyd -->>修改完配置文件后,重啟chrony服務
[root@chronyd ~]# netstat -antulp|grep chronyd  -->>查看時間服務器是否允許
udp 0 0 0.0.0.0:123 0.0.0.0:* 12505/chronyd 
udp 0 0 127.0.0.1:323 0.0.0.0:* 12505/chronyd 
udp6 0 0 ::1:323 :::* 12505/chronyd 
[root@chronyd ~]# ss -antulp|grep chronyd
udp UNCONN 0 0 *:123 *:* users:(("chronyd",pid=12505,fd=7))
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=12505,fd=5))
udp UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=12505,fd=6))

顯示系統當前日期和時間

[root@chronyd ~]#timedatectl 

Local time: 二 2019-11-12 11:06:46 CST
Universal time: 二 2019-11-12 03:06:46 UTC
RTC time: 二 2019-11-12 03:06:46
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

注意NTP synchronized值,只有時間服務器自己同步完成時間后,才能為其他服務器提供時間同步。

 設置日期和時間 

timedatectl set-time "YYYY-MM-DD HH:MM:SS"

timedatectl set-time "YYYY-MM-DD"

timedatectl set-time "HH:MM:SS"

查看所有可用的市區

1 [root@chronyd ~]# timedatectl list-timezones |grep Asia/Shanghai
2 Asia/Shanghai

 設置時區

 1 [root@chronyd ~]# timedatectl set-timezone Asia/Shanghai 

設置硬件時間

#硬件時間默認為UTC

1 [root@chronyd ~]# timedatectl set-local-rtc 1
2 [root@chronyd ~]# hwclock --systohc --localtime
3 #兩條命令效果等同

 啟用NTP時間同步

1 [root@chronyd ~]# timedatectl set-ntp yes
2 [root@chronyd ~]# timedatectl set-ntp true
1 #兩條命令效果等同
2 問題:ntp設置為yes,手動設置時間報錯。

查看時間源信息

 1 [root@chronyd ~]# chronyc sources -v
 2 210 Number of sources = 1
 3 
 4 .-- Source mode '^' = server, '=' = peer, '#' = local clock.
 5 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
 6 | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
 7 || .- xxxx [ yyyy ] +/- zzzz
 8 || Reachability register (octal) -. | xxxx = adjusted offset,
 9 || Log2(Polling interval) --. | | yyyy = measured offset,
10 || \ | | zzzz = estimated error.
11 || | | \
12 MS Name/IP address Stratum Poll Reach LastRx Last sample 
13 ===============================================================================
14 ^* chronyd 10 6 177 9 -15ns[ -15us] +/- 25us
15 
16 [root@chronyd ~]#

客戶端時間同步配置

#yum install chrony -y  -->>安裝時間服務器軟件

修改配置文件

1 [root@Client ~]# vim /etc/chrony.conf
2 
3 # Use public servers from the pool.ntp.org project.
4 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
5 #server 0.centos.pool.ntp.org iburst
6 #server 1.centos.pool.ntp.org iburst
7 #server 2.centos.pool.ntp.org iburst
8 #server 3.centos.pool.ntp.org iburst
9 server 192.168.56.112 iburst -->>添加該行,表示到這台服務器去同步時間。

重啟chrony服務,並設置開機自啟動

1 [root@Client ~]# systemctl restart chronyd.service -->>重啟chrony服務
2 [root@Client ~]# systemctl enable chronyd.service -->>設置開機自啟動模式

查看時間同步狀態

1 [root@Client ~]# timedatectl 
2 Local time: 四 2013-02-14 12:37:25 CST
3 Universal time: 四 2013-02-14 04:37:25 UTC
4 RTC time: 日 2019-05-12 04:24:01
5 Time zone: Asia/Shanghai (CST, +0800)
6 NTP enabled: no
7 NTP synchronized: yes -->>為yes表示已同步
8 RTC in local TZ: no
9 DST active: n/a

重啟chronyd服務后,需要過幾分鍾才會自動完成同步。

如果需要手動,可以使用下面的命令

[root@Client ~]# ntpdate 192.168.56.112
14 Feb 12:40:09 ntpdate[8038]: adjust time server 192.168.56.112 offset -0.000067 sec

也可以使用crontab -e定時任務同步時間

1 [root@Client ~]# crontab -l
2 */2 * * * * /usr/sbin/ntpdate 192.168.56.112  -->>創建腳本每個2分鍾來同步時間一次
 
         
1 如果同步失敗,有可能是時間服務器的時間為同步,即時間服務器NTP  synchronized 為no
2 
3 手動修改時間時,必須把NTP enable 設置為no
4 
5 如果把 NTP enable修改為yes時,表示開啟自動同步時間,此時,是不能手動修改時間的。如果為no時,表示關閉自動同步時間。

 

1 [root@chronyd ~]# timedatectl set-ntp true 
2 
3 [root@chronyd ~]# timedatectl set-time "2019-11-11 12:56:11"
4 Failed to set time: Automatic time synchronization is enabled

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM