Linux 时间一致环境ntp服务器部署

it2026-03-01  2

背景

生产环境中,一个业务系统往往涉及几十台甚至上百台主机或云主机构成,有些管理系统对整个环境时间一致性由强要求,即使没有,保证环境中时间的一致性,有利于业务交互时时间匹配,保证事务的一致性,甚至环境中的监控系统,也需要监测时间一致性,保证数据上报正常。

ntp架构

下图来源网络,在前端管理节点上部署ntp服务器,且采取主备模式,局域网的ntp服务器先于外网ntp同步完成,局域网内再同本地ntp服务器同步时间。实际环境中,可借助类似架构,两个管理节点可以用HAproxy+keepalive做Heartbeat,并设置对外提供ntp服务的VIP地址192.168.0.103。

ntp环境时间检查

1)系统时区检查:

# date Thu Oct 22 16:46:15 CST 2020 //CST代表了中国标准时间(+8区) #date -R Thu, 22 Oct 2020 16:46:18 +0800 //显示当前为+0800时区,即东八区,中国标准时间 #zdump Asia/Taiwan //zdump命令查看相应时区时间 Asia/Taiwan Thu Oct 22 09:02:23 2020 Asia #zdump Asia/Shanghai Asia/Shanghai Thu Oct 22 17:02:35 2020 CST #tzselect # 使用tzselect命令获取配置时区 Please identify a location so that time zone rules can be set correctly. Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean 10) Pacific Ocean 11) none - I want to specify the time zone using the Posix TZ format. #? # timedatectl status //查看当前时间配置 Local time: Thu 2020-10-22 17:57:57 CST Universal time: Thu 2020-10-22 09:57:57 UTC RTC time: Thu 2020-10-22 09:57:57 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a #echo "export TZ='Asia/Shanghai'" >> /etc/profile // #source /etc/profile #timedatectl set-timezone Asia/Shanghai //命令配置时区 #ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime //做软链接 # hwclock --show //查看硬件时钟时间 Thu 22 Oct 2020 06:20:40 PM CST -1.001636 seconds #hwclock --set --date="19/03/12 21:55" //设置硬件时钟时间 #hwclock --systohc //系统及硬件时钟同步,hc代表硬件时间,sys代表系统时间,systohc表示以系统时间为基准,同步至硬件时间

NTP服务器部署及配置

1)rpm -qa ntp* //验证服务器端NTP服务是否已安装,由下可知已经安装 ntpdate-4.2.6p5-28.el7.centos.x86_64 ntp-4.2.6p5-28.el7.centos.x86_64 2)rpm -ql ntp //查看ntp相关的文件 …… /etc/ntp.conf //ntp服务器的主配置文件 /usr/lib/systemd/system/ntpd.service //开机启动ntpd脚本文件 ……

3)常用的国内NTP服务器参考: times.aliyun.com

ntp.aliyun.com

cn.pool.ntp.org

0.cn.pool.ntp.org

1.cn.pool.ntp.org

4)同步局域网ntp服务器与外部ntp的时间: #ntpdate times.aliyun.com 22 Oct 18:30:07 ntpdate[23856]: adjust time server 120.25.115.20 offset -0.101925 sec

5)查看本地的ntp服务: #ntpstat Unable to talk to NTP daemon. Is it running? #vi ntp.conf //查看修改ntp服务器配置文件

driftfile /var/lib/ntp/drift //#系统时间与BIOS事件的偏差记录 restrict default nomodify notrap nopeer noquery //拒绝所有IPv4的client连接此NTP服务器 restrict -6 default kod nomodify notrap nopeer noquery # 拒绝所有IPv6的client连接此NTP服务器 restrict 127.0.0.1 # 放行本机localhost对NTP服务的访问,利用restrict 来管理权限控制 restrict ::1 restrict -6 ::1 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 放行192.168.1.0网段主机与NTP服务器进行时间同步 # 允许局域网内设备与这台服务器进行同步时间.但是拒绝让他们修改(nomodify)服务器上的时间 restrict 192.168.35.0 mask 255.255.255.0 nomodify notrap restrict 172.16.10.0 mask 255.255.255.0 nomodify notrap 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 ntp.aliyun.com iburst includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor logfile /var/log/ntpstats/ntpd.log # 定义ntp日志目录 pidfile /var/run/ntp.pid # 定义pid路径

参数说明:

(1)配置文件中利用restrict 来管理权限控制,语法如下:

Restrict [IP] mask [netmask_IP] [parameter]

其中,Parameter 各参数意义如下:

ignore :拒绝所有类型的NTP联机。 nomodify: 客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端可透过这部主机来进行网络校时; noquery:客户端不能够使用ntpc与ntpq等指令来查询时间服务器,不提供NTP的网络校时。 notrap:不提供trap 这个运程事件登入的功能。 notrust:拒绝没有认证的客户端。 Kod:kod技术可以阻止“Kiss of Death “包对服务器的破坏。 Nopeer:不与其他同一层的NTP服务器进行时间同步。

(2)利用server 设定上层NTP服务器,语法格式:server [IP or hostname] [prefer] 参数主要如下: perfer:表示优先级最高 burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。 iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。

5)启动ntp服务: #systemctl start ntpd #systemctl enable ntpd #systemctl status ntpd ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2020-10-22 18:49:04 CST; 4s ago Process: 24383 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 24384 (ntpd) CGroup: /system.slice/ntpd.service └─24384 /usr/sbin/ntpd -u ntp:ntp -g

Oct 22 18:49:03 systemd[1]: Starting Network Time Service… Oct 22 18:49:04 ntpd[24383]: ntpd 4.2.6p5@1.2349-o Fri Apr 13 12:52:27 UTC 2018 (1) Oct 22 18:49:04 ntpd[24384]: proto: precision = 0.104 usec Oct 22 18:49:04 ntpd[24384]: 0.0.0.0 c01d 0d kern kernel time sync enabled Oct 22 18:49:04 y systemd[1]: Started Network Time Service.

#ss -tunlp | grep ntp //ntpd服务使用UDP/tcp的123端口 tcp UNCONN 0 0 172.10:123 : users:((“ntpd”,24384,20)) tcp UNCONN 0 0 172…2:123 : users:((“ntpd”,24384,19)) tcp UNCONN 0 0 127.0.0.1:123 : users:((“ntpd”,24384,18)) tcp UNCONN 0 0 :123 : users:((“ntpd”,24384,16)) tcp UNCONN 0 0 fe80::f816:3eff:feb9:fa7a:123 ::: users:((“ntpd”,24384,23)) tcp UNCONN 0 0 fe80::f816:3eff:fe8d:d89a:123 ::: users:((“ntpd”,24384,22)) tcp UNCONN 0 0 ::1:123 ::: users:((“ntpd”,24384,21)) tcp UNCONN 0 0 :::123 ::😗 users:((“ntpd”,24384,17)) #ntpstat //列出NTP服务器是否与上层联机 synchronised to NTP server (116.203.151.74) at stratum 3 time correct to within 374 ms polling server every 64 s #ntptrace –n //列出目前NTP服务器(第一层)与上层NTP服务器(第二层)彼此之间的关系,注意:该命令需要安装ntp-perl包 #ntpq -p //列出目前我们的NTP与相关的上层NTP的状态 remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由"+“则代表目前正在作用钟的上层NTP,如果是”*"则表示也有连上线,不过是作为次要联机的NTP主机。

refid:参考的上一层NTP主机的地址

st:即stratum阶层

when:几秒前曾做过时间同步更新的操作

poll:下次更新在几秒之后

reach:已经向上层NTP服务器要求更新的次数

delay:网络传输过程钟延迟的时间

offset:时间补偿的结果

jitter:Linux系统时间与BIOS硬件时间的差异时间

注意:ntp服务默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可在配置文件添加 SYNC_HWCLOCK=yes;ntpd 有一个自我保护的机制:如果本机与上源时间相差太大,ntpd 不会运行时间同步操作,所以新设置的时间服务器一定要先 ntpdate 从上源取得时间初值, 然后启动 ntpd服务。ntpd服务运行后,先是每64秒与上源NTP服务器同步一次,根据每次同步时测得的误差值经复杂计算逐步调整自己的时间,随着误差减小,逐步增加同步的间隔。每次跳动,都会重复这个调整的过程。

cron定时任务脚本

0 12 * * * /usr/sbin/ntpdate ntp_server_ip vim ntp.sync

#!/bin/bash # # $Id: sync-clock,v 1.6 2009/12/23 15:41:29 jmates Exp $ # # Use ntpdate to get rough clock sync with department of Genome Sciences # time server. NTPDATE=/usr/sbin/ntpdate SERVER="ntp_server_ip " # if running from cron (no tty available), sleep a bit to space # out update requests to avoid slamming a server at a particular time if ! test -t 0; then MYRAND=$RANDOM MYRAND=${MYRAND:=$$} if [ $MYRAND -gt 9 ]; then sleep `echo $MYRAND | sed 's/.*\(..\)$/\1/' | sed 's/^0//'` fi fi $NTPDATE -su $SERVER # update hardware clock on Linux (RedHat?) systems if [ -f /sbin/hwclock ]; then /sbin/hwclock --systohc fi

注意:ntpdate同步时间会造成时间的突变和跳跃,对一些依赖时间的程序和服务会造成影响。比如sleep,timer等。而且ntpd服务可以在修正时间的同时,修正CPU Tick。因此理想的做法为,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间。

最新回复(0)