前言
内网服务器通过NTP服务器进行时间同步。
一、安装配置ntp
1、服务器是最小安装,不带ntp,但一般ntp都自带有了。
yum -y install ntp*
#检查NTP服务端的ntp版本
ntpq -c version
#查看当前时区
timedatectl
#设置timezone的时区
timedatectl set-timezone 'Asia/Shanghai'
timedatectl set-timezone Asia/Shanghai
2、编辑配置文件ntp.conf
vim /etc/ntp.conf
注释掉下图中4个服务器地址,添加常用的授时服务器
#添加常用的授时服务器,允许上层时间服务器主动修改本机时间
server ntp1.aliyun.com
server time1.aliyun.com
# 本地时钟服务器
server 127.127.1.0
fudge 127.127.1.0 stratum 10
#注释掉restrict default nomodify notrap nopeer noquery ,该项运行所有的网段来访问时钟服务器
#restrict default nomodify notrap nopeer noquery
#默认不允许访问
restrict default ignore
#配置明细,放开192.168.0.0段访问
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
3、开放防火墙123/udp端口
#防火墙放开123/udp端口
firewall-cmd --zone=public --add-port=123/udp --permanent
#重启防火漆
firewall-cmd --reload
4、启动ntp服务
#启动ntp服务
systemctl start ntpd
#设置开机自启
systemctl enable ntpd
#重启ntpd服务
systemctl restart ntpd
查看ntp服务状态
systemctl status ntpd
5、检查服务状态
#检查ntp端口是否正常开启(服务是否开启)
netstat -tunl | grep 123
# 确认NTP同步正常
ntpstat
提示 synchronised to NTP server 那证明已经连上了NTP服务器,
提示 unsynchronised 开头的 那证明还没有连上NTP服务器
#NTP服务器的列表
ntpq -p
ntpq -p列出了所有作为时钟源校正过本地NTP服务器时钟上层NTP服务器的列表,每一列的含义分别如下:
remote:响应请求的NTP服务器的名称(IP地址或域名),带“*”的表示本地NTP服务器与该服务器同步。
refid:远程NTP服务器使用的上一级ntp服务器的IP地址
st:远程NTP服务器的级别,由于NTP是层级结构,有顶端的服务器,多层的Relay
Server再到客户端。所以服务器从高到低,级别可以设定为1~16级。为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器。
t:本地NTP服务器与远程NTP服务器的通信方式。u:单播;b:广播;I:本地 when:上次成功请求后到现在的秒数
poll:本地NTP服务器与远程NTP服务器同步的时间间隔。
reach:这是一个八进制的值,用来测试衡量前八次查询是否成功和服务器连接。377表示都成功,0表示不成功
delay:网络延时,单位为微秒(μs)
offset:本地NTP服务器与远程NTP服务器的时间偏移,单位为毫秒(ms)。offset越接近于0,主机与NTP服务器的时间越接近
jitter:查询偏差的分布值,用于表示远程NTP的网络延时是否稳定,单位为微秒(μs)。
二、ntp.conf配置文件
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
# 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
restrict default ignore
# 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.168.0.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 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 ntp1.aliyun.com iburst
server time1.aliyun.com iburst
server 127.127.1.0 # 本地时钟服务器
fudge 127.127.1.0 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
# Enable public key cryptography.
#crypto
# 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
1、restrict 作用:对ntp做权限控制 ignore:忽略所有类型的NTP连接请求
nomodify:限制客户端不能使用命令ntpc和ntpq来修改服务器端的时间 noquery:不提供NTP网络校时服务
notrap:不接受远程登录请求 notrust:不接受没有经过认证的客户端的请求 【如果没有用任何参数,那么表示不做任何限制】
# 默认允许所有可连接客户端ntpdate到本机
restrict default nomodify notrap noquery
# 默认所有客户端禁止ntpdate到本机
restrict default ignore
#restrict default ignore的情况下,可以设置哪些客户可以ntpdate到本机
restrict 10.220.5.0 mask 255.255.255.0 nomodify notrap
#允许10.220.5.0/24 网段主机进行时间同步
2、server 作用:指定ntp服务器的地址 格式:server [ip or hostname] [perfer] 例子:server
127.127.1.0 <<<将当前主机作为时间服务器3、fudge 作用:设置时间服务器的层级 格式:fudge ip [stratum int] 例子:fudge 10.225.5.1
stratum 10 注意:fudge必须和server一块用, 而且是在server的下一行 stratum 0~15 0:表示顶级
10:通常用于给局域网主机提供时间服务
三、相关报错
在NTP客户端执行ntpdate -d命令更新NTP客户端的时间时,如果报以下错误:
no server suitable for synchronization found。需要检查NTP服务器的配置是否正确。
ntpdate -d 192.168.5.128
# 使用ntpstat 命令查看
ntpstat
若提示 unsynchronised 相关的 说明NTP服务器配置有问题,需要检查ntp.conf配置文件等, 配置本地NTP服务器 ntp.conf中必须要写 :
server 127.127.1.0 # 本地时钟服务器
fudge 127.127.1.0 stratum 10