您的位置:首页 > 运维架构 > Linux

CentOS配置时间同步NTP

2013-09-29 13:29 537 查看
为什么要使用ntpd而不是ntpdate?

       原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。(注:当本地时间与标准时间相差30分钟以上是ntpd会停止工作)

 

ntp通信协议原理

1.首先主机启动NTP。

2.客户端会向NTP服务器发送调整时间的message。

3.然后NTP server会送出当前的标准时间给client

4.client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。

NTP这个deamon采用了123端口。(UDP)

“当我们要利用Tim server来进行实践的同步更新时,就需要使用NTP软件提供的ntpdate来连接端口123”

 

与时间相关的一些命令和配置文件
1./etc/ntp.conf

linux各版本虽然目录不同,但文件名相同。可以用which ntp.conf 或者locate ntp.conf来查找。这是NTP唯一的一个设置文件。

2./usr/share/zoneinfo/

这个里面规定了这个主要时区的时间设置文件。

3./etc/sysconfig/clock

这个文件是linux的主要时区设置文件,每次开机后linux会自动读取这个文件来设置系统所默认的显示时间,可以看看它里面到底设置了什么:

cat /etc/sysconfig/clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE="Asia/Shanghai"
UTC=true
ARC=false

4./etc/localtime

本地端时间配置文件。

5./bin/date

这个是时间的修改命令,除了输出时间,还可以修改时间。

6./sbin/hwclock

因为linux系统上面BIOS时间与linux系统时间是分开的,所以使用date这个指令调整了时间之后,还需要使用hwclock才能将修改过的时间写入BIOS中。

7./usr/sbin/ntpd

这是NTP的daemon文件,需要启动它才能提供NTP服务,这个命令会读取/etc/ntp.conf里面的设置。

8./usr/sbin/ntpdate

这是client用来连接NTP Server的主要执行文件,如果您不想启用NTP,只想启用NTP Client功能的话,可以只应用此命令。

9,/usr/sbin/ntptrace

可以用来追踪某台时间服务器的时间对应关系。

 

安装与配置:

1、设置时区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2、2、安装ntpd服务

yum -y install ntp

3、配置ntpd

vi /etc/ntp.conf

restrict default kod nomodify notrap nopeer noquery   #
restrict -6 default kod nomodify notrap nopeer noquery  #针对ipv6设置

# 允许本地所有操作
restrict 127.0.0.1
restrict -6 ::1

# 允许的局域网络段或单独ip
restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify motrap
restrict 192.168.1.123 mask 255.255.255.255 nomodify motrap

# 使用上层的internet ntp服务器
restrict cn.pool.ntp.org
restrict 1.cn.poo.ntp.org
restrict 0.asia.pool.ntp.org
restrict 3.asia.pool.ntp.org

server cn.pool.ntp.org prefer
server 1.cn.poo.ntp.org
server 0.asia.pool.ntp.org
server 3.asia.pool.ntp.org

# 如果无法与上层ntp server通信以本地时间为标准时间
server   127.127.1.0    # local clock
fudge    127.127.1.0 stratum 10

# 计算本ntp server 与上层ntpserver的频率误差
driftfile /var/lib/ntp/drift

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
#日志文件
logfile /var/log/ntp.log

修改/etc/sysconfig/ntpd:

# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"

# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=yes #make no into yes; BIOS的时间也会跟着修改

# Additional options for ntpdate
NTPDATE_OPTIONS=""

验证与状态检查:

运行ntp

service ntpd start/stop/restart

查看ntp的端口

应该看到123端口

netstat -unlnp

查看ntp服务器有无和上层连通

# ntpstat

synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s

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

查看ntp服务器与上层间的联系

# ntptrace -n 127.0.0.1

127.0.0.1: stratum 3, offset -0.001095, synch distance 0.532610
116.193.83.174: timed out, nothing received

查看ntp服务器与上层ntp服务器的状态

# ntpq -p

其中:

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

如果所有远程服务器的jitter值是4000并且delay和reach的值是0,那么说明时间同步是有问题的

可能的原因是防火墙阻断了与server之间的通讯,即123端口是否正常开放;

此外每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接,否则你在客户端执行“ntpdate 服务器ip”的时候将返回:

27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found

将ntp服务设置开机启动

chkconfig --levels 3 ntpd on

附录:

中国国家授时中心210.72.145.44

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: