centos7局域網同步時間ntp

1、簡介 
本文主要對ntp服務在局域網內的安裝和配置做了簡要的介紹。


2、服務器端安裝
tar zxvf ntp-4.2.6p5.tar.gz
cd ntp-4.2.6p5
./configure --prefix=/usr/ntp --enable-all-clocks --enable-parse-clocks
make
make install
該部分是在時間同步服務中充當服務器的機器上要進行的安裝,其他機器將會行這臺機器上獲取時間,實現同步。


3、服務器端配置
(1) 允許客戶機的配置
修改ntp.conf配置文件
vi /etc/ntp.conf


①、第一種配置:允許任何IP的客戶機都可以進行時間同步
將“restrict default kod nomodify notrap nopeer noquery”這行修改成:
restrict default nomodify


②、第二種配置:只允許10.15.62.*網段的客戶機進行時間同步
在restrict default nomodify notrap noquery(表示默認拒絕所有IP的時間同步)之後增加一行:
restrict 10.15.62.0 mask 255.255.255.0 nomodify     


(2) 同步時間設置
①通過網絡同步時間
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org


②使用本地時間
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
server 127.127.1.0     # local clock
fudge  127.127.1.0 stratum 10


4、啓動ntp服務器
(1)通過bin目錄下的腳本啓動
/usr/local/ntp/bin/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid


(2)通過service工具啓動
service ntpd status
service ntpd start
service ntpd stop


5、客戶端配置
(1)安裝ntpdate
tar zxvf ntpdate.tar.gz
然後把解壓出的ntpdate可執行腳本拷貝到/usr/bin目錄或者是其他系統自動查詢的可執行程序的目錄即可。
(2)配置
vim /etc/crontab
如下配置成每小時同步一次
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
   1  *  *  *  * root ntpdate 10.15.62.70 && hwclock -w


6、常見問題
(1)no server suitable for synchronization found
在ntp服務器啓動之後需要一段時間才能夠通過客戶端進行時間的同步,所以如果這個問題是在服務器端剛啓動的時候出現的是屬於正常的情況,請等待一段時間在進行嘗試。如果還是有問題可以查看網絡或者是服務器端與客戶端的配置情況。
(2)端口占用
在使用ntpdate進行同步時如果發現NTP端口被佔用的提示,請使用ps命令查看系統是不是運行着ntp相關的服務,很多服務器安裝上之後會自動的啓動一些服務,之用將其關閉就可以了。
(3)防火牆和selinux問題
這個問題往往影響到服務器端和客戶端正常連接,在發現連接問題時,請首先考慮這兩個方面。

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