1. 程式人生 > >Centos7安裝搭建NTP伺服器和NTP客戶端同步時間

Centos7安裝搭建NTP伺服器和NTP客戶端同步時間

NTP簡介:

NTP是網路時間協議(Network Time Protocol),它是用來同步網路中各個計算機的時間的協議。 在計算機的世界裡,時間非常地重要 例如:對於火箭發射這種科研活動,對時間的統一性和準確性要求就非常地高,是按照A這臺計算機的時間,還是按照B這臺計算機的時間? NTP就是用來解決這個問題的,NTP(Network Time Protocol,網路時間協議)是用來使網路中的各個計算機時間同步的一種協議。 它的用途是把計算機的時鐘同步到世界協調時UTC,其精度在區域網內可達0.1ms,在網際網路上絕大多數的地方其精度可以達到1-50ms。 它可以使計算機對其伺服器或時鐘源(如石英鐘,GPS等等)進行時間同步,它可以提供高精準度的時間校正,而且可以使用加密確認的方式來防止病毒的協議攻擊。  

環境:

系統:CentOS Linux release 7.4.1708 (Core) 

NTP Server伺服器IP:192.168.0.15

NTP Client客戶端IP:192.168.0.16

 

1、搭建NTP伺服器

1.1、檢視伺服器是否安裝ntp,系統預設安裝ntpdate;

[[email protected] ~]# rpm -qa |grep ntp
ntpdate-4.2.6p5-28.el7.centos.x86_64
ntp-4.2.6p5-28.el7.centos.x86_64

1.2、安裝ntp,ntpdate已經系統預設安裝過了

[[email protected] ~]# yum install -y ntp

1.3、修改ntp配置檔案

[[email protected] ~]# vim /etc/ntp.conf

把配置檔案下面四行註釋掉:
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst
然後在下面新增這幾行:
server 0.cn.pool.ntp.org iburst
server 
1.cn.pool.ntp.org iburst server 2.cn.pool.ntp.org iburst server 3.cn.pool.ntp.org iburst

1.4、啟動ntp服務,並開機自啟動

[[email protected] ~]# systemctl start ntpd
[[email protected] ~]# systemctl enable ntpd

1.5、查詢ntp是否同步

[[email protected] ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*119.28.206.193  100.122.36.196   2 u  128  128  377   19.711   -0.468   5.363

1.6、開啟防火牆ntp預設埠udp123

[[email protected] ~]# firewall-cmd --permanent --zone=public --add-port=123/udp
success
[[email protected] ~]# firewall-cmd --reload
success

 

2、NTP客戶端配置

安裝的NTP跟上面的步驟一樣

2.1、修改ntp配置檔案,將上面的NTP伺服器作為客戶端同步NTP時間伺服器

[[email protected] ~]# vim /etc/ntp.conf
#配置允許NTP Server時間伺服器主動修改本機的時間
restrict 192.168.0.15 nomodify notrap noquery
#註釋掉其他時間伺服器
#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
#配置時間伺服器為本地搭建的NTP Server伺服器
server 192.168.0.15

2.2、與NTP server伺服器同步一下時間:

[[email protected] ~]# ntpdate -u 192.168.0.15

2.3、檢視ntp同步狀態

能看到已經成功同步,要記得開啟ntpd這個伺服器

[[email protected] ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.0.15  119.28.206.193   3 u    7   64    1    0.217  -288085   0.000