linux設定ntp時間同步
1.安裝ntp
ntp yum –y install ntp
2.設定ntp服務開機啟動
chkconfig ntpd on
3.將master節點設定為主伺服器(在master節點操作)
3.1. vi /etc/ntp.conf,內容如下:
driftfile /var/lib/ntp/ntp.drift #草稿檔案
# 允許內網其他機器同步時間
restrict 192.168.137.0(替換成自己的ip) mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# 中國這邊最活躍的時間伺服器 :
[http://www.pool.ntp.org/zone/cn](http://www.pool.ntp.org/zone/cn)
server 210.72.145.44 perfer # 中國國家受時中心
server 202.112.10.36 # 1.cn.pool.ntp.org
server 59.124.196.83 # 0.asia.pool.ntp.org
# allow update time by the upper server
# 允許上層時間伺服器主動修改本機時間
restrict 210.72.145.44 nomodify notrap noquery
restrict 202.112.10.36 nomodify notrap noquery
restrict 59.124.196.83 nomodify notrap noquery
# 外部時間伺服器不可用時,以本地時間作為時間服務
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
3.2 重啟服務:service ntpd restart
3.3檢視同步狀態:netstat -tlunp | grep ntp
4.設定slave到master的同步(slave節點操作)
4.1. vi /etc/ntp.conf,內容如下:
driftfile /var/lib/ntp/ntp.drift # 草稿檔案
statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# 讓NTP Server為內網的ntp伺服器
server 192.168.137.110 (master節點ip)
fudge 192.168.137.110 stratum 5
# 不允許來自公網上ipv4和ipv6客戶端的訪問
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
4.2重啟服務:service ntpd restart
4.3手動同步:ntpdate -u 192.168.137.110