CentOS7上使用chronyc同步時間


實際生產環境中,服務器之間的時間是需要同步,但並不是所有機器可以直接連外網,這時可以用Chrony工具解決。

 解決方法是將其中一台設為時間服務器,然后其它服務器和這台時間服務器同步即可。具體步驟如下:

一. 配置時間服務器

Chrony時間服務器上: 

[root@node151 ~]# systemctl stop firewalld
[root@node151 ~]# systemctl disalbe firewalld
[root@node151 ~]# yum -y install chrony
[root@node151 ~]# vim /etc/chrony.conf
[root@node151 ~]# grep "^server" /etc/chrony.conf 
server 0.centos.pool.ntp.org iburst  #公網時間服務器,按需需修改
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
server node151  iburst    # 添加本機(node151為本機主機名,需要客戶端可解析,也可以使用ip)
[root@node151 ~]# grep -n  "^allow" /etc/chrony.conf   #允許其他節點可以連接
26:allow 192.168.1.0/24 
[root@node151 ~]# timedatectl set-timezone Asia/Shanghai #設置時區
[root@node151 ~]# systemctl enable chronyd.service  #啟動 NTP 服務並將其配置為隨系統啟動 
[root@node151 ~]# systemctl start chronyd.service 

其它節點:

[root@node152 ~]# systemctl stop firewalld
[root@node152 ~]# systemctl disable firewalld
[root@node152 ~]# yum -y install chrony
[root@node152 ~]# grep -n "^server" /etc/chrony.conf #編輯/etc/chrony.conf 文件並注釋所有內容。添加本地Chrony時間服務器
3:server node151 iburst
[root@node152 ~]# timedatectl set-timezone Asia/Shanghai #設置時區
[root@node152 ~]# systemctl enable chronyd.service #啟動 NTP 服務並將其配置為隨系統啟動
[root@node152 ~]# systemctl start chronyd.service

二、驗證操作:

Chrony時間服務器上執行這個命令:

[root@node151 ~]# chronyc sources 
210 Number of sources = 5
MS Name/IP address         Stratum Poll Reach LastRx Last sample       # Name/IP address 列的內容顯示NTP服務器的主機名或者IP地址         
===============================================================================
^- 61-216-153-106.HINET-IP.>     3   6    17    13  +1752us[-1466us] +/-   63ms
^- time5.aliyun.com              2   6    17    12   -925us[-4142us] +/-   16ms
^- ntp7.flashdance.cx            2   6    17    11    +15ms[  +15ms] +/-  194ms
^? dns1.synet.edu.cn             0   6     0     -     +0ns[   +0ns] +/-    0ns
^* node151          3   6    17     7    +14us[-7821ns] +/-   18ms

其他節點執行相同命令:

[root@node152 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* node151          4   6    17    23   -744us[-6012us] +/-   33ms

注意:chrony和ntpd類似firewalld和iptables,不能共存,同時只能存在一個服務運行。

其他一些時間同步命令:

查看日期時間及NTP狀態:# timedatectl
查看和配置時區:# timedatectl list-timezones;# timedatectl set-timezone Asia/Shanghai
修改日期時間:# timedatectl set-time "2015-01-21 11:50:00"(可以只修改其中一個)
開啟NTP:# timedatectl set-ntp true/flase
查看chrony服務所有conf配置文件分布# rpm -ql chrony |grep conf
檢查chrony服務配置文件所在# rpm -qc chrony
查看chrony安裝情況# rpm -qi chrony
檢查ntp安裝情況# yum search ntp |grep ^ntp.x86_64或# yum search ntp(列出所有ntp相關包)


免責聲明!

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



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