首页 > 编程知识 正文

linux关闭ntp服务器,linux下ntp服务器搭建

时间:2023-05-05 20:39:59 阅读:134001 作者:1461

一.介绍:

要同步时间,请使用ntpdate命令或使用ntpd服务。 在使用之前,必须明确ntpd和ntpdate在更新时间时的不同之处。 ntpd不仅仅是时间同步服务器,还可以作为客户端与标准时间服务器同步时间。 此外,同步是平稳的,ntpdate并不立即同步。 由于在生产环境中谨慎使用ntpdate,所以两者不能同时运行。

使用ntpd服务比ntpdate和cron的组合更好。 这是因为,ntpdate通过同步时间来引起时间的跳跃,从而影响依赖于时间的程序和服务。 例如sleep、timer等。 另外,ntpd服务器也可以在修改时间的同时修改cpu tick。 理想的方法是在通电时使用ntpdate强制同步时间,否则使用ntpd服务同步时间。

需要注意的是,ntpd有自我保护设定:如果本机和上传源的时间太大,ntpd就不能工作。 因此,新设置的时间服务器必须在启动ntpdate之前从上源获取时间初始值。 ntpd服务运行后,首先每64秒与源服务器同步一次,根据每次同步测量的误差值经过复杂的计算,逐步调整自己的时间,随着误差的减小,逐步增加同步的间隔。 每次都重复这个调整的过程。

时钟的转变可能会在某些程序中引起严重的问题。 许多APP应用依赖于连续的时钟——,这是常见的假设。 这意味着检索时间是线性的,某些操作(如数据库事务)通常取决于时间不会倒流这一事实。 遗憾的是,ntpdate用一种叫做“jump”的方法调整时间。 获得时间后,ntpdate使用settimeofday(2)设置系统时间。 这有几个明显的问题。

第一,这样做不安全。 ntpdate的设定依赖于ntp服务器的安全性,攻击者可以利用软件设计上的缺陷,获得ntp服务器,使与之同步的服务器执行某种消耗性的任务。 由于ntpdate采用了跳转方式,因此服务器无法知道是否发生了异常(如果时间不同,唯一的方法以服务器为准)。

第二,这样做不准确。 如果ntp服务器关闭,相应的服务器也将无法同步时间。 对此,ntpd不仅可以校正计算机的时间,还可以校正计算机的时钟。

第三,这样做不优雅。 因为是跳跃,而不是加快或减慢时间,所以依赖于时间序列的程序会发生错误。 例如,如果ntpdate发现你的时间变快了,它可能会经历两个相同的时间,对某些APP应用程序来说是致命的。 因此,唯一能让时间跳跃的点是计算机刚刚启动,还有很多服务器没有启动的时候。 在其余时候,理想的情况是使用ntpd校准时钟,而不是调整计算机时钟的时间。

NTPD在与时间服务器同步期间,记录BIOS计时器的振荡频率偏差——或Local Clock的自然漂移——。 由此,即使网络有问题,本机也能够维持相当正确的行驶。

示例:使用ntpdate更简单。 格式如下

# ntpdate [-nv] [NTP IP/hostname]

# ntpdate 192.168.0.2

# ntpdate time.ntp.org

但是,这样的同步只是强制将系统时间设置为ntp服务器时间。 如果cpu tick有问题,只是治标不治本。 因此,一般配合cron命令进行定期的同步设定。 例如,在crontab中: root路径下添加()

在/etc/crontab文件下添加

012 * * */usr/sbin/ntpdate 192.168.0.1

这样,每天12点正好,使时间同步。 ntp服务器为192.168.0.1。

配置方案:

二.服务端配置

1 .首先检查安装服务器上是否安装了ntp、ntpdate

# rpm -qa | grep ntp

ntpdate-4.2.6 P5-29.el7.centos.x86 _ 64

NTP-4.2.6 P5-29.el7.centos.x86 _ 64

2 .如果没有,则必须使用安装

# yum -y install ntp ntpdate

3 .修改NTP配置文件/etc/ntp.conf

1 )注释以下的配置

225.225.225.0 nomotify notrap

#时间服务器列表
server 210.72.145.44    #中国国家授时中心
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com

#当外部时间不可用时,使用本地时间
server 127.0.0.1
fudge 127.0.0.1 stratum 10

#允许上层时间服务器主动修改本机时间
restrict 210.72.145.44    nomodify notrap noquery
restrict ntp1.aliyun.com  nomodify notrap noquery
restrict ntp2.aliyun.com  nomodify notrap noquery
restrict ntp3.aliyun.com  nomodify notrap noquery

4.保存退出,重启ntp服务、加入开启自启
# systemctl enable ntpd
# systemctl restart ntpd

5.启动ntp服务时,先手动同步下本地时间
[root@m01 ~]# ntpdate -u ntp1.aliyun.com

6.查询ntp同步时间是否启动
等待几分钟后,当出现如同第二次命令执行结果时即可

# ntpstat 
unsynchronised
  time server re-starting
   polling server every 8 s

# ntpstat 
synchronised to NTP server (120.25.115.20) at stratum 3
   time correct to within 154 ms
   polling server every 64 s
 

三、客户端配置
1.安装ntp、ntpdate
[root@web01 ~]# yum install ntp ntpdate


2.修改配置文件/etc/ntp.conf

1)注释以下配置
# 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

2)新增如下配置
#ntp服务器地址
server 172.16.1.61

#新增:允许上层时间服务器主动修改本机时间
restrict 172.16.1.61 nomodify notrap noquery

#新增:当外部时间不可用时,使用本地时间
server 127.0.0.1  #local clock
fudge 127.0.0.1 stratum 10

3.保存配置,重启ntp服务并加入开启自启
[root@web01 ~]# systemctl enable ntpd
[root@web01 ~]# systemctl restart ntpd

4.查看ntp服务器信息
[root@web01 zabbix]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 172.16.1.61     120.25.115.20    3 u   30   64    1    0.512  -42.740   0.000
 localhost       .INIT.          16 l    -   64    0    0.000    0.000   0.000
 

四、测试
1.查看ntp服务端时间
[root@m01 ~]# date
Sun Mar 29 00:10:52 CST 2020

2.修改客户端时间
[root@web01 ~]# date -s 2020-01-01
Wed Jan  1 00:00:00 CST 2020
[root@web01 ~]# date
Wed Jan  1 00:00:03 CST 2020

3.等待一段时间后客户端时间自动同步,这里用手动同步验证下
[root@web02 ~]# ntpdate -u 172.16.1.61
29 Mar 00:12:14 ntpdate[19563]: adjust time server 172.16.1.61 offset 0.008159 sec
[root@web01 ~]# date
Sun Mar 29 00:12:22 CST 2020

4.如果觉得自动同步时间间隔比较久,可以设定个定时任务,使用ntpdate命令来同步服务端时间
这里以每分钟同步为例
[root@m01 ~]# crontab -e 
* * * * * /usr/sbin/ntpdate -u 172.16.1.61 &> /dev/null
 

至此,ntp时间同步服务器搭建完成


 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。