一、安装
yum install -y ntp
二、配置
服务端:
编辑/etc/ntp.conf,加入以下内容:
# prefer表示优先同步此时间服务器,下面可以加入其它时间服务器 server time.nist.gov prefer server pool.ntp.org # 允许同网段的服务器同步时间 restrict 192.168.1.0 mask 255.255.255.0 nomodify # 禁止其它IP同步时间 restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap noquery
service ntpd restart
chkconfig --level 3 ntpd on
netstat -an |grep 123(服务成功启动后会在UDP123端口监听)
客户端:
*/10 * * * * /usr/sbin/ntpdate 192.168.1.111 (每10分钟同步服务器,初次同步需等待10分钟左右)
三、工具
检查时间服务器同步的状态命令:
ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
LOCAL(0) .LOCL. 10 l 39 64 377 0.000 0.000 0.001
*time.nist.gov .ACTS. 1 u 122 128 377 223.517 13.477 2.158
ntpstat
synchronised to NTP server (192.43.244.18) at stratum 2
time correct to within 22 ms
polling server every 128 s
客户端排错模式
ntpdate -d 192.168.1.111
22 Dec 15:31:49 ntpdate[29632]: ntpdate [email protected] Tue May 19 13:58:06 UTC 2009 (1)
Looking for host 192.168.1.111 and service ntp
host found : 192.168.1.111
transmit(192.168.1.111)
receive(192.168.1.111)
transmit(192.168.1.111)
receive(192.168.1.111)
transmit(192.168.1.111)
receive(192.168.1.111)
transmit(192.168.1.111)
receive(192.168.1.111)
transmit(192.168.1.111)
server 192.168.1.111, port 123
stratum 2, precision -20, leap 00, trust 000
refid [192.168.1.111], delay 0.02577, dispersion 0.00000
transmitted 4, in filter 4
reference time: d0bc25d8.d1a66104 Wed, Dec 22 2010 15:23:04.818
originate timestamp: d0bc27e5.f845e9aa Wed, Dec 22 2010 15:31:49.969
transmit timestamp: d0bc27e5.f7325918 Wed, Dec 22 2010 15:31:49.965
filter delay: 0.02580 0.02577 0.02577 0.02577
0.00000 0.00000 0.00000 0.00000
filter offset: 0.004133 0.004121 0.004121 0.004121
0.000000 0.000000 0.000000 0.000000
delay 0.02577, dispersion 0.00000
offset 0.004121
22 Dec 15:31:49 ntpdate[29632]: adjust time server 192.168.1.111 offset 0.004121 sec
ntpdate修改时间的方式为跃变,在数据库等要求时间为线性的应用场合下可能会发对应用造成影响,可以考虑使用ntpd。