我们找一台服务器让它的时间和互联网同步,其他的主机和该服务器同步,带宽可以充分利用,效率也更高。
因为ntp服务器既是客户端又是服务器,默认情况下,ntp是没有启用的,我们先要搞清楚是要当客户端or服务器,
例如,目前我们的时间是错误的,要和远程的服务器同步
我们拿172.20.0.1来做远程主机,它是服务器,

1、[root@Centos6 ~]# yum install ntp

Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-12.el6.centos.2 will be updated
---> Package ntp.x86_64 0:4.2.6p5-15.el6.centos will be an update
--> Processing Dependency: ntpdate = 4.2.6p5-15.el6.centos for package: ntp-4.2.6p5-15.el6.centos.x86_64
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-12.el6.centos.2 will be updated
---> Package ntpdate.x86_64 0:4.2.6p5-15.el6.centos will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================
Package Arch Version Repository Size

Updating:
ntp x86_64 4.2.6p5-15.el6.centos updates 600 k
Updating for dependencies:
ntpdate x86_64 4.2.6p5-15.el6.centos updates 79 k

Transaction Summary

Upgrade 2 Package(s)

Total download size: 679 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): ntp-4.2.6p5-15.el6.centos.x86_64.rpm | 600 kB 00:00
(2/2): ntpdate-4.2.6p5-15.el6.centos.x86_64.r | 79 kB 00:00

Total 2.3 MB/s | 679 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : ntpdate-4.2.6p5-15.el6.centos.x86_64 1/4
Updating : ntp-4.2.6p5-15.el6.centos.x86_64 2/4
Cleanup : ntp-4.2.6p5-12.el6.centos.2.x86_64 3/4
Cleanup : ntpdate-4.2.6p5-12.el6.centos.2.x86_64 4/4
Verifying : ntpdate-4.2.6p5-15.el6.centos.x86_64 1/4
Verifying : ntp-4.2.6p5-15.el6.centos.x86_64 2/4
Verifying : ntpdate-4.2.6p5-12.el6.centos.2.x86_64 3/4
Verifying : ntp-4.2.6p5-12.el6.centos.2.x86_64 4/4

Updated:
ntp.x86_64 0:4.2.6p5-15.el6.centos

Dependency Updated:
ntpdate.x86_64 0:4.2.6p5-15.el6.centos

Complete!

2、[root@centos7 ~]# rpm -ql ntp

/etc/dhcp/dhclient.d
/etc/dhcp/dhclient.d/ntp.sh
/etc/ntp.conf
/etc/ntp/crypto
/etc/ntp/crypto/pw
/etc/sysconfig/ntpd
/usr/bin/ntpstat
/usr/lib/systemd/ntp-units.d/60-ntpd.list
/usr/lib/systemd/system/ntpd.service
/usr/sbin/ntp-keygen
/usr/sbin/ntpd
/usr/sbin/ntpdc
/usr/sbin/ntpq
/usr/sbin/ntptime
/usr/sbin/tickadj
/usr/share/doc/ntp-4.2.6p5
/usr/share/doc/ntp-4.2.6p5/COPYRIGHT
/usr/share/doc/ntp-4.2.6p5/ChangeLog
/usr/share/doc/ntp-4.2.6p5/NEWS
/usr/share/man/man5/ntp.conf.5.gz
/usr/share/man/man5/ntp_acc.5.gz
/usr/share/man/man5/ntp_auth.5.gz
/usr/share/man/man5/ntp_clock.5.gz
/usr/share/man/man5/ntp_decode.5.gz
/usr/share/man/man5/ntp_misc.5.gz
/usr/share/man/man5/ntp_mon.5.gz
/usr/share/man/man8/ntp-keygen.8.gz
/usr/share/man/man8/ntpd.8.gz
/usr/share/man/man8/ntpdc.8.gz
/usr/share/man/man8/ntpq.8.gz
/usr/share/man/man8/ntpstat.8.gz
/usr/share/man/man8/ntptime.8.gz
/usr/share/man/man8/tickadj.8.gz
/var/lib/ntp
/var/lib/ntp/drift
/var/log/ntpstats

3、[root@Centos6 ~]# 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 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 192.168.1.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 172.20.0.1 iburst:
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
"/etc/ntp.conf" 54L, 1804C      

我们添加上server 172.20.0.1 iburst(该词为加速同步的关键词)

4、启动脚本

[root@Centos6 ~]# service ntpd status
ntpd is stopped
[root@Centos6 ~]# service ntpd start
Starting ntpd: [ OK ]
[root@Centos6 ~]# date
Mon Jan 29 17:24:06 CST 2018
此时一旦启动脚本,瞬间同步时间

5、我们故意把时间弄错,再启动脚本

[root@Centos6 ~]# date -s "-1 year"
Mon Jan 29 17:23:25 CST 2018
[root@Centos6 ~]# date
Mon Jan 29 17:23:28 CST 2018
[root@Centos6 ~]# service ntpd restart
Shutting down ntpd: [ OK ]
Starting ntpd: [ OK ]
[root@Centos6 ~]# date
Mon Jan 29 17:24:06 CST 2018
它是逐步同步的过程,所以不会马上同步,然而,我们用如下命令它与主机时间同步:

[root@Centos6 ~]# ntpq -premote           refid      st t when poll reach   delay   offset  jitter
==============================================================================172.20.0.1      5.103.139.163    2 u   37   64    1    1.979  -15.289   0.000
+ntp7.flashdance 194.58.202.20    2 u   33   64    1  490.878  -13.246  19.820
*85.199.214.101  .GPS.            1 u   30   64    1  309.518   39.975   1.333ntp.wdc1.us.lea .STEP.          16 u    -   64    0    0.000    0.000   0.000jp.linode.oxoox .STEP.          16 u    -   64    0    0.000    0.000   0.000
而我们应该用ntpdate命令实现与主机立即同步

[root@Centos6 ~]# ntpdate 172.20.0.1
29 Jan 17:25:33 ntpdate[29759]: the NTP socket is in use, exiting
它会报错,因为开启了service ntpd restart ntp服务,
应该先暂停服务,再立即同步,最后再开启服务。

[root@Centos6 ~]# service ntpd stop
Shutting down ntpd:                                        [  OK  ]
[root@Centos6 ~]# ntpdate 172.20.0.1
29 Jan 17:26:00 ntpdate[29779]: adjust time server 172.20.0.1 offset -0.015599 sec
[root@Centos6 ~]# service ntpd start
Starting ntpd:                                             [  OK  ]
[root@Centos6 ~]# date
Tue Jan 29 17:26:28 CST 2019

先确保时间一致,再走ntp协议!

ntp协议走的是udp协议的123端口,将来配置防火墙时,要把123端口打开.

使用chrony实现时间同步
首先,chrony也可以既当客户端又可作服务器

1、先配置chrony客户端

[root@centos7 ~]# vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
sercer 172.20.0.1
#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                                 # Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.

我们只需添加sercer 172.20.0.1把其他的server都注释掉即可完成配置。

2、打开chrony状态,此时为关闭 如下:

[root@centos7 ~]# systemctl status chronyd.service
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:chronyd(8)
man:chrony.conf(5)

打开chronyc 专门的客户端工具

[root@centos7 ~]# chronyc
chrony version 3.2
Copyright (C) 1997-2003, 2007, 2009-2017 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.

chronyc> help
System clock:
tracking Display system time information
makestep Correct clock by stepping immediately
makestep <threshold> <updates>
Configure automatic clock stepping
maxupdateskew <skew> Modify maximum valid skew to update frequency
waitsync [<max-tries> [<max-correction> [<max-skew> [<interval>]]]]
Wait until synchronised in specified limits
Time sources:
sources [-v] Display information about current sources
sourcestats [-v] Display statistics about collected measurements
reselect Force reselecting synchronisation source
reselectdist <dist> Modify reselection distance
使用chronyc sources -v
root@Centos6 ~]# chronyc sources -v
chronyc> sources -v
210 Number of sources = 1

.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample

^* gateway 2 6 177 23 +160us[ +271us] +/- 252ms
chronyc>

#####  vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.sserver ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
#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# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

我们添加了server ntp.aliyun.com iburst server ntp1.aliyun.com iburst 两项,与互联网同步的操作
把时间搞错,把服务重新启动:
[root@centos7 ~]# date -s "2 year"
Fri Jan 29 18:55:06 CST 2021
[root@centos7 ~]# systemctl restart chronyd
[root@centos7 ~]# date
Tue Jan 29 18:56:49 CST 2021
[root@centos7 ~]# chronyc sources -v
210 Number of sources = 2

.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample

^* 203.107.6.88 2 6 77 1 -1324us[-3791us] +/- 19ms
^+ 120.25.115.20 2 6 77 0 +1182us[+1182us] +/- 22ms
[root@centos7 ~]# date
Tue Jan 29 18:58:51 CST 2019
此时时间同步完成,相差两年也会马上同步,速度可见一斑。

转载于:https://blog.51cto.com/14128387/2347727

使用ntp协议同步时间,chronyc sources -v 同步时间相关推荐

  1. RHEL7 -- 使用Chrony设置时间与时钟服务器同步

    Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确. 它由两个程序组成:chronyd和chronyc. chronyd是一个后台运行的守护进程,用于调整内核 ...

  2. Linux实现NTP协议同步时间

    NTP简介 网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供 ...

  3. 网络NTP协议时间校对源码

    从科锐培训中断回来,好几个月都没写过博客,以后像在武汉这样学习的日子大概不会有了.最近三个月换了两份工作,也够折腾,期间有想过重回科锐的念头,种种原因最后放弃了. 写这个NTP校时的程序,起因是家里旧 ...

  4. 如何基于NTP协议获取阿里云实时时间

    以下文章来源于:公_众_号开源电子网 读取更多技术文章,请扫码关注 关注公众号,后台回复:资料 (免费领取项目文件) NTP协议简介: NTP服务器(Network Time Protocol(NTP ...

  5. 通过NTP协议进行网络授时时钟同步服务

    通过NTP协议进行网络授时时钟同步服务 通过NTP协议进行网络授时时钟同步服务 NTP(Network Time Protocol)是由美國德拉瓦大學的D.L. Mills教授於1985年提出,除了可 ...

  6. Linuxwindows时间服务器搭建定时同步设置详细讲解

    目录 一.Linux关闭防火墙 二.ntp时间同步配置 二.chrony配置时间同步 1.服务端配置(192.168.65.188) 2.客户端配置(192.168.65.166) 三.windows ...

  7. FFmpeg之视频封装格式、流媒体协议、视频编解码协议和传输流格式、时间戳和时间基、视频像素数据

    通用视频分析工具:Mediainfo.Elecard StreamEye(视频编码分析工具); 视频封装格式: ********************MP4****************** MP ...

  8. Dynamo涉及的算法和协议——p2p架构,一致性hash容错+gossip协议获取集群状态+向量时钟同步数据...

    转自:http://www.letiantian.me/2014-06-16-dynamo-algorithm-protocol/ Dynamo是Amazon的一个分布式的键值系统,P2P架构,没有主 ...

  9. ntp协议原理linux网络编程,NTP协议

    一:NTP的工作原理(UDP端口 123) NTP工作原理: 上图是NTP的基本工作原理,路由器A和路由器B通过网络连接,它们都有自己独立的系统时钟,要实现各自的系统时钟的自动同步,作如下的假设: - ...

  10. ntp协议中 服务器失效怎么办,排除网络时间协议(NTP)故障

    本文提供信息关于怎样排除故障与网络时间协议(NTP)的常见问题. 思科建议您有一好了解NTP如何工作和一好知识网络时间协议. 本文档不限于特定的软件和硬件版本. 有关文档规则的详细信息,请参阅 Cis ...

最新文章

  1. Sketchup Pro(草图大师) 2017中文版 64位
  2. Ubuntu 14.04 下 OF-Config安装
  3. ESP32cam蓝牙模块与arduino uno通信实验
  4. 中文通用百科知识图谱(CN-DBpedia)
  5. 为何类型转型Integer[] = Object[]可以,而Integer[] = int[]却不行
  6. linux img 内核启动,linux的启动流程(initrd.img)
  7. mysql自增主键查询及auto_increment修改。
  8. 买房就是创业,房东就是创业者
  9. 阿凡达时隔十年重映,王者归来还是炒冷饭?Python爬取上千条评论并分析
  10. 筛选法建立初始堆_MTT法检测细胞增殖
  11. (6.0系统手机)安卓神器Xposed框架ROOT安装指南
  12. import错误:undefined symbol: _ZN6caffe26detail37_typeMetaDataInstance_preallocated_32E
  13. 2022春招——芯动科技FPGA岗技术面(一面心得)
  14. Linux tmpfs是什么
  15. PPT里怎么加入边翻页幻灯片边连续播放音乐?
  16. 在表示计算机存储容量中1T,1T等于多少G,
  17. 使用Cobbler批量部署Linux和Windows
  18. Zotero添加影响因子插件IF
  19. Java学习之字节流和字符流的转化
  20. ReadFile功能

热门文章

  1. 视频画中画效果,拖动进度条可以seek到相应视频帧显示
  2. 从Android转大前端半年,我的一些思考
  3. 我发现混的好的,都是挺能“吹牛逼”的
  4. 姚聪 旷世_首款国产豪华品牌大型SUV,汉龙旷世品质气场100分! - 快讯
  5. python天气预报的功能介绍_40行Python代码实现天气预报和每日鸡汤推送功能
  6. gin框架-2-返回是html格式的网页数据
  7. gin -get请求的小示例1-Handle处理GET请求
  8. 实现了一个本地版本的在线json测试环境光-pythono
  9. android 禁用剪切板_如何阻止应用程序阅读Android剪贴板以保护您的隐私
  10. LibFewShot:小样本学习与细粒度分类(二) -- 数据集、训练、测试