设为首页 收藏本站
查看: 813|回复: 0

[经验分享] 服务器/客户端方式NTP服务器配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-9 10:58:35 | 显示全部楼层 |阅读模式
1、NTP服务介绍
Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP可以利用多个途径和来源的时间服务器来更加精确的校正时间。
现在绝大部分的操作系统都支持NTP,比如在Unix和Windows中都有他们自己的NTP服务。比如在Windows中的SNTP。由于各个系统厂商会根据自己的系统情况对NTP来进行优化和设置,更加适合他自己的系统,对于其他系统的兼容性就会差很多。还有不同时期的系统会使用不同版本的NTP服务。最新版本位V4,由于每个版本都要他自己的一些特征。所以在设置NTP服务器时会有一些兼容性问题。因此,在多类型操作系统的环境中,尽量使用NTP官方组织提供的软件进行设置而不使用其他厂商在系统中默认提供的NTP服务器。NTP官方组织为 www.ntp.org

2、NTP服务器的运行方式

NTP服务器的使用方式包括,固定连接和动态连接方式。在固定连接中,客户端仅仅接收指定的服务器端的连接,无论服务端是否正常连接他都不会去换另外的服务器端接收时间。一般客户/服务器方式为这种模式。另外一直为动态连接,客户端在不停接收服务器端的广播信息,当一台服务器停止服务,只要有服务器广播出来的正确可靠的时间信息,客户端就可以正常的更新时间。一般为广播方式连接。

1.服务器/客户端方式
客户服务器方式为在互联网上使用最为广泛的一种方法,在设定服务器后,他可以灵活的配置客户端,让客户端选择是否使用NTP服务器或使用那台NTP服务器。客户服务器方式使用常用的RPC(remote-procedure-call)方式来传输时间信息。他们运行的过程为,客户端发出时间请求给服务器端,然后根据服务器端的答复来调节时间。
一般在客户端可以定义他们同步时间的间隔,和他们请求最大和最小的时间间隔。客户端会根据自己时钟时间和NTP服务器时间进行比对,如果客户端本机时钟比较准确,他们查询的时间间隔就会变长,如果本机时间出现误差较大,客户端会在比较短的时间内做出时间请求,来保障客户端时间的精确度。

2.广播/多播方式
多播和广播方式利用在以太网上的广播和多播协议来进行时间同步,时间服务器会定时发出广播和多播信息给指定的网段。使用广播和多播需要网络设备支持该协议,同时客户端也支持该协议。可以使用multicasting和manycasting在不同网段进行广播信息,来达到在包含多个网段的复杂网络中使用广播和多播来提供NTP服务。

主动/被动方式
主动/被动方式最多使用在集群系统中,由于集群系统对同一集群内的所有服务器的时间同步要求比较严格,他们相互配置为NTP服务器端和客户端,在不停的提供时钟同步服务,当其中的一台服务器出现停顿时不会影响到其他服务器的时间同步。可以说时互备的方式来进行时间同步服务。

3、NTP服务器安装
安装环境:
1
2
3
4
[iyunv@ntp-server ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[iyunv@ntp-server ~]# uname -r
2.6.32-642.el6.x86_64



NTP服务在安装系统的时候会被默认安装上
[iyunv@ntp-server ~]# rpm -qa ntp
ntp-4.2.6p5-10.el6.centos.x86_64
如果没有安装
1.配置yum源
1)备份
1
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup



2)下载新的CentOS-Base.repo 到/etc/yum.repos.d/
CentOS 5
1
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo



CentOS 6
1
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo



CentOS 7
1
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo



3)之后运行yum makecache生成缓存
2.安装NTP服务
yum install -y ntp

4、配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[iyunv@ntp-server ~]# 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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::1
# Hosts on local network are less restricted.
#restrict 10.0.0.0 mask 255.255.255.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 pool.ntp.org              配置时间服务器
server time.nist.gov             配置时间服务器
#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




5、启动NTP服务器
如果有定时时间同步先注释掉,以免发生冲突
1
2
[iyunv@ntp-server ~]# cat /var/spool/cron/root   
#*/5 * * * * /usr/sbin/ntpdate pool.ntp.org &>/dev/null



启动NTP服务
1
2
[iyunv@ntp-server ~]# /etc/init.d/ntpd start
Starting ntpd:                                           [  OK  ]




ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系
1
2
3
4
5
[iyunv@ntp-server ~]#  ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*news.neu.edu.cn 202.118.1.47     2 u   48   64    1   89.492   25.310   0.000
nist1-lnk.binar .INIT.          16 u    -   64    0    0.000    0.000   0.000



位置
标志
含义
remote之前
*
响应的NTP服务器和最精确的服务器
+
响应这个查询请求的NTP服务器
blank(空格)
没有响应的NTP服务器
列表上方
remote
响应这个请求的NTP服务器的名称
refid
NTP服务器使用的更高一级服务器的名称
st
正在响应请求的NTP服务器的级别
when
上一次成功请求之后到现在的秒数
poll
本地和远程服务器多少时间进行一次同步,单位秒,在一开始运行NTP的时候这个poll值会比较小,服务器同步的频率大,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach
用来测试能否和服务器连接,是一个八进制值,每成功连接一次它的值就会增加
delay
从本地机发送同步要求到ntp服务器的往返时间
offset
主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset越接近于0,主机和ntp服务器的时间越接近
jitter
统计了在特定个连续的连接数里offset的分布情况。简单地说这个数值的绝对值越小,主机的时间就越精确
ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。
1
2
3
4
5
6
[iyunv@ntp-server ~]# ntpstat
synchronised to NTP server (216.229.0.179) at stratum 2
   time correct to within 4121 ms
   polling server every 64 s
[iyunv@ntp-server ~]# ntpdate 10.0.0.129
6 Jun 00:01:39 ntpdate[1496]: the NTP socket is in use, exiting



6、客户机时间同步
客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。
1
2
[iyunv@ntp-server ~]# ntpdate 10.0.0.129
8 Sep 15:43:16 ntpdate[2962]: adjust time server 10.0.0.129 offset 0.117714 sec



然后将ntpdate 10.0.0.129写入定时任务中


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-269888-1-1.html 上篇帖子: SSH上传提示:encountered 1 errors during the transfer错误解决办法 下篇帖子: Grafana+Graphite搭建配置步骤 服务器 客户端
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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