CentOS7 配置NTP服务器


前言

内网服务器通过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
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是在 CentOS 7 上搭建 NTP 服务器的步骤: 1. 安装 NTP:运行以下命令安装 NTP: ``` sudo yum install ntp ``` 2. 配置 NTP:编辑 `/etc/ntp.conf` 文件,配置服务器地址和允许访问的客户端地址。例如,以下是一个示例配置: ``` server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict <客户端IP地址> mask <子网掩码> nomodify notrap ``` 其中,`server` 指定 NTP 服务器地址,`restrict` 用于限制访问控制。 3. 启动 NTP:运行以下命令启动 NTP 服务并设置为开机自启: ``` sudo systemctl start ntpd sudo systemctl enable ntpd ``` 4. 验证 NTP:使用 `ntpq` 命令验证 NTP 是否正常工作。例如,运行以下命令: ``` ntpq -p ``` 如果 NTP 正常工作,将显示类似以下内容的输出: ``` remote refid st t when poll reach delay offset jitter ============================================================================== *ntp1.example.c .GPS. 1 u 154 256 377 0.862 -30.200 1.835 +ntp2.example.c .PPS. 1 u 173 256 377 0.991 -30.230 1.309 -ntp3.example.c .GPS. 1 u 130 256 377 1.003 -29.990 1.935 ``` 其中,`*` 表示当前服务器NTP服务器,`+` 表示当前服务器NTP 辅助服务器,`-` 表示当前服务器NTP 被动服务器。 以上就是在 CentOS 7 上搭建 NTP 服务器的步骤。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值