centos6.5安装配置NTP,集群各机器间时间同步

本文详细介绍了如何在Linux环境下配置NTP服务器及客户端,实现网络内的精确时间同步。包括安装NTP服务、设置开机自启、配置NTP服务器、同步硬件时间等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试验环境

提君博客原创

>>提君博客原创  http://www.cnblogs.com/tijun/  <<

IP主机名角色描述同步方式
192.168.11.11ltt1.bg.cn

NTPD服务

1、负责与外部公共NTPD服务同步标准时间

 2、作为内外网络的NTPD服务

NTPD服务平滑同步
192.168.11.12ltt2.bg.cn 内外NTP客户端 内网设备与192.168.11.11同步时间NTPD服务平滑同步
192.168.11.13ltt3.bg.cn 内外NTP客户端 内网设备与192.168.11.11同步时间NTPD服务平滑同步
192.168.11.14ltt4.bg.cn 内外NTP客户端 内网设备与192.168.11.11同步时间NTPD服务平滑同步
192.168.11.15ltt5.bg.cn 内外NTP客户端 内网设备与192.168.11.11同步时间NTPD服务平滑同步

 

 

 

 

 

 

 

 

第一步,以上所有的机器都安装ntp

[root@ltt1 ~]# yum install ntp

第二步,设置开机自启动

[root@ltt1 conf]# chkconfig ntpd on
[root@ltt1 conf]# chkconfig --list ntpd
ntpd               0:off    1:off    2:on    3:on    4:on    5:on    6:off

第一、第二,以上两步,在各机器上都要执行

提君博客原创

第三步,配置内网NTP-server(192.168.11.11)

此步是在你要设置成NTP服务的机器上配置,它将给内网所有的机器进行授时,即内网所有机器都是以它的时间为准。

我这里选择的是192.168.11.11。

先使用ntpdate手动同步下内网中作为 NTPD 服务器的时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步。即在 192.168.11.11 上执行 

ntpdate -u 1.cn.pool.ntp.org

然后要找到可以正常提供时间服务的外部主机,执行以下命令

[root@ltt1 ~]# ntpdate -q 1.cn.pool.ntp.org
server 51.15.41.135, stratum 3, offset -0.648578, delay 0.37547
server 193.228.143.12, stratum 2, offset -0.652907, delay 0.36064
server 202.118.1.130, stratum 2, offset -0.650668, delay 0.05075
server 120.25.115.20, stratum 2, offset -0.650029, delay 0.06900
29 Sep 17:51:21 ntpdate[1500]: step time server 202.118.1.130 offset -0.650668 sec

这里可以看到列出了四个外部主机,以及和本机时间的时间差异。

最后一行的末尾,time server 202.118.1.130 offset -0.650668 sec,这说明时间差异最小的一个外部主机为202.118.1.130

必要的信息已经获取,现在正是开始配置NTP

[root@ltt1 ~]# vi /etc/ntp.conf 

这里列出的只是注释掉内容和添加的内容,

复制代码
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#设置对内网主机提供NTP服务。我的集群网关是192.168.11.2
restrict 192.168.11.2 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #注释掉centos默认的对时服务器,如果主机能访问外网,最好做这一步
#server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst
#指定上层对时的外部服务器,这些都是上面刚刚得到的可以正常提供时间服务的外部主机 server 51.15.41.135 server 193.228.143.12 server 202.118.1.130 perfer server 120.25.115.20 #修改上层服务器对本机的权限 restrict 51.15.41.135 nomodify notrap noquery restrict 193.228.143.12 nomodify notrap noquery restrict 202.118.1.130 nomodify notrap noquery restrict 120.25.115.20 nomodify notrap noquery #外部时间服务器不可用时,设置以本地时间作为时间服务 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10
复制代码

 第四步,同步硬件时间

提君博客原创

在 Linux 下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了,那么既然两个时钟独自运行,那么时间久了必然就会产生误差了,而 NTP 默认又只更新系统时间,因此我们需要设置硬件时钟进行同步调整

[root@ltt1 ~]# vi /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
SYNC_HWCLOCK=yes
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"

第五步,启动ntp服务,查看相关状态,验证

>>提君博客原创  http://www.cnblogs.com/tijun/  <<

[root@ltt1 ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

查看服务连接和监听

复制代码
[root@ltt1 ~]# netstat -tlunp | grep ntp
udp        0      0 192.168.11.11:123           0.0.0.0:*                               1327/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1327/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1327/ntpd           
udp        0      0 fe80::20c:29ff:fec5:88c5:123 :::*                                    1327/ntpd           
udp        0      0 ::1:123                     :::*                                    1327/ntpd           
udp        0      0 :::123                      :::*                                    1327/ntpd           
复制代码

查看网络中的NTP服务器,同时显示客户端和每个服务器的关系

复制代码
[root@ltt1 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp3.itcomplian 5.103.128.88     3 u   25   64   37  446.548  -700.47   1.006
 ntp1.flashdance 193.11.166.20    2 u   21   64   17  432.794  -705.08  12.690
*202.118.1.130   202.118.1.47     2 u   23   64   37   24.599  -653.77   1.470
 120.25.115.20   10.137.38.86     2 u   18   64   37   42.456  -651.64   1.038
 LOCAL(0)        .LOCL.          10 l   90   64   36    0.000    0.000   0.000
复制代码

查看时间同步状态

提君博客原创

[root@ltt1 ~]# ntpstat
synchronised to NTP server (202.118.1.130) at stratum 3 
   time correct to within 1658 ms
   polling server every 64 s

第六步,配置NTP-client,这是在除192.168.11.11的其他机器上进行

[root@ltt2 ~]# vi /etc/ntp.conf 

注意,这里的配置是和192.168.11.11有区别的

复制代码
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

#对时服务器,配置成ntp-server的机器
server 192.168.11.11 restrict 192.168.11.11 nomodify notrap noquery server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10
复制代码

配置同步硬件时间

[root@ltt2 ~]# vi /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
SYNC_HWCLOCK=yes
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"

第七步,启动ntp-client,手动更新时间,查看相关状态,验证,这里以ltt2为例

[root@ltt2 ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

手动向192.168.11.11更新一次时间

ntpdate -u 192.168.11.11

查看服务连接和监听

复制代码
[root@ltt2 ~]# netstat -tlunp | grep ntp
udp        0      0 192.168.11.12:123           0.0.0.0:*                               10776/ntpd          
udp        0      0 127.0.0.1:123               0.0.0.0:*                               10776/ntpd          
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               10776/ntpd          
udp        0      0 ::1:123                     :::*                                    10776/ntpd          
udp        0      0 fe80::20c:29ff:fef9:26cb:123 :::*                                    10776/ntpd          
udp        0      0 :::123                      :::*                                    10776/ntpd       
复制代码

查看网络中的NTP服务器,同时显示客户端和每个服务器的关系

[root@ltt2 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ltt1.bg.cn      202.118.1.130    3 u  880 1024  377    1.184    0.242   1.190
 LOCAL(0)        .LOCL.          10 l  14h   64    0    0.000    0.000   0.000

查看时间同步状态

>>提君博客原创  http://www.cnblogs.com/tijun/  <<

[root@ltt2 ~]# ntpstat
synchronised to NTP server (192.168.11.11) at stratum 4 
   time correct to within 85 ms
   polling server every 1024 s

到此,机器的同步时间就配置完成了。

提君博客原创

 

附:ntpq -p指令,显示的各项含义

提君博客原创

位置标志含义
符号*响应的NTP服务器和最精确的服务器
+响应这个查询请求的NTP服务器
blank(空格)没有响应的NTP服务器
标题remote响应这个请求的NTP服务器的名称
refidNTP服务器使用的更高一级服务器的名称
st正在响应请求的NTP服务器的级别
when上一次成功请求之后到现在的秒数
poll本地和远程服务器多少时间进行一次同步,单位秒,在一开始运行NTP的时候这个poll值会比较小,服务器同步的频率大,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach用来测试能否和服务器连接,是一个八进制值,每成功连接一次它的值就会增加
delay从本地机发送同步要求到ntp服务器的往返时间
offset主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset越接近于0,主机和ntp服务器的时间越接近
jitter统计了在特定个连续的连接数里offset的分布情况。简单地说这个数值的绝对值越小,主机的时间就越精确

转载于:https://www.cnblogs.com/muziyunxuan/p/8526871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值