CentOS下NTP安装配置

安装
yum install ntp


配置文件 /etc/ntp.conf
restrict default kod nomodify
notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
# 用restrict控管权限
# nomodify - 用户端不能更改ntp服务器的时间参数
# noquery - 用户端不能使用ntpq,ntpc等命令来查询ntp服务器
# notrap - 不提供trap远端登录
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
# 如下是两个ntp服务器
restrict cn.pool.ntp.org
restrict 164.67.62.194

server 0.cn.pool.ntp.org
server 1.centos.pool.ntp.org

server 2.centos.pool.ntp.org
server 3.centos.pool.ntp.org
linux

broadcast 172.255.255.255                          ios

broadcastclient                                           web

disable authcentos

broadcast是指对哪一个局域网广播时间,broadcastclient和disable auth是指客户端不须要加密验证就能够同步服务器服务器

三、编辑/etc/ntp/step-tickers写入上层NTP服务器IP网络

cn.pool.ntp.org
socket

127.0.0.1加密

四、配置NTP服务自动启动spa

chkconfig ntpd onrest

五、启动NTP

sercive ntpd start

# 系统时间与bios事件的误差记录

driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
# 日志
logfile /var/log/ntp.log
broadcastdelay 0.008

一个说明:
我在vmware下搭建的ntp服务器,别人能够同步它,它本身的时间却不对。照理是它同步上层,下层同步它呀。后来找了蛮久的资料,发现这个文件:/etc/ntp/step-tickers,说是ntp服务器没法主动同步上层服务器,因此将地址写入该文件中便可。

我在物理机里暂时没发现这个问题,也许是物理机原本本身的时间就正确,就难以发现它有没有主动同步上层服务器。

运行ntp
service ntpd start/stop/restart
chkconfig ntpd on

查看ntp的端口,应该看到123端口
netstat -unlnp

查看ntp服务器有无和上层连通
[root@S5 ~]# ntpstat
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s

注意:此命令出现上述synchronised结果比较慢,个人用了大概5分钟。

查看ntp服务器与上层间的联系
[root@S5 ~]# ntptrace -n 127.0.0.1
127.0.0.1: stratum 2, offset 0.064524, synch distance 0.121167
210.72.145.44: timed out, nothing received***Request timed out


查看ntp服务器与上层ntp服务器的状态
[root@S5 ~]# ntpq -p
其中,
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过期间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差

 

客户端同步时间的方法
方法一:使用 ntpdate
ntpdate [IP_Address]
看下面的例子,其中192.168.1.5是我在内网搭建的ntp服务器
[root@zy180 ~]# date
Thu Sep 25 13:04:17 CST 2008
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 14:33:13 ntpdate[10302]: step time server 192.168.1.5 offset 5327.700798 sec
[root@zy180 ~]# date
Thu Sep 25 14:33:18 CST 2008

ntpdate 的特色:
1,ntpdate只运行一次就结束,即只同步一次。因此要使用ntpdate同步时间的话要将该命令写入cron中。
vi /etc/crontab
* * * * * root ntpdate 192.168.1.5 && hwclock -w
之因此使用hwclock -w命令稍后再说。
2,ntpdate同步时间是跳跃的,如上面的显示,时间当即就同步了,这将可能致使文件时间标记、监控数据的紊乱。

方法二:使用ntpd服务
step1:/etc/ntpd.conf 写入 server [IP_Address]
step2:chkconfig ntpd on
step3:service ntpd start

大概过一刻钟开始起做用。

FAQ:系统报错 - Sep 25 11:59:39 localhost kernel: set_rtc_mmss: can't update from 7 to 59
==================================网上查得源码==========================================
if (abs(real_minutes - cmos_minutes) < 30) { update_cmos() } else { printk("set_rtc_mmss: can't update from %d to %d\n", cmos_minutes, real_minutes); } ========================================================================================
bios时间与系统时间差别超过30分钟,就会报上述错误,只要修改bios时间或系统时间便可。

虚拟机下我的体验:
个人vm客户端用ntpd同步服务器后,慢慢变成标准时间,可接着又比标准时间愈来愈慢,而后出现上述报错。因此我以为ntpd的同步频率比个人vm时钟频率跑得慢。

 

 

ntpd 的特色:
1,ntpd服务始终运行,按期同步时间。
2,ntpd服务是慢慢改变时间直至标准时间。
3,若是系统时间与bios时间差别超过30分钟,ntpd就会报错。

总结:
1,推荐使用 ntpd 服务。
2,ntpd开启后,没法使用ntpdate,看以下例子:
[root@zy180 ~]# service ntpd start
Starting ntpd: [ OK ]
[root@zy180 ~]# ntpdate 192.168.1.5
25 Sep 15:25:22 ntpdate[12334]: the NTP socket is in use, exiting

附:linux的时间、时区
1,时间
系统时间:date 命令
bios时间:hwclock命令 (-r 读取bios时间,-w 写入bios时间)
每次开机时系统从新读bios时间。
因此前面ntpdate命令时的cron脚本须要用到hwclock,以免机器重启后时间混乱。


2,时区
linux的全部时区文件都放在 /usr/share/zoneinfo 下。
linux系统的时区文件是 /etc/localtime,这是个时间格式的档案,不是ACSII码。它与/usr/share/zoneinfo/下的本身的时区文件是同样的。

个人时区是Asia-Shanghai,用diff比较两个文件,以下:[root@zy180 ~]# diff /usr/share/zoneinfo/Asia/Shanghai /etc/localtime[root@zy180 ~]#在 /etc/sysconfig/clock 文件中也写有本时区的信息:[root@zy180 ~]# cat /etc/sysconfig/clockZONE="Asia/Shanghai"UTC=falseARC=false

相关文章
相关标签/搜索