服务器大本营

 找回密码
 我要入营

QQ登录

只需一步,快速开始

查看: 12|回复: 0

NTP服务器怎么设置?Linux搭建NTP时间服务器配置详解

[复制链接]

7万

敬重

854

主题

207

精华

管理员

积分
9222

启航之章进阶之印里程之碑突破之证飞跃之星蜕变之勋卓越之路龙年行大运

QQ
发表于 昨天 11:29 | 显示全部楼层 |阅读模式

我要入营,结交更多好友,开启更多功能,轻松玩转服务器大本营!

您需要 登录 才可以下载或查看,没有账号?我要入营

x
一、NTP服务
NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。
NTP服务器就是利用NTP协议提供时间同步服务的。

二、NTP服务搭建
1、环境准备
使用VMware虚拟化安装linux系统并将网络环境设置为NAT,可以连上外网
主机名称 IP地址 系统版本 角色
linux-node1 192.168.66.11 CentOS 7 NTP服务器
linux-node2 192.168.66.12 CentOS 7 客户端
注意:关闭系统selinux和防火墙

2、安装NTP服务
查看系统是否安装 ntp服务
  1. ~]# rpm -qa ntp
  2. ntp-4.2.6p5-28.el7.centos.x86_64
复制代码

若没有安装可以使用 YUM 命令进行安装即可
  1. ~]# yum -y install ntp
复制代码

3、配置NTP服务
ntp 服务器默认是不运行客户端进行时间同步的,所有我们需要配置文件设置允许。NTP服务的默认配置文件是/etc/ntp.conf
  1. ~]# cp /etc/ntp.conf{,_$(date +%Y%m%d%H)}
  2. ~]# vim /etc/ntp.conf
  3. ………省略内容………
  4. #restrict default kod nomodify notrap nopeer noquery 注释此行内容
  5. # nomodify 客户端不可以修改时间参数但是可以同步时间服务器,添加以下内容
  6. restrict default nomodify   
  7. 以下为 NTP服务默认的时间同步源,先将其注释
  8. #server 0.centos.pool.ntp.org iburst
  9. #server 1.centos.pool.ntp.org iburst
  10. #server 2.centos.pool.ntp.org iburst
  11. #server 3.centos.pool.ntp.org iburst
  12. 添加新的时间同步源
  13. server time1.aliyun.com
  14. ………省略内容………
复制代码

4、启动NTP服务
客户端先将系统优化是定时任务自动同步时间服务器的定时任务注释,如果不注释的话可能会冲突
提示:本地的 ntp 时间服务器会跟互联网的时间服务器冲突,只能选择一个进行同步。
  1. ~]# crontab -l
  2. # time sync by albert at 2019-02-10
  3. #*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &>/dev/null
  4. ~]# systemctl enable ntpd.service
  5. Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
  6. ~]# systemctl start ntpd.service
  7. # 查看ntp服务器的详细状态
  8. ~]# ntpq -p
  9.      remote           refid      st t when poll reach   delay   offset  jitter
  10. ==============================================================================
  11. 203.107.6.88    10.165.84.13     2 u   28   64    1   15.767  708.472   0.000
  12. # 显示网络时间同步状态
  13. # 使用ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下.
  14. # 刚启动的时候,一般是:
  15. ~]# ntpstat
  16. unsynchronised
  17.   time server re-starting
  18.    polling server every 8 s
  19. # 连接并同步后:
复制代码

三、客户机时间同步
客户机要等几分钟再与新启动的 ntp 服务器进行时间同步,否则会提示 no server suitable for synchronization found 错误。
  1. ~]# ntpdate 192.168.66.11
  2. 14 Feb 11:27:59 ntpdate[6528]: no server suitable for synchronization found
复制代码

客户端同步时间的方法:
  1. ~]# ntpdate 192.168.66.11
  2.   14 Feb 11:37:25 ntpdate[1453]: step time server 192.168.66.11 offset 0.880807 sec
  3. # 将命令放入计划任务即可
  4. ~]# crontab -l
  5. */5 * * * * /usr/sbin/ntpdate 192.168.66.11 &>/dev/null
复制代码

四、注意事项
当我们搭建 NTP 服务器的时候一定要先将后面优化的时间同步定时任务注释,不然会产生冲突

常见错误 1:
No association ID's returned
检查网络是否连通:ping www.baidu.com

常见错误 2:
  1. ~]# ntpdate 192.168.66.11
  2. Error resolving 192.168.66.11: Servname not supported for ai_socktype (-8)
  3. 14 Feb 11:37:25 ntpdate[1411]: Can't find host 192.168.66.11: Servname not supported for ai_socktype (-8)
  4. 14 Feb 11:37:25 ntpdate[1411]: no servers can be used, exiting
复制代码
客户机要等几分钟再与新启动的 ntp 服务器进行时间同步,否则会提示 no server suitable for synchronization found 错误。
可能原因:客户端缺少什么配置文件
检查客户端是否可以与互联网的时间服务器同步,若不行,就是客户端的问题!排查客户端故障

感谢您的阅读,服务器大本营-技术文章内容集合站,助您成为更专业的服务器管理员!
一入IDC深似海,从此你我是良人!
您需要登录后才可以回帖 登录 | 我要入营

本版积分规则

点击直接加入[服务器大本营QQ频道]
滴!摸鱼时间到~
Loading...

QQ|Archiver|手机版|网站地图|服务器大本营 ( 赣ICP备2021009089号 )

GMT+8, 2025-8-8 15:09 , Processed in 0.063075 second(s), 26 queries , Gzip On.

Powered by 服务器大本营

© 2021-2023 联系飞飞

快速回复 返回顶部 返回列表