CentOS6.x 自带了时间服务器,此处不需要安装,直接进行配置即可。如发生没有安装的,请自行安装 yum install ntp ntpdate组件。
一、服务器配置
1.编辑配置文件/etc/ntp.conf文件
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
//找到上述配置处,去掉前面的#号,修改地址范围为允许访问NTP服务的地址,例如
restrict 10.121.55.0 mask 255.255.255.0 nomodify notrap
//本例中允许接收同步请求的地址范围是10.121.55.0/24
2.配置上游NTP服务器
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
//找到上述配置处,在后面增加主服务器的配置,例如此处的M01.com
server M01.com iburst
在server 最后加上本地回路
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
示例
二、客户端配置
- 编辑配置文件/etc/ntp.conf文件
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
//找到上述配置处,在后面增加主服务器的配置,例如此处的M01.com,将其他都注释
server M01.com iburst
例:
三、配置服务自启动
1、服务端和客户端都需要配置自动启动
1):启动ntp服务
chkconfig ntpd on
systemctl start ntpd service ntpd start /etc/init.d/ntpd start
2、查看ntp服务有无和上层ntp服务器联通
1.ntpstat
ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
刚启动的时候,一般是:
# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
连接并同步后:
synchronised to NTP server (202.120.2.101) at stratum 4
time correct to within 1192 ms
polling server every 64 s
命令:ntpstat
查看ntp状态可能出现如下情况
1: unsynchronised time server re-starting polling server every 8 s
2:unsynchronised polling server every 8 s
这种情况属于正常,ntp服务器配置完毕后,需要等待5-10分钟才能与/etc/ntp.conf中配置的标准时间进行同步。
等一段时间之后,再次使用ntpstat命令查看状态,就会变成如下正常结果
3、查看ntp服务器与上层ntp的状态
命令:ntpq -p
第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。
remote:列出源的 IP 地址或主机名。
when:指出从轮询源开始已过去的时间(秒)。
poll:指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
reach:是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
offset:是源时钟与本地时钟的时间差(毫秒)。
四:查看时间是否与本地时间同步
命令:date +"%F %H:%M:%S"
若时间对不上,查看时区是否正确,若不正确将时间设为Shanghai的
找到/etc/localtime文件是否指向/usr/share/zoneinfo/Asia/Shanghai,如:
若指向不正确,则先删除localtime文件,再使用软连接连接/usr/share/zoneinfo/Asia/Shanghai
命令:rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
好了ntp服务到此配置完成