為什麼需要時間同步?
如果web服務沒有時間同步很有可能會出一些莫名奇妙的的問題,比如我就曾遇到過一則案例,伺服器剛剛申請完CA憑證,但是在用戶端通路時卻提示證書過期。
由其在虛拟化的環境當中,虛拟化的系統的CPU是虛拟的,時鐘震蕩不精确,為什麼會不精确呢?因為虛拟化的CPU并不是真正的CPU,就容易産生誤差。
什麼時候需要時間同步?
為了避免後期維護起來麻煩,我們最好在安裝系統的時候就做好時間同步。
常用的時間同步服務有:ntp和chrony,centos7以上都采用chrony,主要是因為其比較精确。無論用戶端還是伺服器都要安裝chrony做為背景程序。
做用戶端時
//#預設都配置好了時間伺服器,隻要啟動服務,并且網絡通配,chrony就可自行向配置檔案裡面指向的時間伺服器同步時間,預設指向centos的官方時間伺服器。
yum install chrony
rpm -qc chrony
/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
//預設是偵聽udp323端口
[root@tomcat2 ~]# systemctl start chronyd
[root@tomcat2 ~]# systemctl enable chronyd
[root@tomcat2 ~]# ss -unlp | grep 323
UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=8572,fd=5))
UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=8572,fd=6))
[root@tomcat2 ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? sv1.ggsrv.de 2 6 1 3 -44ms[ -44ms] +/- 132ms
^* time5.aliyun.com 2 6 77 4 +215us[+1227us] +/- 10ms
^- ntp7.flashdance.cx 2 6 77 3 -2098us[-2098us] +/- 204ms
^- time.cloudflare.com 3 6 77 4 +11ms[ +11ms] +/- 124ms
做時間伺服器時
#allow 192.168.0.0/16