ntp配置

版本查看方法

使用cat /etc/.kyinfo 命令查询当前麒麟系统版本,若milestone一栏显示的版本号有3.0字样,则该系统为3.0系统。同样,若该栏显示3.2或3.3字样,则为对应版本系统。
在这里插入图片描述

服务配置

kylin3.0

在3.0系统/etc/ntp.conf文件中,主要包含以下几个部分:授权设置、时间服务器设置、日志及秘钥文件。我们需要更改的内容是其中的授权设置和时间服务器设置。

授权设置

授权设置在/etc/ntp.conf中的内容如图3所示,我们无需更改文件中的默认内容,只需添加授权给要使用ntp服务的网段即可。
默认授权设置,默认拒绝所有来源的任何访问和本机时间同步
如要授权192.168.121.0网段设备进行时间同步,则在文件中加入以下内容:restrict 192.168.121.0 netmask 255.255.255.0 nomodify notrap
增加192.168.121网段授权

##下面两行默认是拒绝所有来源的任何访问
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
#开放本机的任何访问
restrict 127.0.0.1
restrict -6 ::1
# 允许内网其他机器同步时间(允许192.168.121.0/24 网段主机进行时间同步)
restrict 192.168.121.0 mask 255.255.255.0 nomodify notrap
#允许上层时间服务器主动修改本机时间
restrict 192.168.33.44 nomodify notrap noquery

restrict解析:

作用:对ntp做权限控制

  • ignore:忽略所有类型的NTP连接请求
  • nomodify:限制客户端不能使用命令ntpc和ntpq来修改服务器端的时间
  • noquery:不提供NTP网络校时服务
  • notrap:不接受远程登录请求
  • notrust:不接受没有经过认证的客户端的请求

【如果没有用任何参数,那么表示不做任何限制】

例子:restrict 10.220.5.0 mask 255.255.255.0 nomodify <<<允许10.220.5.0/24 网段主机进行时间同步

时间服务器设置

注释以下几行
在这里插入图片描述
将局域网内的两台前置服务器192.168.121.1 192.168.121.2作为时间服务器,且默认优先与192.168.121.1对时。
在这里插入图片描述
而对于时间服务器本身,即例中的两台前置。其与外部时钟源,如GPS,进行对时,所以其时间服务器就是自身。因此,它们的配置文件中时间服务器设置
时间服务器本身配置本地对时

#指定ntp服务器地址
server 192.168.121.1 iburst
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10

日志及密钥设置

在/etc/ntp.conf文件中,还包含了记录时间调整的日志文件以及ntpd服务的秘钥控制文件。这些设置默认不更改日志及秘钥默认配置
至此,/etc/ntp.conf的基本配置已经完成。配置完成后,就可以用service ntpd start/stop/restart命令来 启动/停止/重启ntp服务了。

配置示例:

[root@redhat7 ~]# cat /etc/ntp.conf  | grep ^# -v | grep ^$ -v
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
restrict 192.168.121.0 mask 255.255.255.0 nomodify notrap
server 192.168.121.1 iburst
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

kylin3.2系统

kylin 3.2系统的ntp服务配置方式同kylin 3.0系统完全一致,请参见kylin 3.0系统部分进行配置。

kylin 3.3系统

kylin 3.3系统的ntp服务配置方式同kylin 3.0系统完全一致,请参见kylin 3.0系统部分进行配置。

在kylin 3.3系统中,除了使用service命令来控制ntp服务外,还可以使用systemctl命令加以控制,这也是kylin 3.3推荐的控制方式。其使用方法为:systemctl start/stop/restart ntpd.service ,分别表示 启动/停止/重启 ntp服务。

ntp服务状态查看

启动ntp服务后,我们需要检查ntp服务运行状态,可通过ntpstat命令查看简略状态,或者通过ntpq 命令查看详细状态。

ntpstat命令

使用ntpstat命令,可以查看当前设备是同哪台时间服务器进行通信,并显示矫正范围和频率。ntpstat命令输出
ntpstat解释如下

synchronised to local net at stratum 9      // 本NTP服务器层次为9,已向本地时钟源同步过
time correct to within 11 ms     // 时间校正到相差11ms之内
polling server every 64 s        // 每64秒会向上级NTP轮询更新一次时间

ntpq命令

  • *正在使用的上层NTP
  • +表示已连线,可提供时间更新的候补服务器
  • -表示不合格的NTP服务器
  • x表示远程服务器不可用

使用ntpq -p 命令可以查看ntp服务的详细运行状态ntpq -p命令输出
该命令结果中各个参数意义如下:

  • remote:时间服务器,前面带*表示当前对时服务器 ;
  • refid: 更上一层的ntp地址;
  • st:stratum层级,值越小优先级越高;
  • t:通信模式 u: unicast, b: broadcast, l: local ;
  • when: 距离上次对时的时间间隔(单位s);
  • poll:对时时间间隔(单位s);
  • reach:时间调准次数;
  • delay:对时网络传输延迟;
  • offset:时间偏差(单位ms);
  • jitter:时间偏差统计值,描述抖动情况;

配置开机自启

在确认ntp服务运行状态正常后,需要将ntp服务加入开机自启动列表中,避免设备重启后未启动ntp服务。

kylin3.0&kylin3.2系统

kylin 3.0和kylin 3.2系统使用chkconfig ntpd on命令将ntp服务设为开机自启动,使用chkconfig --list | grep ntpd 来检查是否已将ntp服务设为开机自启动。
将ntp服务设为开机自启动(kylin 3.0 & kylin 3.2)

kylin3.3及以上系统

kylin 3.3系统中需要使用systemctl enable ntpd.service 命令将ntp服务设为开机自启动,使用systemctl is-enabled ntpd.service 命令来检查是否已将ntp服务设为开机自启动。将ntp服务设为开机自启动(kylin 3.3)
至此,ntp时间同步服务全部配置完成。

常见问题

时差过大无法同步

由于ntp同步时间时不会进行跳跃式的调整,因此当两者时间差距较大时,使用ntp进行调整会非常缓慢甚至无法对时。

在这种情况下,建议先使用date -s “YYYY-MM-DD HH:MM:SS”命令进行手动调时,或者使用ntpdate <时间服务器ip 方式>进行粗略对时。待时间差异不大后,再开启ntpd服务进行时间同步。

需要额外注意的是,在ntpd服务运行时,无法使用ntpdate命令进行手动对时。需要加-u参数去对ntpdate -u 192.168.2.100

出现unsynchronised状态

当使用ntpdate手动对时,或者使用ntpstat命令查看ntp服务状态时,有时会出现无法对时的问题,其中会包含”no suitable synchronization”或”unsynchronised”关键词。在这里插入图片描述
无法进行对时
出现该异常的原因是当前设备同时间服务器间的通信异常,一般出现在开启系统防火墙且未开放123端口的情况下。

处理办法是关闭双方防火墙或者均开放udp 123端口。

关闭系统防火墙

kylin 3.0 & kylin 3.2

 service iptables stop    # 关闭防火墙
 chkconfig iptables off   # 关闭防火墙自启动

kylin3.3及以上

systemctl stop firewalld.service     # 关闭防火墙
systemctl disable firewalld.service  # 关闭防火墙自启动

开放123端口

kylin 3.0 & kylin 3.2

开放123端口:
   iptables -A INPUT -p udp --dport 123 -j ACCEPT
   iptables -A INPUT -p tcp --dport 123 -j ACCEPT
保存更改:
   service iptables save

kylin3.3及以上

 开放123端口:
        firewall-cmd --zone=public --add-port=123/udp --permanent  
        firewall-cmd --zone=public --add-port=123/tcp  --permanent    
  重新载入:
    firewall-cmd --reload

ntpq -p对时服务器前面未显示*号

查看日志

异常情况的日志
正常情况的日志

原因分析

出现这种情况一般都是三级客户端去找二级时钟服务器去对时,特别是市调找省调对的时候容易出现

原因其实是因为 ntp 与时钟源在 sanity check 时可能出现了错误,状态一直卡在 kernel time sync status 0040 阶段,时钟无法进入 0001 阶段。

服务器和时钟源计算出来的距离太大了,超过默认的1倍距离,需要降低了对时钟源的误差要求,将距离调大

可以粗暴的理解为网络质量较差,硬件环境不满足NTP同步时钟源的要求

增加配置参数tos maxdist 2,表示2倍距离,层级之下的其他服务器,也得加上这个配置,不然也会认为误差过大

与时钟源同步的服务器可以采用这个配置。tos和restrict都得写。其中tos maxdist 考虑到稳定性需要 ≥ 2

与二(≥ 2)级时钟服务器 可以采用这个配置。

  • tos和restrict都得写。
  • 其中tos maxdist 考虑到稳定性需要 ≥ 3,此处设置为 5。
  • 另外本地(127.127.1.0)地址的同步频率不能快于 二(≥ 2)级时钟服务器的同步频率,否则会因为频率的问题,优先同步到本地时钟源,如果不知道其规律,可考虑删除所有的 minpoll/maxpoll 值在这里插入图片描述
    参考链接:

https://ksmeow.moe/ntp/

https://developer.aliyun.com/article/459258

附录

ntp.conf中restrict参数说明

restrict 表示进行授权,授权哪些主机可以同时间服务器通信

  • 格式: restrict IP地址 mask 子网掩码 参数
  • 参数可以是以下变量
    • ignore: 拒绝所有类型的ntp连接;
    • nomodify: 客户端不能使用ntpc与ntpq程序来修改服务器的时间参数;
    • noquery: 客户端不能使用ntpq、ntpc等指令来查询服务器时间,等于不提供ntp的网络校时;
    • notrap: 不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务;
    • notrust: 拒绝没有认证的客户端;
    • nopeer: 不与其他同一层的ntp服务器进行时间同步;

完整ntp.conf文件示例

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.121.0 netmask 255.255.255.0 nomodify notrap

# Hosts on local network are less restricted.
#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 192.168.121.1 iburst perfer
server 192.168.121.2 iburst

#broadcast 192.168.1.255 key 42    # broadcast server
#broadcastclient      # broadcast client
#broadcast 224.0.1.1 key 42    # multicast server
#multicastclient 224.0.1.1    # multicast client
#manycastserver 239.255.254.254    # manycast server
#manycastclient 239.255.254.254 key 42  # manycast client

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
server  127.127.1.0  # local clock
fudge  127.127.1.0 stratum 8

# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

服务端配置示例

driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict  -6 ::1
restrict  192.168.42.0  mask 255.255.255.0  nomodify notrap  
server 127.127.1.0 iburst
tos cohort 1
fudge   127.127.1.0  stratum  8
includefile /etc/ntp/crypto/pw 
keys /etc/ntp/keys

客户端配置示例

driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
server 192.168.42.143 iburst      # 设置上游ntp服务器地址是192.168.42.143
tos cohort 1                      # 启用对具有相同层级别的多播服务器回复,有利于减少存在大量具有相同层级的客户端时的爆发
fudge   127.127.1.0  stratum 8
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
<think>好的,用户需要查找关于如何配置NTP服务器或客户端的信息。首先,我得回忆一下之前提供的引用内容,看看里面有哪些相关的步骤。 用户提到的引用[1]到[4]都涉及NTP配置,包括服务端和客户端。比如,引用[2]和[3]提到了修改ntp.conf文件,启动ntpd服务,还有客户端的配置引用[4]则提到了防火墙设置和服务状态检查。需要把这些步骤整理出来,分清楚服务端和客户端的操作。 首先,配置NTP服务器的话,应该包括安装软件、修改配置文件、启动服务、设置防火墙和验证这几个步骤。引用里提到要编辑/etc/ntp.conf,添加本地时钟源,比如server 127.127.1.0,还有stratum层级。然后启动ntpd服务,用systemctl命令,还要处理防火墙允许UDP 123端口,最后用ntpq -p或者ntpstat来检查状态。 客户端配置的话,同样要修改ntp.conf,指定服务器的IP地址,启动服务,并同步时间。Windows客户端的配置可能需要在时间设置里添加NTP服务器地址,这部分引用[2]和[4]都有提到。 需要注意用户可能是在内网环境配置,所以服务端可能需要使用本地时钟而不是外部服务器。另外,层级(stratum)的设置要合理,比如服务器设为8,客户端可能需要同步后层级加1。 另外,用户的问题里可能还关心如何验证配置是否成功,比如用ntpstat或者查看系统时间。还有防火墙配置这一步容易被忽略,需要提醒用户开放端口。 可能用户是系统管理员,需要详细的步骤,所以回答要分点清晰,步骤明确。同时,根据用户提供的引用内容,确保信息准确,比如配置文件的路径,命令的正确性。 最后,生成相关问题的时候,要考虑用户可能进一步的问题,比如服务无法启动怎么办,如何选择stratum,或者如何配置冗余服务器等。这些问题可以帮助用户深入理解配置过程。</think>### NTP服务器与客户端配置指南 #### 一、NTP服务器配置 1. **安装NTP服务** 在CentOS/RHEL系统中: ```bash yum install ntp ``` 配置文件路径为`/etc/ntp.conf`[^3][^4]。 2. **修改配置文件** ```bash vi /etc/ntp.conf ``` - 注释默认的外部时间源(如`server 0.centos.pool.ntp.org`) - 添加本地时钟源(适用于内网环境): ```bash server 127.127.1.0 fudge 127.127.1.0 stratum 8 # 设置层级为8[^2] ``` 3. **启动NTP服务** ```bash systemctl start ntpd # 启动服务 systemctl enable ntpd # 设置开机自启 timedatectl set-ntp yes # 启用时间同步[^2] ``` 4. **防火墙配置** 开放UDP 123端口: ```bash firewall-cmd --permanent --add-port=123/udp firewall-cmd --reload ``` 5. **验证状态** ```bash ntpq -p # 查看同步状态 ntpstat # 检查服务状态 timedatectl # 显示时间配置[^2] ``` --- #### 二、NTP客户端配置 1. **修改客户端配置** ```bash vi /etc/ntp.conf ``` - 指定NTP服务器IP: ```bash server 192.168.1.100 # 替换为实际服务器IP ``` 2. **启动服务并同步时间** ```bash systemctl restart ntpd ntpdate 192.168.1.100 # 强制立即同步[^1] ``` 3. **Windows客户端配置** - 打开「控制面板」→「日期和时间」→「Internet时间」→「更改设置」 - 输入NTP服务器地址,勾选「立即同步」[^2] --- #### 三、关键参数说明 1. **层级(stratum)**:服务器层级需逐级递增(如服务器设为8,客户端同步后为9)[^2]。 2. **本地时钟源**:`127.127.1.0`为本地硬件时钟,适用于无外网环境[^2][^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值