NTP是最长用的也是最基本的服务了,很多加密应用都依赖此服务,比如:open***、puppet、saltstack等都需要首先同步好时间,所以把搭建NTPserver总结下,方便以后自己查询。
首先下载ntp server源码包:ntp-4.2.6p5.tar,如附件。
解压,进入ntp-4.2.6p5目录开始安装,命令如下:
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make && make install
安装完成后直接配置/etc/ntp.conf 是ntp这个包提供的配置文件,配置如下:
driftfile /var/lib/ntp/drift
#restrict default kod nomodify notrap nopeer noquery
restrict 58.55.127.0 mask 255.255.255.0 nomodify
restrict 221.235.188.0 mask 255.255.255.0 nomodify
restrict default nomodify notrap noquery
restrict -6 default kod 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 -6 ::1
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
修改配置文件:
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.pool.ntp.org
#server 0.centos.pool.ntp.org
#server 1.centos.pool.ntp.org
#server 2.centos.pool.ntp.org
配置完成后开启ntp服务:
[root@box logs]# /etc/init.d/ntpd restart
Shutting down ntpd: [FAILED]
Starting ntpd: [ OK ]
查看服务是否正常运行:
root@box logs]# netstat -lntup |grep ntp
udp 0 0 192.168.2.137:123 0.0.0.0:* 14010/ntpd
udp 0 0 58.55.127.137:123 0.0.0.0:* 14010/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 14010/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 14010/ntpd
udp 0 0 fe80::2a0:d1ff:feea:bd15:123 :::* 14010/ntpd
udp 0 0 fe80::2a0:d1ff:feea:bd14:123 :::* 14010/ntpd
udp 0 0 ::1:123 :::* 14010/ntpd
udp 0 0 :::123 :::* 14010/ntpd
查看NTP状态是否同步上层NTP服务器成功:
[root@box logs]# ntpstat
synchronised to NTP server (83.137.98.96) at stratum 3
time correct to within 499 ms
polling server every 64 s
看到synchronised to NTP server,说明已经同步成功了,
查看NTP Server时间是否正确:
[root@box logs]# date
Thu Sep 5 11:03:33 CST 2013
如果显示如下:
[root@box logs]# ntpstat
unsynchronised
polling server every 16 s
说明未同步成功,请检查配置是否修改正确。
还可以用这个命令查看;
[root@box logs]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
spacys.de 130.133.1.10 2 u 35 64 3 346.738 -7.946 0.536
blueshift.trevo 209.51.161.238 2 u 56 64 1 252.679 0.387 0.000
jaded.fsck.ca 132.163.4.103 2 u 45 64 1 286.477 -37.529 0.000
每项含义可以参照:
http://baike.baidu.com/view/5874764.htm
PS:
每次重启NTP 服务器之后大约要3-5 分钟客户端才能与server 建立正常的通讯
切记每次修改了配置文件后都需要重新启动服务来使配置生效。
命令行修改时区三步:
[root@localhost ~]# vi /etc/sysconfig/clock
ZONE="Asia/Shanghai"
[root@localhost ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite `/etc/localtime'? y
[root@localhost ~]# date
Thu Sep 5 19:33:12 CST 2013
最后crontab中添加:
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
Ntpdate –u +ip 同步时间。
原文地址: http://www.myjishu.com/?p=172