ntpdate命令、ntp服务、chrony服务、ntp/chrony时间服务器的搭建

前言

环境:centos 7.9
在生产服务器中,我们需要保证服务器的时间是最准确的,所以需要进行时间同步,时间同步有2中方式,一种是:手动同步,另外一种是后台守护进程自动同步。
时间同步服务主要有2个软件可以实现,ntp和chrony,ntp是centos6默认的时间同步服务,在centos7之后,不在使用默认的ntp服务,改用chrony实现时间同步。
ntp的缺点:ntp启动之后,并不是立即同步时间为时间服务器的时间,比如,当前客户端是18:00,而时间服务器是18:15,当客户端启动ntp服务之后时间并不是立即同步为18:15,而是慢慢的慢慢的同步一点点时间,直到与时间服务器时间相同,之所以ntp这样设计,是基于这样的考虑,如果立即同步客户端的时候,那么之间的15分钟的日志记录将会是空白的。

ntpdate 命令手动同步时间

ntpdate 命令用于时间同步,可以将当前服务器的时间同步为时间服务器的时间(首先得有一个时间服务器),用法如下:

#安装ntpdate命令
yum install ntpdate
#手动同步时间,192.168.0.2为时间服务器IP
#所谓时间服务器,就是安装有某个可以同步时间的应用服务服务器,不能是任意服务器
ntpdate 192.168.0.2

内网环境下,通常,ntpdate 命令会配合crontab 来定时同步时间,如下:

0 12 * * * * /usr/sbin/ntpdate 192.168.0.2

ntpd服务自动同步时间(联网自动同步时间)

假如你的服务器是可以链接互联网的,那么也可以开启ntpd服务让系统自动进行互联网时间同步,如下:

systemctl enable --now ntpd
systemctl status ntpd
#ntpd服务默认配置文件/etc/ntp.conf里面配置了其去同步外网时间服务器

搭建ntp内网时间服务器(内网ntp时间服务器-搭建失败)

以上都假设服务器可以链接外网或者有一台时间服务器,但是在企业内网环境下,服务器不允许连接外网,所以需要自己搭建一台内网时间服务器,但是本身时间服务器自身的时间需要同步外网服务器的时候,所以时间服务器要可以连接外网,这时候时间服务器即作为客户端又做为服务端,客户端的意思是时间服务器要去同步外网时间,服务端的意思是自己要提供时间给内网其他机器同步。

#关闭防火墙selinux,因为内网其他客户端需要来连接同步本机时间
systemctl disable --now firewalld
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
setenforce 0
#安装ntp服务
yum install -y ntp

修改ntp.conf配置文件

安装完ntp服务之后,需要编辑ntp服务的配置文件,如下:

[root@node2 ~]# cat /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							#保持默认

# 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											#保持默认
# Hosts on local network are less restricted.
restrict 192.166.1.0 mask 255.255.0.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 参数用于定义上层时间服务器,即要与上层哪个时间服务器同步时间
server 0.centos.pool.ntp.org iburst						#0.centos.pool.ntp.org是centos官方时钟服务器的域名
server 1.centos.pool.ntp.org iburst						#1.centos.pool.ntp.org是centos官方时钟服务器的域名
server 2.centos.pool.ntp.org iburst						#2.centos.pool.ntp.org是centos官方时钟服务器的域名
server 3.centos.pool.ntp.org iburst						#3.centos.pool.ntp.org是centos官方时钟服务器的域名
server cn.pool.ntp.org iburst							#这是中国地区的NTP服务器池
server ntp.aliyun.com iburst							#这是阿里云提供的NTP服务器
server ntp1.aliyun.com iburst							#这是阿里云提供的NTP服务器
server ntp2.aliyun.com iburst							#这是阿里云提供的NTP服务器
server 127.0.0.1 iburst

#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

# 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
#参数解释
restrict <ipaddr> <netmask> [ignore | nomodiy | noquery | notrap | notrust | nokod ]
restrict的主要作用是对ntp做权限控制,表示哪些客户端可以链接我,其各个参数如下:
ignore:忽略所有类型的NTP连接请求
nomodiy:表示客户端不能更改本机NTP服务器的时间参数,但客户端可以通过NTP服务器校对时间
noquery:不提供NTP服务,即客户端不能使用ntpq,ntpc来查询ntp服务器,等于ntp服务器不提供校对时间服务
notrap:不提供trap远程事件登陆(Remote Event Logging)的功能,拒绝任何被发送的控制包
notrust:拒绝没有通过认证的客户端
kod:kod技术可以阻止kiss of death包(一种DOS攻击)对服务器的破坏,kod用来发送特定的Kiss-Of-Death回应报文,以减慢那些超过规定速度界限的客户机
nopeer:不和其他同一层的NTP服务器进行时间同步

server cn.pool.ntp.org iburst	#server 参数指定上层的时间服务器,表示来同步哪个时间服务器,iburst参数设置更快同步包响应。

调整 NTP 的同步时间间隔

默认ntp服务器在启动时会去同步时间,而后每隔1024s同步一次时间,如果需要修改这个同步间隔,可以通过如下步骤修改:

vi /etc/ntp.conf
#其中minpoll 4表示最小为2的4次方,maxpoll 5表示最大为2的5次方
server ntp.aliyun.com minpoll 4 maxpoll 5 iburst
#重启ntp使配置生效
systemctl restart ntpd
#查看是否生效,即可查看poll值为16(即2的4次方)
ntpq -p

启动 ntp服务

修改完配置文件就可以启动ntp服务了,如下:

systemctl enable --now ntpdate.service			#启动ntp服务,设置开机自启

启动ntp服务之后,以后ntp就会自动的去同步网络上的时间了。

至此,内网ntp服务器搭建完成,内网环境的其他服务器需要同步时间,直接就同步内网ntp服务器即可,使用命令ntpdate命令结合crontab计划任务定时同步即可,如下:

*/5 * * * * /usr/sbin/ntpdate 192.168.1.120 &>/dev/null

ntp问题

在测试过程中发现,时间服务器本身是可以正常同步外网时间了,但是其他客户端服务器去同步时间服务器却同步不成功,原因不详:

时间服务器是这样配置的:
[root@localhost ~]# grep -Ev '^$|#' /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict 127.0.0.1 
restrict ::1
restrict 192.168.544.5 mask 255.255.255.0 nomodify notraip	#开放网段,运行哪些客户端服务器来同步时间
server ntp.aliyun.com minpoll 4 maxpoll 5 iburst	#去同步网络时间服务器的时候
server 192.168.544.2 minpoll 2 maxpoll 5  iburst	#当网络时间服务器不可以的时候本机时间作为同步源
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

客户端服务器是这样配置的:
[root@ks1 ~]# grep -Ev '^$|#' /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict 127.0.0.1 
restrict ::1
server 192.168.544.2 minpoll 2 maxpoll 3  iburst	#去同步时间服务器的时候
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
[root@ks1 ~]# 
#以上不管是时间服务器还是客户端服务器,看着没有问题,并且防火墙selinux管关闭了,但是客户端时间总是同步不知道,ntpd进程也重启过了
#使用ntpdate 命令同步报错:
[root@ks1 ~]# ntpdate -d 192.168.544.2
30 Oct 13:43:08 ntpdate[10175]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
Looking for host 192.168.544.2 and service ntp
host found : 192.168.544.2
transmit(192.168.544.2)
receive(192.168.544.2)
transmit(192.168.544.2)
receive(192.168.544.2)
transmit(192.168.544.2)
receive(192.168.544.2)
transmit(192.168.544.2)
receive(192.168.544.2)
192.168.544.2: Server dropped: Leap not in sync
server 192.168.544.2, port 123
stratum 3, precision -22, leap 11, trust 000
refid [192.168.544.2], delay 0.02592, dispersion 0.00002
transmitted 4, in filter 4
reference time:    e8e9c4c8.9d79d359  Mon, Oct 30 2023 13:59:36.615
originate timestamp: e8e9c4d3.7bbb55ac  Mon, Oct 30 2023 13:59:47.483
transmit timestamp:  e8e9c0f3.0d6e51e0  Mon, Oct 30 2023 13:43:15.052
filter delay:  0.02617  0.02611  0.02606  0.02592 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 992.4305 992.4307 992.4305 992.4305
         0.000000 0.000000 0.000000 0.000000
delay 0.02592, dispersion 0.00002
offset 992.430595

30 Oct 13:43:15 ntpdate[10175]: no server suitable for synchronization found
[root@ks1 ~]# 
#一直同步不成功,不知道为什么
#放弃ntp了

chrony服务自动同步时间(联网自动同步时间)

假如你的服务器是可以链接互联网的,那么也可以开启chronyd服务让系统自动进行互联网时间同步,如下:

yum install chrony -y
systemctl enable --now chronyd
systemctl status chrony d
#chronyd服务默认配置文件/etc/chrony.conf里面配置了其去同步外网时间服务器

搭建chrony内网时间服务器(内网chrony时间服务器-搭建成功)

chrony是centos7默认的时间同步软件,chrony比ntp更好,ntp问题太多了,同步时间又慢,chrony同步时间精度比ntp高,所以推荐时间chrony来同步时间。

#关闭防火墙selinux,因为内网其他客户端服务器需要来同步本机时间
systemctl disable --now firewalld
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
setenforce 0
#安装chrony
yum install chrony -y
#查看配置配置文件
[root@localhost ~]# cat /etc/chrony.conf 
# 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		#默认是从centos官网的时间服务器同步时间
#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				#修改为从阿里云提供的NTP服务器同步时间
server ntp1.aliyun.com iburst				#修改为从阿里云提供的NTP服务器同步时间
server ntp2.aliyun.com iburst				#修改为从阿里云提供的NTP服务器同步时间
#iburst 参数表示当服务器可达时发送8个数据包而不是通常的1个数据,包间隔2秒,该参数可加快初始同步速度

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
#启用内核模式,系统时间每11分钟会拷贝到实时始终(RTC)默认启用该参数,保持默认即可
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
allow 192.168.544.0/16			#去掉注释,表示允许指定网段的其他客户端服务器来同步本机的时间
#allow 0.0.0.0/0				#0.0.0.0表示所有网段服务器

# Serve time even if not synchronized to a time source.
#去掉注释,启用该参数,表示当前server参数指定的时间服务器都无法同步的时候,本机的时间仍可以作为时间源供客户端同步
local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking
[root@localhost ~]# 
#启动chrony并设置开机自启
systemctl enable --now chronyd
chronyd的监听端口是udp/323

客户端配置chrony

上面服务器已经配置了chrony时间服务器,限制只要客户端去同步它的时间即可:

#客户端安装chrony
yum install chrony -y
vim /etc/chrony.conf			#编辑配置文件
server 192.168.544.2 iburst		#将server参数改为内网时间服务器的IP即可
systemctl enable --now chronyd
date		#可以看到时间马上就同步的

chrony客户端工具

#查看帮助
chronyc  help
#查看同步情况
chronyc sources -v 

总结

1、yum install -y ntp			#安装内网ntp服务器,ntp服务器要去同步网络时间,所以要连外网
2、vim /etc/ntp.conf			#修改配置文件,开放权限让内网其他服务器可以链接自己,修改网络时间源的地址
3、systemctl start ntpd			#启动ntp服务
4、systemctl enable ntpd		#设置开机自启ntp服务,以后ntp服务会自动的定时去同步网络时间服务器
5、crontab -e					#内网其他服务器编辑计划任务,定时同步内网ntp服务器
	*/5 * * * * /usr/sbin/ntpdate 192.168.1.120 &>/dev/null

#建议不要使用ntp软件来同步时间了,改用chrony同步时间更好,改用chrony来搭建时间服务器更好
#使用chrony搭建NTP时间服务器
yum install chrony -y
vim /etc/chrony.conf			#主要修改或启用两个参数
server ntp1.aliyun.com iburst			#修改为从阿里云提供的NTP服务器同步时间
local stratum 10  	#去掉注释,启用该参数,表示当前server参数指定的时间服务器都无法同步的时候,本机的时间仍可以作为时间源供客户端同步
systemctl enable --now chronyd
#客户端安装chrony去同步时间服务器的时间
yum install chrony -y
vim /etc/chrony.conf			#主要修改server参数
server 192.168.544.2 iburst		#修改自己内网的时间服务器IP即可
systemctl enable --now chronyd

#以上,当我们需要配置服务器的时间时,可以这样做
#临时同步
yum install ntpdate -y
ntpdate 192.168.544.2
#当然也可以使用定时任务来定时同步
crontab -e
*/5 * * * * /usr/sbin/ntpdate 192.168.1.120 &>/dev/null

#更好的时间同步方案,安装chrony
yum install chrony -y
vim /etc/chrony.conf			#主要修改server参数
server 192.168.544.2 iburst		#修改为自己内网的时间服务器IP即可
systemctl enable --now chronyd
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值