虽然NTPD已经不再是CentOS缺省的时间同步软件,由于其长时间占据这个地位,还是将其作为网络时间同步搭建的工具之一来进行介绍。

NTP基础知识

基础知识请参看如下文章,此处不再赘述。

  • https://liumiaocn.blog.csdn.net/article/details/88414432

事前准备

用途 IP OS版本
NTP服务器 192.168.163.131 CentOS7.6
NTP客户端 192.168.163.132 CentOS7.6
[root@host131 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@host131 ~]#

安装准备

安装命令:yum install -y ntp
注:包含了ntp和ntpdate,在本文的示例中客户端和服务器端均采用同样的安装包

[root@host131 ~]# rpm -qa |grep ntp
ntp-4.2.6p5-28.el7.centos.x86_64
ntpdate-4.2.6p5-28.el7.centos.x86_64
[root@host131 ~]# ntpd --version
ntpd 4.2.6p5exit 0
[root@host131 ~]#

端口开放:如果有防火墙的话,需要注意ntpd的服务是使用UDP方式在123端口进行服务。要么关闭,要么加入规则对端口放行。
firewall设定:
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload

安装设定

设定文件:/etc/ntp.conf

设定文件ntp.conf是ntpd服务的重中之重,设定格式如下所示:

设定格式:
设定指令 指令参数

设定指令:server

使用介绍:
使用 server ip或者server hostname方式进行设定
设定例:server 192.168.163.131
设定例:server host131

参数介绍:prefer
perfer参数用于设置优先使用的NTP服务器
设定例:
server 192.168.163.131 prefer
server 192.168.163.132
设定说明:默认使用192.168.163.131进行同步,192.168.163.132为备用NTP服务器

参数介绍:burst
当NTP服务器可用时,向其发送一些列并发包进行检测
设定例:server 192.168.163.131 burst

参数介绍:iburst
当NTP服务器不可用时,向其发送一些列并发包进行检测
设定例:server 192.168.163.131 iburst

设定指令:fudge

说明:fudge英文含义具有“欺骗”的意思。这条指令必须紧跟server指令,为server指令提供附加信息。
使用示例:
server 127.127.1.0
fudge 127.127.1.0 stratum 5
场景说明:指定本地时钟的层为5,在无法取到外部服务的时候使用本地时钟,在无法连接外网的局域网环境中运用较多。此时一般将其他的server设定全部删除或注释即可。

设定指令:logfile

参数介绍:日志的FULL PATH
说明:默认情况下,NTP服务器的日志保存在/var/log/messages中,但也可自行指定
使用例:logfile /var/log/ntpd.log

设定指令:driftfile

参数说明:/var/lib/ntp/drift
说明:根据实际时间计算出服务器增减时间的比率,通过指定文件进行记录,调整时间时会根据此文件进行补偿。此指令一般无需修改

设定指令:restrict

使用介绍
restrict [地址] mask [子网掩码] [参数]

参数说明:ignore
说明: 拒绝所有类型的NTP连接

参数说明:nomodify
说明: 客户端无法修改服务器端时间

参数说明:noquery
说明: 客户端无法查询服务器端时间

参数说明:notrap
说明: 不提供远程日志功能

参数说明:notrust
说明: 不允许接入没有认证的客户端

参数说明:nopeer
说明: 不允许和同一层的其他服务器进行时间同步

使用示例:
restrict 192.168.163.131 mask 255.255.255.0 nomodify notrap
说明:这也是缺省的ntp.conf被注释的使用例,允许192.168.163.*的机器进行连接,但是不允许修改主机时间,不提供远程日志功能

设定文件:/etc/ntp/step-tickers

用于记录使用ntpdate同步时间时用到的NTP服务器信息

[root@host131 ~]# cat /etc/ntp/step-tickers
# List of NTP servers used by the ntpdate service.0.centos.pool.ntp.org
[root@host131 ~]#

设定示例(服务器侧)

在本文中,进行最小的修改启动ntpd,场景为无法连接外网的NTP服务器和NTP客户端。承担NTP服务器的host131通过server和fudge指令进行设定。内容的修改仅限于如下三点:

  • restrict的IP修改
  • 外网的NTP源注释掉
  • 加入支持本地时钟的server与fudge
[root@host131 ~]# cp /etc/ntp.conf /etc/ntp.conf.org
[root@host131 ~]# vi /etc/ntp.conf
[root@host131 ~]# diff /etc/ntp.conf /etc/ntp.conf.org
17c17
< restrict 192.168.163.0 mask 255.255.255.0 nomodify notrap
---
> #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
21,27c21,24
< #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 127.127.1.0
< fudge  127.127.1.0 stratum 5
---
> 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
[root@host131 ~]#

启动ntpd服务

[root@host131 ~]# systemctl start ntpd
[root@host131 ~]#

结果确认

[root@host131 ~]# systemctl status ntpd
● ntpd.service - Network Time ServiceLoaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)Active: active (running) since Thu 2019-02-14 22:59:15 EST; 10s agoProcess: 11775 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 11776 (ntpd)CGroup: /system.slice/ntpd.service└─11776 /usr/sbin/ntpd -u ntp:ntp -gFeb 14 22:59:15 host131 ntpd[11776]: Listen and drop on 1 v6wildcard :: UDP 123
Feb 14 22:59:15 host131 ntpd[11776]: Listen normally on 2 lo 127.0.0.1 UDP 123
Feb 14 22:59:15 host131 ntpd[11776]: Listen normally on 3 enp0s3 192.168.163.131 UDP 123
Feb 14 22:59:15 host131 ntpd[11776]: Listen normally on 4 lo ::1 UDP 123
Feb 14 22:59:15 host131 ntpd[11776]: Listen normally on 5 enp0s3 fe80::853e:84d1:5e1:6303 UDP 123
Feb 14 22:59:15 host131 ntpd[11776]: Listening on routing socket on fd #22 for interface updates
Feb 14 22:59:15 host131 ntpd[11776]: 0.0.0.0 c016 06 restart
Feb 14 22:59:15 host131 ntpd[11776]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Feb 14 22:59:15 host131 ntpd[11776]: 0.0.0.0 c011 01 freq_not_set
Feb 14 22:59:16 host131 ntpd[11776]: 0.0.0.0 c514 04 freq_mode
[root@host131 ~]#

添加到自启动列表

[root@host131 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@host131 ~]#

结果确认

使用ntpq进行结果确认,可以看到确实是本地同步(LOCAL),同时stratum也被设定为5。 因为是本地,所以offset也都是0。

[root@host131 ~]# ntpq -premote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l   41   64    7    0.000    0.000   0.000
[root@host131 ~]#

设定示例(客户端)

客户端的时间同步一般来说有两种方式:
*方式1: 结合crontab的定时任务,使用ntpdate连接NTP服务器进行时间同步
*方式2: 设定ntpd,将server设定为内网的NTP服务器,可自动同步

此处采用方式2进行设定。

事前准备

安装命令:yum install -y ntp
注:包含了ntp和ntpdate,在本文的示例中客户端和服务器端均采用同样的安装包

[root@host131 ~]# rpm -qa |grep ntp
ntp-4.2.6p5-28.el7.centos.x86_64
ntpdate-4.2.6p5-28.el7.centos.x86_64
[root@host131 ~]# ntpd --version
ntpd 4.2.6p5exit 0
[root@host131 ~]#

设定文件修改

修改内容如下:

[root@host132 ~]# cp /etc/ntp.conf /etc/ntp.conf.org
[root@host132 ~]# vi /etc/ntp.conf
[root@host132 ~]# diff /etc/ntp.conf /etc/ntp.conf.org
21,25c21,24
< #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 192.168.163.131
---
> 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
[root@host132 ~]#

启动并确认ntpd服务

[root@host132 ~]# systemctl start ntpd
[root@host132 ~]# systemctl status ntpd
● ntpd.service - Network Time ServiceLoaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)Active: active (running) since Thu 2019-02-14 23:15:13 EST; 5s agoProcess: 11592 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 11593 (ntpd)CGroup: /system.slice/ntpd.service└─11593 /usr/sbin/ntpd -u ntp:ntp -gFeb 14 23:15:13 host132 ntpd[11593]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123
Feb 14 23:15:13 host132 ntpd[11593]: Listen and drop on 1 v6wildcard :: UDP 123
Feb 14 23:15:13 host132 ntpd[11593]: Listen normally on 2 lo 127.0.0.1 UDP 123
Feb 14 23:15:13 host132 ntpd[11593]: Listen normally on 3 enp0s3 192.168.163.132 UDP 123
Feb 14 23:15:13 host132 ntpd[11593]: Listen normally on 4 lo ::1 UDP 123
Feb 14 23:15:13 host132 ntpd[11593]: Listen normally on 5 enp0s3 fe80::310b:d919:625d:a6f4 UDP 123
Feb 14 23:15:13 host132 ntpd[11593]: Listening on routing socket on fd #22 for interface updates
Feb 14 23:15:13 host132 ntpd[11593]: 0.0.0.0 c016 06 restart
Feb 14 23:15:13 host132 ntpd[11593]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Feb 14 23:15:13 host132 ntpd[11593]: 0.0.0.0 c011 01 freq_not_set
[root@host132 ~]#

确认同步信息

使用ntpq命令,可以看到连接的NTP服务器为192.168.163.131

[root@host132 ~]# ntpq -premote           refid      st t when poll reach   delay   offset  jitter
==============================================================================host131         .INIT.          16 u    -   64    0    0.000    0.000   0.000
[root@host132 ~]#

但是继续确认,发现其实时间并未同步, 原因在于本机时间比host131更新。按照ntp的机制,这种情况是不会同步的。

[root@host132 ~]# date; ssh host131 date
Thu Feb 14 23:20:04 EST 2019
Thu Feb 14 23:19:36 EST 2019
[root@host132 ~]#

手动使用ntpdate进行时间同步,收到如下错误提示:

[root@host132 ~]# ntpdate host131
14 Feb 23:21:39 ntpdate[11723]: the NTP socket is in use, exiting
[root@host132 ~]#

这也是相较于chrony被诟病的原因之一,这里先停下服务,然后再进行手动时间同步。

[root@host132 ~]# systemctl stop ntpd
[root@host132 ~]# ntpdate host131
14 Feb 23:26:05 ntpdate[11804]: step time server 192.168.163.131 offset -28.203650 sec
[root@host132 ~]#

再次重启,然后使用ntpd进行确认

[root@host132 ~]# ntpq -premote           refid      st t when poll reach   delay   offset  jitter
==============================================================================host131         LOCAL(0)         6 u   13   64    1    0.384   -0.006   0.000
[root@host132 ~]#

注意:offset是显示时间同步效果的重要标尺,单位为毫秒,需要多多观测。

再次确认两台机器的时间信息,发现同步的比较不错

[root@host132 ~]# date; ssh host131 date
Thu Feb 14 23:36:37 EST 2019
Thu Feb 14 23:36:37 EST 2019
[root@host132 ~]#

常见问题

现象: no server suitable for synchronization found
可能原因:检查NTP服务器和NTP客户端两侧的firewalld是否正确设定,只是验证原因的情况下可以先systemctl stop firewalld将其关闭。

参考文章

http://doc.ntp.org/

网络协议:NTP:2:使用NTPD搭建NTP服务器相关推荐

  1. linux进行校时ntp,Linux下快速搭建ntp时间同步服务器

    Linux下快速搭建ntp时间同步服务器 背景: 服务器多了,时间是否一致以及是否准备就显得格外重要,虽然这个问题总是被忽略,但是统一时间是很有必要的,因为时间问题导致服务出现问题也是司空见惯,本文简 ...

  2. 在linux挂载网络驱动器,利用centos 6.6 搭建iscsi服务器并在centos挂载iscsi网络驱动器...

    ################################################################## 搭建iscsi服务器: 参考自:http://www.linuxi ...

  3. Qt网络编程——使用OpenCV与TCP搭建图像处理服务器

    前言 前面的博客有写过如果使用TCP搭建一个客户端与服务器,连接并互发信息,这里主是演示,如何把客户端的图像发往服务器,服务器得到图像后,按指令做不同的处理,并返回给客户端处理之后的结果,客户端只负责 ...

  4. 华为“扫地僧”纯手打《趣谈—网络协议.pdf》,看完只剩一个字:香

    不知道你有没有发现一个问题: 这几年技术更新很快,这几年OpenStack.Docker.Mesos ,Kubernetes.微服务.Serverless.AIOps等技术一个比一个赶,往往都是还没了 ...

  5. Internet主要的网络协议

    TCP/IP,即网络通信协议,这个协议是Internet最基本的协议,也是Internet国际互联网络的基础.TCP/IP由网络层的IP协议以及传输层的TCP协议组成.它分为4个层次:网络访问层,网络 ...

  6. 虚拟机2012搭建DNS服务器,Windows Server2012 安装配置DNS服务器方法详解

    Windows Server2012 安装配置DNS服务器方法详解 在云服务器 Windows Server2012 上安装配置DNS服务器方法,安装与配置非常简单,在这里写个完整教程方便大家查询 一 ...

  7. 内网服务器时间修改,内网(无网络)搭建ntp时间同步服务

    有三台服务器作为集群,地址分别为:192.168.1.33(以下简称33),192.168.1.35(以下35),192.168.1.37(以下37).都没有网络, 这里以33作为ntp服务器,其他两 ...

  8. Linux服务器搭建--NTP服务器的搭建与配置

    起因 公司内网里有多台服务器,由于是局域网下,所以时间无法与Internet进行时间同步,故想通过搭建NTP服务器,NTP服务器与Internet进行时间同步,而局域网中的其他服务器与该NTP服务器进 ...

  9. linux——搭建NTP服务器

    NTP是网络时间协议(Network Time Protocol)的缩写,是一种用于同步计算机时钟的协议.它可以通过互联网或局域网等网络传输时间信息,使得多台计算机的时钟保持同步.NTP是一种分布式的 ...

最新文章

  1. openstack neutron-fwaas 防火墙之iptables实现细节详解
  2. python怎么编辑文件夹_python创建修改文件
  3. 24、Cocos2dx 3.0游戏开发找小三之网格动作:高炫酷的3D动作
  4. Smart Form中四种窗口类型的区别
  5. antdesignvue upload vue3个人笔记待更新
  6. leetcode - 740. 删除与获得点数
  7. JSON.parse()、eval()、JSON.stringify()、jQuery.parseJSON()的用法
  8. 【java】Java实现单向链表反转
  9. Antd Mobile Design输入框组件InputItem错误显示方式封装
  10. CSDN创始人蒋涛出席2021 中关村论坛,启动“科创中国”开源创新榜单评选
  11. Python破解验证码,只要15分钟就够了!
  12. 学到一招!三行 Python 代码轻松提取 PDF 表格数据!
  13. Android中tcp和udp的区别,tcp和udp使用总结
  14. 智能陈桥五笔输入法 for linux,在Linux下安装陈桥五笔输入法
  15. 2022-07-04-5万字长文说清楚到底什么是“车规级”
  16. 微分方程建模(人口预测,捕食者猎物)
  17. 诡异!意识何以意识到意识自身?道翰天琼认知智能机器人API接口平台为您揭秘。
  18. word 怎么删除多余的空白页
  19. CentOS 6.2下安装基于Suricata + Barnyard 2 + Base的入侵检测系统
  20. ssm框架基于javaEE的企业办公OA管理系统

热门文章

  1. linux基础-vmware与Centos安装
  2. Python利用cv2创建10*10象棋棋盘图片
  3. 动态规划---例题1.矩阵连乘问题
  4. 数据挖掘工程师 VS. 算法工程师
  5. 收到一套传家宝...
  6. 顶象业务安全大讲堂全新升级,挖洞大神喊你来get 挖洞秘籍啦!
  7. 到底什么是代码重构?
  8. 备份工具xtrabackup
  9. 七雄争霸mysql修改_七雄争霸单机版
  10. 名帖186 米芾 行书《蜀素帖》