最近服務器被迫關機了一次,突然想把時間同步服務搞一搞 (內網
環境,無法連接互聯網
)
為什么我要記錄這個文章?
因為這玩意很簡單,花了我幾乎一天的時間.
總是報錯,嘗試修改配置,繼續報錯,再我幾乎絕望,想吐槽 linux ntpd 垃圾的時候,它突然生效了,
只是因為加了一個配置interface listen 10.100.2.242
,也或許是因為剛好需要等待的時間到了.
我就不明白了,為什么網上都說要等幾分鍾才能生效呢? 我用ntpdate -d -v $IP
來同步,為什么給我
報錯,我還不知道該怎么修改.
活該 linux 這么難用.
ntpd 服務器配置(10.100.2.242)
vim ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
#restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 10.100.2.0 mask 255.255.255.0 nomodify notrap
# 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 iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 127.127.1.1 burst true prefer
#fudge 127.127.1.1 stratum 8
interface listen 10.100.2.242
#... 省略后面的了
關鍵的地方在於:
針對指定局域網網段是生效
restrict 10.100.2.0 mask 255.255.255.0 nomodify notrap
設置服務器自己作為單獨的一個ntpd 服務器來提供服務
server 127.127.1.1 burst true prefer
再上述配置增加后,還不生效,神奇的,我找到了下面的幾乎
無人提及配置,貌似生效了
interface listen 10.100.2.242
這個在man手冊都沒看到,怎么了?
fudge 127.127.1.1 stratum 0
fudge參數貌似不生效
ntpd --version
ntpd 4.2.6p5
根據 ntp.org 的記錄,最新版本是4.2.8pX 系列, 這應該是版本問題了;
ntp 客戶端配置 (10.100.2.243-249) 全部使用10.100.2.242
的時間配置
客戶端就簡單了,當測試ok,我就批量操作了:
yum install ntpd -y
sed -i 's/^server/#server/g' /etc/ntp.conf
line=`grep "#server" /etc/ntp.conf -n|tail -n 1|cut -d ':' -f 1`
if test -z "$line";then echo "not found server ";return;fi
sed -i -e "$line a server 10.100.2.242 prefer iburst" /etc/ntp.conf
systemctl enable ntpd||chkconfig --level 345 ntpd on
systemctl restart ntpd||service ntpd restart
ntpstat ; ntpq -p; ntpdate -d
date
ntpdate -v -d 10.100.2.242
date
終於生效了,開啟各種ntpd服務,最好不要用crontab
來寫ntpdate
的定時命令,ntpdate只適合手動運行.
后來又查了一下,原來windows的w32time
服務也是支持ntpd的, 既可以配置為ntpd server
,也可以作為ntp client
來
鏈接ntpd server
(不管是win還是linux的,通通支持),最關鍵的,配置很簡單,使用很簡單.
所以 linux 真好用.
REF: http://doc.ntp.org/
ntp.conf:很少有人提及的事
ntp的interface listen配置,綁定監聽