NTP时间服务器安装与配置

2023-11-14

第1章 Server端的安装与配置
1.1查看系统是否已经安装ntp服务组件
rpm -qa | grep “ntp”
#查看是否已经安装ntp组件,有如下两个组件说明已经安装
ntpdate-4.2.6p5-1.el6.centos.x86_64
ntp-4.2.6p5-1.el6.centos.x86_64
#查看是否已经安装ntp组件,有上述两个组件说明已经安装
1.2 yum安装
yum -y install ntp
1.3 ntp服务器端的配置
1.3.1配置文件
配置文件/etc/ntp.conf
配置文件默认内容
egrep -v “^$|#” ntp.conf #排除默认注释及空行
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery #对默认的client拒绝所有操作
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1 #允许本地地址的一切操作
restrict -6 ::1
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
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
修改配置
cat /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery #对默认的client拒绝所有操作
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1 #允许本机的一切操作
restrict 172.23.100.0 mask 255.255.255.0 nomodify notrap noquery #允许局域网内所有client连接到这台服务器
restrict -6 ::1 同步时间.但是拒绝让他们修改服务器上的时间
#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 ntp1.aliyun.com #同步公网的时间
server cn.ntp.org.cn
server cn.pool.ntp.org
server 127.127.1.0 #如果无法连接公网,则使用自己本身的时间作为标准时间。
fudge 127.127.1.0 stratum 10 #设置stratum级别
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
1.3.2 restrict安全相关参数
ignore :关闭所有的NTP联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery:不提供客户端的时间查询
第2章 Client的安装与配置
客户端的安装同服务器,如上。
2.1配置文件
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
#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 ntpserver #添加自己的时间服务器如:172.23.100.99
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
第3章服务启动
3.1服务器段启动
在服务器端启动NTP服务,命令如下:
/etc/init.d/ntpd start或service ntpd start
添加开启启动:
chkconfig ntpd on
3.2客户端同步
3.2.1 ntpdate命令同步
在客户端运行时间同步命令ntpdate,测试服务器是否工作。该方法可以不用再客户端启动ntpd服务,可以将ntpd服务做成定时任务来同步时间服务:
命令格式:ntpdate server
ntpdate 172.23.100.99
25 Apr 15:42:57 ntpdate[31878]: adjust time server 172.23.100.99 offset -0.002143 sec
注:ntpdate命令需要客户端不能运行ntpd服务,否则会报如下错误:
ntpdate master1
19 Dec 17:45:51 ntpdate[20110]: the NTP socket is in use, exiting
3.2.2 ntpdate定时任务
echo ‘*/10 * * * * /usr/sbin/ntpdate 172.23.100.99 &>/dev/null’ >> /var/spool/cron/root
该命令是做一个每10分钟同步一次时间服务器的任务,其中master1是自己配置的时间服务器,&>/dev/null表示不打印日志。
3.2.3 ntpd服务同步
启动ntpd服务:
/etc/init.d/ntpd start
查看同步的状态:ntpq -p
ntpq -p
remote refid st t when poll reach delay offset jitter

time5.aliyun.co 10.137.38.86 2 u 27 64 1 52.650 3657191 0.000
LOCAL(0) .LOCL. 10 l 26 64 1 0.000 0.000 0.000
各项参数的含义如下:
服务参数
含义
remote
本地机器连接的远程NTP服务器。
refid
给远程服务器提供时间同步的服务器。
st
远程服务器的层级别(stratum)。由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端,所以服务器从高到低级别可以设定为1-16。
t
本地NTP服务器与远程NTP服务器的通信方式,u:单播;b:广播;i:本地
when
本地机器和远程服务器进行一次时间同步的剩余时间。
poll
本地机和远程服务器多少时间进行一次同步(单位为秒)
reach
测试能够和服务器连接,每成功一次它的值就会增加
delay
从本地机发送同步要求到服务器的round trip time
offset
本地机和服务器之间的时间差别。Offset接近0,就和时间服务器的时间越接近。
jitter
统计了再特定个连续的连接数里offset的分布情况。
+
它将作为辅助的NTP Server和带有
号的服务器一起为我们提供同步服务. 当*号服务器不可用时它就可以接管
*
远端的服务器已经被确认为我们的主NTP Server,我们系统的时间将由这台机器所提供

远程服务器被clustering algorithm认为是不合格的NTP Server
x
远程服务器不可用

3.3同步机器时间
linux除了系统时间之外,还有机器时间,这两者可能不同一,可以用hwclock查看机器时间:
hwclock
Tue 19 Dec 2017 06:27:40 PM CST -0.627173 seconds
3.3.1使用hwclock –w命令将系统时间同步到机器时间
hwclock –w
3.3.2配置/etc/sysconfig/ntpd文件
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。
3.4 IPTABLES 配置
由于NTP服务需要使用到UDP端口号123,所以当系统的防火墙(Iptables)启动的情况下,必须开放UDP端口号123。
[root@localhost ~]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all – 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp – 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all – 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all – 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all – 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@localhost ~]# /sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT
[root@localhost ~]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT udp – 0.0.0.0/0 0.0.0.0/0 udp dpt:123
2 ACCEPT all – 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 ACCEPT icmp – 0.0.0.0/0 0.0.0.0/0
4 ACCEPT all – 0.0.0.0/0 0.0.0.0/0
5 ACCEPT tcp – 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 REJECT all – 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all – 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@localhost ~]#
如果防火墙没有开放UDP端口号123,有可能出现下面情况。
[root@localhost ~]# /usr/sbin/ntpq -c rv | grep stratum
stratum=16, precision=-24, rootdelay=0.000, rootdisp=3.525, refid=INIT,
[root@localhost~]#
A stratum level of 16 indicates that NTP is not synchronizing correctly.If a stratum level of 16 is detected, wait 15 minutes and issue the command again. It may take this long for the NTP server to stabilize.If NTP continues to detect a stratum level of 16, verify that the NTP port (UDP Port 123) is open on all firewalls between the cluster and the remote machine you are attempting to synchronize to.
3.5.启动NTP服务
[root@localhost ~]# service ntpd status
ntpd is stopped
[root@localhost ~]# service ntpd start
Starting ntpd: [ OK ]
[root@localhost ~]#
service ntpd status #查看ntpd服务状态
service ntpd start #启动ntpd服务
service ntpd stop #停止ntpd服务
service ntpd restart #重启ntpd服务
检查ntp服务是否开机启动,将其设置为开机启动。
[root@localhost ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost ~]# runlevel
N 3
[root@localhost ~]# chkconfig ntpd on #在运行级别2、3、4、5上设置为自动运行
[root@localhost ~]# chkconfig --list ntpd
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]#
如果要设置在运行级别上自动运行,可以使用下面命令
chkconfig --level 345 ntpd on
可以用下面命令检测NTP服务是否运行
[root@localhost ~]# pgrep ntpd
2639
2641
[root@localhost ~]# netstat -tlunp | grep ntp #如果看到123端口,说明ntp服务成功启动。
udp 0 0 192.168.7.224:123 0.0.0.0:* 2639/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2639/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2639/ntpd
udp 0 0 fe80::250:56ff:feb3:b5:123 ::? 2639/ntpd
udp 0 0 ::1:123 ::? 2639/ntpd
udp 0 0 :::123 ::? 2639/ntpd
查看ntp服务器有无和上层ntp连通
[root@localhost ~]# ntpstat
synchronised to NTP server (192.168.7.49) at stratum 6
time correct to within 440 ms
polling server every 128 s
[root@localhost ~]#
查看ntp服务器与上层ntp的状态
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter

192.168.7.49 192.168.7.50 5 u 13 64 3 5.853 1137178 2.696
[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter

192.168.7.49 192.168.7.50 5 u 17 64 3 5.853 1137178 2.696

3.6 ntpd、ntpdate的区别
下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:
使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。
时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:
第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NTP时间服务器安装与配置 的相关文章

随机推荐

  • 面向对象OO 设计、架构终极理解, 以及如何学习一个领域

    程序就是一些互相引用的内存快 互相发消息 每个内存块就是一个状态机 状态的迁移规则是定制好的一些消息 方法 构造函数用来初始化状态 一个内存块的方法除了改变自身状态 也有可能向引用的别内存快发消息 引起别的内存块发生状态转移 重点不在过程化
  • 常用数学函数

    转自 https zh cppreference com w cpp numeric math 函数 定义于头文件
  • 详解Transformer的自注意力机制、位置编码以及整体架构(内容生动易懂,并有完整transformer架构详解)

    文章目录 1 3 Transformer一些前置知识 1 3 1 自注意力机制 Self Attention very important 1 3 2 位置编码 Positional Encoding 1 3 3 Transformer模型
  • @RabbitListener和@RabbitHandler的使用

    1 RabbitListener 注解是指定某方法作为消息消费的方法 例如监听某 Queue 里面的消息 2 RabbitListener标注在方法上 直接监听指定的队列 此时接收的参数需要与发送市类型一致 Component public
  • 常见的Linux系统性能问题及其解决方法

    CPU负载高 如果CPU负载高 可能是由于CPU资源不足或进程 服务的异常活动所致 可以使用top或htop命令查看系统的CPU使用情况 并确定哪个进程或服务使用了大量的CPU资源 如果负载高 可以考虑增加CPU资源或优化进程 服务的代码或
  • 快手如何引流的话术?在快手引流精准粉丝的技巧

    互联网创业圈大部分的项目都离不开流量 更别说精准流量了 流量可以说是大部分互联网创业者心里的痛处 很多人都说 流量难求 其实 要么是不擅长引流 要么是方法出了问题 那么 要如何引流比较好呢 今天 主要讲讲快手这个渠道是如何做的 其实 点石汇
  • C++11-14 第6讲 explicit关键字

    explicit 用来针对构造函数有多个实参 include
  • 利用python进行数据分析——pandas

    import pandas as pd import numpy as np np random seed 12345 import matplotlib pyplot as plt 1 介绍Pandas 工具一 Series obj pd
  • 总结一下多益网络数据研发校招面试

    1 自我介绍 2 说一下你觉得最好的项目 3 数据仓库分层 4 说一下主键和唯一索引 5 说一下hive的group by 怎么在mapreduce执行的 6 说一下hashmap和map区别 7 说一下数据预处理 8 说一下MR原理 9
  • 少儿编程要先学会电脑和英语

    格物斯坦小坦克想说的是少儿编程和计算机代码编程是不一样的 很多人对孩子学习的Scratch语言和成人学习的Java C语言等区分不开 因而习惯性的抗拒说 我的孩子以后可能不会成为一个程序员 学习之前是不是先要懂英语和电脑 实际上少儿电脑编程
  • 如何编写有多个返回值的C语言函数

    1引言 笔者从事C语言教学多年 在教学中学生们常常会问到如何编写具有多个返回值的C语言函数 编写有多个返回值的函数是所有C语言教材里均没有提到的知识点 但在实际教学与应用的过程中我们都有可能会遇到这样的问题 有学生也尝试了不少方法 如把多个
  • B - Equations

    B Equations Consider equations having the following form ax12 b x22 cx32 d x42 0 a b c d are integers from the interval
  • 使用Idea创建一个JavaWeb的SSM(maven)项目~(史上最详细,傻瓜式教学,跟着我的做,不会你找我)

    今天讲的是如何用idea创建一个JavaWeb的Maven SSM项目并且实现简单的登陆功能 项目源码在最后 需要的可以自行下载 本文章过于详细过于面向小白 并且在讲解SSM项目的搭建过程中涉及到了小白们极有可能遇到的Idea的坑 文章比较
  • Keystone 高可靠性部署与性能测试

    Goal Keystone Region 为跨地域的 Openstack 集群提供了统一的认证和用户租户管理 目前公司在国内外部署了数十套 Openstack 集群 其中既有集群在内网 又有集群在公网 既有 Havana 集群 也有 Ice
  • Thumb和ARM指令不能切换问题(error:unsupported interworking call (Thumb -> ARM))

    1 报错现象 xxx ko ection 3 reloc 4 sym xxxxxx unsupported interworking call Thumb gt ARM 2 报错原因和分析 报错信息的翻译 程序不支持代码交织 thumb态切
  • Spring入门简介

    Spring框架是Java平台上最流行的企业应用程序开发框架之一 它是一个轻量级的开源框架 被设计为简化企业级应用程序的开发 Spring的核心目的是为了解决企业应用程序的开发问题 包括代码的耦合 事务管理 异常处理 数据访问 安全性 We
  • fatal error C1083: 无法打开预编译头文件:“Debug\opencv.pch”: No such file or directory

    步骤 方法右键点击你创建的项目 选择 属性标签 点击属性 弹出 项目属性页 在左侧找到以下位置 配置属性 gt C C gt 预编译头 并选择它 在右边的菜单中选择 创建 使用预编译头 中的 不使用预编译头文件 点击 确定 按钮退出即可原因
  • Transformer 架构和 BERT、GPT 和 T5 的兴起:初学者指南

    在广阔且不断发展的人工智能 AI 领域 有些创新不仅会留下深刻的印象 而且会带来巨大的影响 他们重新定义了整个领域的轨迹 在这些突破性的创新中 Transformer 架构成为变革的灯塔 这类似于工业革命期间蒸汽机的发明 推动人工智能进入一
  • 深入理解Unity5中的StandardShader屏幕像素化特效的实现

    概要 续接上文 本文进一步讲解与分析了上文未讲完的Unity5中Standard Shader正向基础渲染通道源码的片段着色实现部分 以及对屏幕像素化后期特效进行了实现 同样需要声明的是 本文中对Stardard Shader源码的一些分析
  • NTP时间服务器安装与配置

    第1章 Server端的安装与配置 1 1查看系统是否已经安装ntp服务组件 rpm qa grep ntp 查看是否已经安装ntp组件 有如下两个组件说明已经安装 ntpdate 4 2 6p5 1 el6 centos x86 64 n