NTP時間服務器配置

1. NTP簡介

NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的各個計算機時間同步的一種協議。它的用途是把計算機的時鐘同步到世界協調時UTC,其精度在局域網內可達0.1ms,在互聯網上絕大多數的地方其精度可以達到1-50ms。

NTP服務器就是利用NTP協議提供時間同步服務的。

2. NTP服務器安裝

# 系統自帶ntp
[root@m01 ~]# rpm -aq ntp
ntp-4.2.6p5-10.el6.centos.x86_64
#如果系統沒有ntp,就安裝
yum -y install ntp

3. 配置NTP服務

[root@m01 ~]# vim /etc/ntp.conf
#restrict default kod nomodify notrap nopeer noquery
restrict default nomodify
# nomodify客戶端可以同步
# 將默認時間同步源註釋改用可用源
#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 ntp1.aliyun.com
server time.nist.gov
#使用sed修改即可
sed -i 's@restrict default kod nomodify@#restrict default kod nomodify@g' /etc/ntp.conf
sed -i '8a restrict default nomodify' /etc/ntp.conf
sed -i '25a server ntp1.aliyun.com\nserver time.nist.gov' /etc/ntp.conf
sed -i 's@server 0.centos@#server 0.centos@g' /etc/ntp.conf
sed -i 's@server 1.centos@#server 1.centos@g' /etc/ntp.conf
sed -i 's@server 2.centos@#server 2.centos@g' /etc/ntp.conf
sed -i 's@server 3.centos@#server 3.centos@g' /etc/ntp.conf

4. 啓動NTP服務器

# 如果計劃任務有時間同步,先註釋,兩種用法會衝突。
[root@m01 ~]# crontab -l
#time sync by ersa at 2016-12-26
#*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
[root@m01 ~]# /etc/init.d/ntpd start
Starting ntpd: 
[root@m01 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+time5.aliyun.co 10.137.38.86     2 u  103  128  377    3.758   -1.650  33.961
*nist1-lnk.binar .ACTS.           1 u  121  128  377  227.922    1.217  26.958
[root@m01 ~]# ntpstat
synchronised to NTP server (216.229.0.179) at stratum 2 
   time correct to within 125 ms
   polling server every 128 s
[root@m01 ~]# ntpdate 10.0.0.61
11 Jan 18:35:10 ntpdate[4462]: the NTP socket is in use, exiting

5. NTP服務器開機啓動

[root@m01 ~]# chkconfig ntpd on
[root@m01 ~]# chkconfig --list ntpd
ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off

6. 客戶機時間同步

客戶機要等幾分鐘再與新啓動的ntp服務器進行時間同步,否則會提示no server suitable for synchronization found錯誤

#客戶端測試
[root@backup ~]# date -s "20161231"
Sat Dec 31 00:00:00 CST 
#172.16.1.61 是服務器 m01(10.0.0.61) 的內網地址
[root@backup ~]# ntpdate 172.16.1.61
11 Jan 18:39:56 ntpdate[3623]: step time server 172.16.1.61 offset 1017582.583577 sec
[root@backup ~]# date
Wed Jan 11 18:39:58 CST echo "#tim sync with m01(172.16.1.61) by ersa" >> /var/spool/cron/root
echo "*/5 * * * * /usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1" >> /var/spool/cron/root


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