多机部署中时间的同步是很重要的,当然有人说了搞毛啊ntpdate命令直接同步不就完事了嘛。是的,这样的确可以达到同步时间的效果,但是会有一定的隐患,具体例子这里就不说明了,自行百度。下面就来介绍一下如何使用NTPD服务平滑同步时间。
检查是否安装NTPD服务
1
rpm -q ntp
有内容表示已经安装,如果没有,安装命令如下:
1
yum install -y ntp
配置NTP服务为自启动
1
chkconfig --list ntpd
显示如下内容即可
ntpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
使用ntpdate手动同步时间,免得时间相差太大,让ntpd不能正常同步
1
ntpdate -u 202.112.10.36
配置内网NTP-Server(192.168.8.109),文件位置/etc/ntpd.conf,修改部分如下(多的添,原文件默认配置不做删除,不一样的进行修改)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18允许内网其他机器同步时间
restrict 192.168.8.0 mask 255.255.255.0 nomodify notrap
中国这边最活跃的时间服务器 : http://www.pool.ntp.org/zone/cn
server 210.72.145.44 perfer # 中国国家受时中心
server 202.112.10.36 # 1.cn.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重启服务
1
/etc/init.d/ntpd restart
查看服务连接和监听
1
netstat -autlnp | grep ntp
出现如下即可
udp 0 0 192.168.8.109:123 0.0.0.0:* 23103/ntpd
配置内网NTP-Clients,修改/etc/ntp.conf文件(此处贴出完整的不带注释的配置文件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
配置时间服务器为本地的时间服务器
server 192.168.8.109
restrict 192.168.8.109 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys使用ntpdate手动同步下时间
1
ntpdate -u 192.168.8.109
几个命令
1
2ntpq -p #查看网络中的NTP服务器
ntpstat #查看时间同步状态
参考文章:http://blog.csdn.net/xw_classmate/article/details/50611489