关于时间ntp服务及较时问题

时间过得好快,一下子又是一周了。刚看了一下时间,上周五写的VPN,一周时间忙这忙那,瞎忙一通。

今天补一篇吧,要不一周都更不了一次,自己都对不起自己了。

今天写的是NTP,较时服务。仿佛看起来不太用得上,其实很重要。我这里有三个场景都碰到过因为时间不对而导致的问题。第一次是遇到考试服务器时间不准,而导致上机考试时间推后的问题。其实WINDOWS自已会去互联网上去找到微软自己的NTP较时服务器去较时。但因为考试要求不能连接外网,而且就算是能连外网也会因为微软默认的NTP因某些情况不稳定而较不了时。第二次是视频监控,因为视频的硬盘录像机有很多台,而且视频监控要求必须是专网且和互联网不联通的,导致录像时间有偏差,且多个录像时间不一致。第三次是某门禁系统,会定时通过WEBSERVICE从省级服务器取数据,一直很正常。忽然出现取不了数据了,最终排查的结果是上下级服务器时间不一致,时间差了2分钟,导致取数据报错。

在这样的应用场景里如果完全靠人用手工去定期较时,肯定是最不稳定、可靠性最差的方案。也就是我前面三次出故障的原因。

1、针对第三种情况,因为能上互联网。就直接修改系统的NTP服务器地址,把默认的微软的NTP服务器改成国内的就可以了。

这里提供几个国内能用的NTP服务器

中国[China] cn.ntp.org.cn [58.220.207.226][47.92.108.218][202.112.29.82] [202.108.6.95] [120.25.108.11] [182.92.12.11] [115.28.122.198]

2、针对第一和第二种情况,就只能在内网架设NTP服务器了。如果有充足的预算,可以购买专用的NTP服务器,仿佛国内只有一、二家有生产许可,利用卫星信号较时。我这里当然没有这么多钱,只能用另一种方案。用双网卡的服务器来做NTP服务器,一块连互联网,一块连内网。当然连接的二个端口安全策略要做好,只许可NTP,其他全部封掉。我最终是用了虚拟化技术,用了一台虚机来实现的。NTP的软件是网上找到一款WINDOWS下面的较时软件,Quick NTP。百度里可以很轻松的搜索到软件和安装教程。我这里就不多说了,安装好后如下图所示。后面,我们只需把内网所有需要较时的服务器全部指到我们自己的这台NTP服务器的内网地址就可以了。

 

3、补充一下互联网巨头们自建的NTP服务(来源:知乎,链接:https://www.zhihu.com/question/30252609/answer/108840850

阿里云提供公共NTP服务,由于阿里云有大量外售的云主机以及YunOS手机天猫魔盒等都需要精确时间同步,因此建设了这一基础设施,公网均可访问,授时信号来自GPS、北斗两套卫星信号,并配备原子钟守时,以下7个域名提供服务,大家可以直接使用。
http://time1.aliyun.com
http://time2.aliyun.com
http://time3.aliyun.com
http://time4.aliyun.com
http://time5.aliyun.com
http://time6.aliyun.com
http://time7.aliyun.com
windows服务器,或者你使用ntpdate,那么可以直接使用http://time.pool.aliyun.com

另外,如果服务器在国外,苹果提供的公共NTP服务,以下7个域名

http://time1.apple.com
http://time2.apple.com
http://time3.apple.com
http://time4.apple.com
http://time5.apple.com
http://time6.apple.com
http://time7.apple.com

Google(谷歌)提供的NTP服务,以下4个域名
http://time1.google.com
http://time2.google.com
http://time3.google.com
http://time4.google.com

 

3、自建Centos ntp服务器

自建的WINDOWS时间服务,有些不稳定。所以近期改成了Centos的,还是虚拟机模式。安装方法如下:

(1)最小化安装好Centos 7之后,安装相关的服务。

yum -y install wget curl net-tools lsof zip unzip ntpdate ntp

其中ntpdate ntp是时间相关服务。

(2)查看当前服务器时区&列出时区并设置时区(如已是正确时区,请略过):

timedatectl
timedatectl list-timezones
timedatectl set-timezone Asia/Shanghai

(3)修改NTP配置:

vim /etc/ntp.conf

配置文件如下:(请对应需求修改)

For more information about this file, see the man pages

ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

新增:日志目录.

logfile /var/log/ntpd.log

Permit time synchronization with our time source, but do not

permit the source to query or modify the service on this system.

restrict default 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 ::1

这一行的含义是授权172.16.128.0网段上的所有机器可以从这台机器上查询和同步时间.

restrict 172.16.128.0 mask 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 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 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst
server 2.cn.pool.ntp.org iburst
server 3.cn.pool.ntp.org iburst

新增:当外部时间不可用时,使用本地时间.

server 172.16.128.171 iburst
fudge 127.0.0.1 stratum 10

broadcast 192.168.1.255 autokey # broadcast server

broadcastclient # broadcast client

broadcast 224.0.1.1 autokey # multicast server

multicastclient 224.0.1.1 # multicast client

manycastserver 239.255.254.254 # manycast server

manycastclient 239.255.254.254 autokey # manycast client

新增:允许上层时间服务器主动修改本机时间.

restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.cn.pool.ntp.org nomodify notrap noquery
restrict 2.cn.pool.ntp.org nomodify notrap noquery

Enable public key cryptography.

crypto

includefile /etc/ntp/crypto/pw

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

Enable writing of statistics records.

statistics clockstats cryptostats loopstats peerstats

Disable the monitoring facility to prevent amplification attacks using ntpdc

monlist command when default restrict does not include the noquery flag. See

CVE-2013-5211 for more details.

Note: Monitoring will not be disabled with the limited restriction flag.

disable monitor

(4)设置系统开机自启动:

systemctl enable ntpd

systemctl enable ntpdate

systemctl is-enabled ntpd

启动NTP服务器:

systemctl start ntpdate

systemctl start ntpd

(5)防火墙配置

我还是比较习惯用iptables,所以先停自带firewall。操作如下:

systemctl status firewalld.service #检测是否开启了firewall

systemctl stop firewalld.service #关闭firewall

systemctl disable firewalld.service #禁止firewall开机自启

安装iptables

yum install -y iptables-services  #安装iptables

systemctl start  iptables.service #开启iptables

systemctl enable  iptables.service #允许iptablesl开机自启

配置iptables,只开ntp端口123

iptables -F INPUT #清除INPUT下的所有规则,只有在初始化的时候才能使用!!!

iptables -I INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT #本条规则为放行ESTABLISHED与RELATED状态的包,也就是本机主动发起的对话,对方机器回应时直接放行。这条规则可用于安装调试时使用,服务器正常使用时建议禁用,用明确路由的规则来代替。这里给时间服务对时使用

iptables -I INPUT -p udp –dport 123 -j ACCEPT #无条件开放123时间服务端口

iptables -A INPUT -j DROP #在INPUT最后追加一条对所有包进行DROP的规则

service iptables save #保存当前规则

(6)查看ntp服务器与上层ntp服务器的状态,如果这里显示正常,则配置全部完成。

ntpq -p

其中,remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差

或者用watch ntpq -p 每隔2秒,刷新一次状态。

2023年4月更新 Centos 8较时

CentOS上本来可以用ntpd进行时间同步,升级到8之后,官网repo不再支持ntpd,改用chrony

sudo yum install chrony

修改chrony配置文件,注释掉centos的时间服务器,改成阿里的时间服务器:

sudo vim /etc/chrony.conf

server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst
设置chronyd开机启动:

sudo systemctl enable chronyd
手工启动chronyd

sudo systemctl start chronyd
查看状态

sudo systemctl status chronyd

CentOS 8有个timedatectl命令可以看时间相关的信息,chrony启动后,可以看到NTP是yes

sudo timedatectl
Local time: Fri 2021-11-26 16:00:40 CST
Universal time: Fri 2021-11-26 08:00:40 UTC
RTC time: Fri 2021-11-26 08:00:40
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
有些环境,只有一台服务器有公网IP,此时需要在这台服务器上下载安装包

cd /tmp
yumdownloader chrony
sudo yumdownloader libseccomp
scp libseccomp-2.3.1-4.el7.x86_64.rpm username@other-server-local-ip:/tmp/.
scp ./chrony-3.4-1.el7.x86_64.rpm username@other-server-local-ip:/tmp/.
在有公网的服务器上ssh到另一台服务器

ssh -l username other-server-local-ip
cd /tmp
sudo yum localinstall ./libseccomp-2.3.1-4.el7.x86_64.rpm
sudo yum localinstall ./chrony-3.4-1.el7.x86_64.rpm
修改有公网访问权限的服务器上的配置文件,把允许客户端访问的注释打开

# Allow NTP client access from local network.
allow 192.168.0.0/16
重启chronyd服务

netstat -na|grep -i udp
能看到upd 123端口

udp 0 0 0.0.0.0:123 0.0.0.0:*

未经允许不得转载:生于七零年代 » 关于时间ntp服务及较时问题

赞 (3) 打赏

评论

2+9=

觉得文章有用就打赏一下文章作者

微信扫一扫打赏