ntp和chronyd

ntp和chronyd

立即同步时间的坏处,影响某些计划任务执行

一、立即同步时间

方法一:

ntpdate ntp.aliyun.com

方法二:重启ntp服务

方法三:重启chrony服务

二、查看ntp和chrony状态

[root@textbox ~]# 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               
===============================================================================
^* localhost                     3   6    17     2    +62us[ +661us] +/-   88ms
[root@textbox ~]# 
[root@textbox ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*localhost       119.28.206.193   3 u   41   64  177    0.784    1.195 3656236

三、ntp做服务端的设置

#注释原来的ntp服务器,原来的ntp服务器都是centos.org美国的
[root@textbox ~]# sed   '/^server/s/^/#/' /etc/ntp.conf -i
#添加两个aliyun的
[root@textbox ~]# sed '1a server ntp.aliyun.com iburst'  /etc/ntp.conf -i
[root@textbox ~]# sed '1a server ntp1.aliyun.com iburst'  /etc/ntp.conf -i
#重启服务
[root@textbox ~]# systemctl restart ntpd
#ntp就这么简单,就可以当服务器使用了
[root@textbox ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+120.25.115.20   10.137.53.7      2 u    4   64   17   42.560   -8.795   6.944
*203.107.6.88    100.107.25.114   2 u    4   64   17   15.542   -7.021   6.460
#123端口,udp
[root@textbox ~]# ss -lutnp | grep ntp
udp    UNCONN     0      0      192.168.38.140:123                   *:*                   users:(("ntpd",pid=61668,fd=19))
udp    UNCONN     0      0      127.0.0.1:123                   *:*                   users:(("ntpd",pid=61668,fd=18))
udp    UNCONN     0      0         *:123                   *:*                   users:(("ntpd",pid=61668,fd=16))
udp    UNCONN     0      0      fe80::69d0:5288:fda9:93ae%ens33:123                  :::*                   users:(("ntpd",pid=61668,fd=21))
udp    UNCONN     0      0       ::1:123                  :::*                   users:(("ntpd",pid=61668,fd=20))
udp    UNCONN     0      0        :::123                  :::*                   users:(("ntpd",pid=61668,fd=17))
#用另一台服务器测试
[root@linux5 ~]# ntpdate 192.168.38.140
 5 Sep 09:17:25 ntpdate[28423]: adjust time server 192.168.38.140 offset -0.007790 sec

四、chrony服务端配置

设置项比ntp多

[root@imooc-nginx ~]# sed '/^server/s/^/#/' /etc/chrony.conf -i
[root@imooc-nginx ~]# sed '1a server ntp.aliyun.com iburst' /etc/chrony.conf -i
[root@imooc-nginx ~]# sed '1a server 0.cn.pool.ntp.org iburst' /etc/chrony.conf -i
[root@imooc-nginx ~]# sed '1a server ntp1.aliyun.com iburst' /etc/chrony.conf -i
#即使当前时服务器未与指定ntp完成时间同步,照样提供服务
[root@imooc-nginx ~]# sed '1a local stratum 10' /etc/chrony.conf -i
#允许哪些服务器同步
[root@imooc-nginx ~]# sed '1a allow 192.168.0.0/16' /etc/chrony.conf -i
[root@imooc-nginx ~]# systemctl restart chronyd
[root@imooc-nginx ~]# systemctl enable chronyd
#查看chronyd状态
[root@imooc-nginx ~]# chronyc sources -v
210 Number of sources = 3

  .-- 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               
===============================================================================
^+ 120.25.115.20                 2   6    17     4   +766us[ +591us] +/-   21ms
^- 111.230.189.174               2   6    17     4    +98us[  +98us] +/-   49ms
^* 203.107.6.88                  2   6    17     4   -187us[ -361us] +/-   16ms
#测试
[root@textbox ~]# ntpdate 192.168.38.154
 5 Sep 09:33:25 ntpdate[61810]: adjust time server 192.168.38.154 offset -0.010546 sec

五、自动同步时间两种方式

1、ntpdate ntp.aliyun.com写入计划任务,每小时同步一次

2、所有服务器chrony都开启,其中一台做时间服务器,其他服务器的server都指向它

六、设置时区

[root@textbox ~]# timedatectl set-timezone Asia/Shanghai
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chrony和NTP都是用于同步系统时钟的协议和工具。它们的作用都是确保计算机上的时钟与网络上的时间保持一致,但它们有一些区别。 引用中提到,Chrony可以比NTP更快、更准确地同步系统时钟,并且在修正时间的同时还可以修正CPU Tick。在Linux系统中,从CentOS 7.2和RHEL 7.0开始,Chrony被用作默认的时间同步服务。它的服务文件为"/usr/lib/systemd/system/chronyd.service",监听端口为323/udp和123/udp。 而引用中提到,NTP网络时间协议,通过UDP 123端口进行网络时钟同步。在RHEL 7中,也支持使用NTP作为时间服务器,但需要额外安装。NTP和Chrony不能同时存在,只能选择其中一个并将另一个屏蔽掉。 需要注意的是,引用中指出,使用ntpdate同步时间会造成时间的跳跃,对一些依赖时间的程序和服务会产生影响,例如sleep、timer等。而使用Chrony服务可以在修正时间的过程中同时修正CPU Tick。因此,在开机时使用ntpdate强制同步时间,在其他时候使用Chrony或NTP服务来同步时间是理想的做法。 综上所述,Chrony和NTP都是用于同步系统时钟的工具,但Chrony在速度和准确性方面可能更优于NTP,并且在修正时间时还可以同时修正CPU Tick。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Linux 时间一致性环境NTP/Chrony服务器部署配置](https://blog.csdn.net/ximenjianxue/article/details/109224984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Chrony 时间与NTP同步详解](https://blog.csdn.net/weixin_43332972/article/details/131440045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值