linux ntp时间同步

简介:

一、搭建时间同步服务器

1、编译安装ntp server
rpm -qa | grep ntp
若没有找到,则说明没有安装ntp包,从光盘上找到ntp包,使用
rpm -Uvh ntp***.rpm
进行安装
2、修改ntp.conf配置文件
vi /etc/ntp.conf
①、第一种配置:允许任何IP的客户机都可以进行时间同步
将“restrict default nomodify notrap noquery”这行修改成:
restrict default nomodify notrap
配置文件示例:/etc/ntp.conf
②、第二种配置:只允许192.168.211.***网段的客户机进行时间同步
在restrict default nomodify notrap noquery(表示默认拒绝所有IP的时间同步)之后增加一行:
restrict 192.168.211.0 mask 255.255.255.0 nomodify notrap



各个选项信息:

#系统时间与BIOS事件的偏差记录

driftfile /etc/ntp/drift

 

restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP

参数有以下几个:

ignore  :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。


设定NTP主机来源(其中prefer表示优先主机)



server time.pool.aliyun.com prefer
server time.windows.com
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org



3、启动ntp服务
service ntpd start
开机启动服务
chkconfig ntpd on
4、ntpd启动后,客户机要等几分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。


二、配置时间同步客户机

手工执行 ntpdate <ntp server> 来同步
或者利用crontab来执行
crontab -e
0 0 * * * /usr/sbin/ntpdate 192.168.191.59 && hwclock -w

每天晚上24点进行同步


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

1
2
3
4
[root@DNS-176241 slaves] # ntpstat 
synchronised to NTP server (182.92.12.11) at stratum 3 
    time  correct to within 41 ms
    polling server every 1024 s

 

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

1
2
3
4
5
6
7
8
9
10
11
12
[root@DNS-176241 slaves] # ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time5.aliyun.co 10.137.38.86     2 u  219  512  377   26.131    2.621   0.252
+13.79.239.69    128.138.141.172  2 u  187  512  377  220.562   15.592   0.737
  dns2.ustb.edu.c 130.149.17.8     2 u  20h  128    0   25.792   -1.642   0.000
+85.199.214.101  .GPS.            1 u   60   16  377  287.174    9.750   2.376
  202.120.2.101.d 85.199.214.100   2 u  466  512  213    3.067   17.591   0.781
-202.118.176.2   202.118.177.155  4 u  145  512  377   44.478    6.772   0.499
  ntp.wdc1.us.lea .INIT.          16 u    - 1024    0    0.000    0.000   0.000
+213.227.164.24  162.23.41.56     2 u  120  512  377  197.167   17.568   0.262
  LOCAL(0)        .LOCL.          10 l   59   64  377    0.000    0.000   0.001


以上命令列出了所有作为时钟源校正过本地NTP服务器时钟的上层NTP服务器的列表,每一列的含义如下所示:

◆ remote: 远程NTP服务器的IP地址或域名,带 “*” 的表示本地NTP服务器与该服务器同步。

◆ refid: 远程NTP服务器的上层服务器的IP地址或域名。

◆ st: 远程NTP服务器所在的层数。

◆ t: 本地NTP服务器与远程NTP服务器的通信方式,u: 单播; b: 广播; l: 本地。

◆ when: 上一次校正时间与现在时间的差值。

◆ poll: 本地NTP服务器查询远程NTP服务器的时间间隔。

◆ reach: 是一种衡量前8次查询是否成功的位掩码值,377表示都成功,0表示不成功。

◆ delay: 网络延时,单位是10的-6次方秒。

◆ offset: 本地NTP服务器与远程NTP服务器的时间偏移。

◆ jitter: 查询偏差的分布值,用于表示远程NTP服务器的网络延时是否稳定,单位为10的-6次方秒。














本文转自leonardos51CTO博客,原文链接:http://blog.51cto.com/leomars/1975925 ,如需转载请自行联系原作者


相关文章
|
3月前
|
消息中间件 Kubernetes NoSQL
Linux时间校准(ntpdate及NTP客户端代码校准示例)
Linux时间校准(ntpdate及NTP客户端代码校准示例)
|
7月前
|
监控 网络协议 Linux
Linux日期和时间管理指南:日期、时间、时区、定时任务和时间同步
Linux日期和时间管理指南:日期、时间、时区、定时任务和时间同步
114 0
|
4月前
|
监控 Linux 定位技术
Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
688 0
linux时间同步
linux时间同步实战操作
|
17天前
|
Linux 网络安全 开发工具
linux 时间同步 chrony
linux 时间同步 chrony
26 1
|
5月前
|
Linux
linux两台服务器时间同步
linux两台服务器时间同步
195 0
|
5月前
|
运维 Linux
linux时间同步以及各种时间格式显示
linux时间同步以及各种时间格式显示
58 0
|
9月前
|
Linux 网络安全
Linux系统之NTP服务器配置
Linux系统之NTP服务器配置
1589 1
|
10月前
|
Linux 网络安全
配置Linux服务器时间同步
配置Linux服务器时间同步
配置Linux服务器时间同步
|
11月前
|
安全 Linux 网络安全
Linux 时间同步 -NTP 服务器
本文主要介绍如何配置 Linux 服务器集群的时间同步,技术实现上使用 NTP 服务器和客户端同步时间。
431 0