版本查看方法
使用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
##下面两行默认是拒绝所有来源的任何访问
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解释如下
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服务的详细运行状态
该命令结果中各个参数意义如下:
- 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服务设为开机自启动。
kylin3.3及以上系统
kylin 3.3系统中需要使用systemctl enable ntpd.service 命令将ntp服务设为开机自启动,使用systemctl is-enabled ntpd.service 命令来检查是否已将ntp服务设为开机自启动。
至此,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