Centos6.5 配置NTP服務器

配置NTP-server

環境:

[root@LNMP ~]# cat/etc/redhat-release 
CentOS release 6.5 (Final)
[root@LNMP ~]# uname -r
2.6.32-431.el6.x86_64
[root@LNMP ~]# uname -m
x86_64

安裝NTP軟件:

[root@LNMP ~]# yum install -yntp

檢查遠程ntp服務器是否可用:

[root@LNMP ~]# ntpdate -q ntp5.aliyun.com
server 182.92.12.11, stratum2, offset -0.011808, delay 0.03224
20 Apr 21:15:37ntpdate[3429]: adjust time server 182.92.12.11 offset -0.011808 sec

配置ntp服務之前,先手動同步一下時間:

[root@LNMP ~]# ntpdate ntp5.aliyun.com

編輯NTP配置文件:(文件位置在/etc/ntp.conf,編輯前備份)

[root@LNMP ~]# cp/etc/ntp.conf /etc/ntp.conf.ori
[root@LNMP ~]# vim/etc/ntp.conf
restrict 192.168.22.0 mask255.255.255.0 nomodify notrap
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com
server ntp4.aliyun.com
server ntp5.aliyun.com
server ntp6.aliyun.com
server ntp7.aliyun.com
# UndisciplinedLocal Clock. This is a fake driver intended for backup
# and when nooutside source of synchronized time is available.
# 外部時間服務器不可用時,以本地時間作爲時間服務
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

把之前的參數註釋掉:

#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

檢查服務狀態:

[root@LNMP ~]# netstat-lntup|grep ntp
udp        0     0 192.168.22.134:123         0.0.0.0:*                              1186/ntpd           
udp        0     0 127.0.0.1:123              0.0.0.0:*                              1186/ntpd           
udp        0     0 0.0.0.0:123                 0.0.0.0:*                               1186/ntpd           
udp        0     0 fe80::20c:29ff:fe51:259b:123 :::*                                   1186/ntpd           
udp        0     0 ::1:123                    :::*                                    1186/ntpd           
udp        0     0 :::123                     :::*                                   1186/ntpd
[root@LNMP ~]# ntpq -p
     remote           refid      st t when poll reach   delay  offset  jitter
==============================================================================
*time5.aliyun.co10.137.38.86     2 u  181 256  377    9.293  -1.990   1.154
+120.25.115.19   10.137.38.86     2 u 138  256  377  45.598   -1.924   1.010
+120.25.115.20   10.137.38.86     2 u 157  256  377  45.795   -1.713   1.113
-time4.aliyun.co10.137.38.86     2 u  247 256  377   45.650  -5.136   2.278
-time6.aliyun.co10.137.38.86     2 u    6 256  377   74.053   8.728   3.008
 LOCAL(0)       .LOCL.          10 l 128m   64   0    0.000    0.000  0.000

ntpstat 命令查看時間同步狀態,這個一般需要5-10分鐘後才能成功連接和同步。所以,服務器啓動後需要稍等下。

[root@LNMP ~]# ntpstat
synchronised to NTP server(182.92.12.11) at stratum 3 
time correct to within 30 ms
polling server every 256 s

如果服務器當前時間和遠程ntp服務器時間相差很多或者服務器時間比遠程ntp服務器時間要快的話,那麼服務器是不會與遠程ntp服務器同步時間的

需要在ntp.conf中加上一行參數:

tinker panic 0

將同步的時間與硬件時鐘進行同步:

[root@LNMP ~]# vim /etc/sysconfig/ntpd
添加一行
SYNC_HWCLOCK="yes"
保存退出,重啓ntp服務

至此,NTP-server時間服務器配置完成!


配置內網NTP-client

首先需要安裝NTPD服務,然後配置爲自啓動(與NTP-Server完全一樣)。然後找其中一臺配置/etc/ntp.conf文件,配置完成驗證通過後,拷貝到其他客戶端機器,直接使用即可。

[root@Proxy ~]# grep -Ev '#'/etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict 127.0.0.1 
restrict -6 ::1
server 192.168.22.134
restrict 192.168.22.134nomodify notrap noquery
fudge 127.127.1.0 stratum 10
includefile/etc/ntp/crypto/pw
keys /etc/ntp/keys

將同步的時間與硬件時鐘進行同步:

[root@Proxy ~]# vim/etc/sysconfig/ntpd
添加一行:
SYNC_HWCLOCK="yes"
保存退出,重啓ntp服務。

檢查ntp服務狀態:

[root@Proxy ~]# netstat-lntup|grep ntp
udp        0     0 192.168.22.136:123         0.0.0.0:*                              2833/ntpd           
udp        0     0 127.0.0.1:123               0.0.0.0:*                               2833/ntpd           
udp        0     0 0.0.0.0:123                0.0.0.0:*                              2833/ntpd           
udp        0     0 fe80::20c:29ff:feff:3bf5:123 :::*                                    2833/ntpd           
udp        0     0 ::1:123                    :::*                                   2833/ntpd           
udp        0     0 :::123                     :::*                                   2833/ntpd
[root@Proxy ~]# ntpq -p
     remote           refid      st t when poll reach   delay  offset  jitter
==============================================================================
 192.168.22.134 173.255.246.13   3 u   20  64   17    0.218  24.195   0.084
*LOCAL(0)        .LOCL.          10 l  24   64   17   0.000    0.000   0.000
[root@Proxy ~]# ntpstat
synchronised to NTP server(192.168.22.134) at stratum 4 
time correct to within 53 ms
polling server every 128 s


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章