Linux基础学习-chrony时间同步服务

Chrony时间同步

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.它的用于是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在网络网上绝大多数的地方其精度可以达到1-50ms.

1.chrony服务部署
//查看一下由哪个包提供的
[root@ntp-server ~]# yum provides chronyd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
chrony-3.1-2.el7.x86_64 : An NTP client/server
Repo        : dvd
Matched from:
Filename    : /usr/sbin/chronyd


//安装chrony服务
[root@ntp-server ~]# yum install chrony
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
dvd                                                                | 4.1 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package chrony.x86_64 0:3.1-2.el7 will be installed
--> Processing Dependency: libseccomp.so.2()(64bit) for package: chrony-3.1-2.el7.x86_64
--> Running transaction check
---> Package libseccomp.x86_64 0:2.3.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================
 Package                Arch               Version                  Repository       Size
==========================================================================================
Installing:
 chrony                 x86_64             3.1-2.el7                dvd             237 k
Installing for dependencies:
 libseccomp             x86_64             2.3.1-3.el7              dvd              56 k

Transaction Summary
==========================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 293 k
Installed size: 761 k
Is this ok [y/d/N]: y
Downloading packages:
------------------------------------------------------------------------------------------
Total                                                     1.6 MB/s | 293 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libseccomp-2.3.1-3.el7.x86_64                                          1/2 
  Installing : chrony-3.1-2.el7.x86_64                                                2/2 
  Verifying  : chrony-3.1-2.el7.x86_64                                                1/2 
  Verifying  : libseccomp-2.3.1-3.el7.x86_64                                          2/2 

Installed:
  chrony.x86_64 0:3.1-2.el7                                                               

Dependency Installed:
  libseccomp.x86_64 0:2.3.1-3.el7                                                         

Complete!

//启动服务
[root@ntp-server ~]# systemctl start chrony
chrony-dnssrv@       chronyd.service      chrony-wait.service  
[root@ntp-server ~]# systemctl start chronyd

chrony服务相关命令

参数解释
list-timezones列出可用时区信息
set-timezone设置时区
set-ntp启用NTP
status查看状态
[root@ntp-server ~]# timedatectl 
      Local time: Tue 2018-08-21 14:02:35 CST
  Universal time: Tue 2018-08-21 06:02:35 UTC
        RTC time: Tue 2018-08-21 06:02:33
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ntp-server ~]# timedatectl 
list-timezones  set-ntp         set-timezone    
set-local-rtc   set-time        status 
[root@ntp-server ~]# timedatectl list-timezones | grep -i shanghai
Asia/Shanghai

//关闭NTP
[root@ntp-server ~]# timedatectl set-ntp false 
[root@ntp-server ~]# timedatectl 
      Local time: Tue 2018-08-21 14:15:36 CST
  Universal time: Tue 2018-08-21 06:15:36 UTC
        RTC time: Tue 2018-08-21 06:15:36
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

//启用NTP
[root@ntp-server ~]# timedatectl set-ntp true 
[root@ntp-server ~]# timedatectl 
      Local time: Tue 2018-08-21 14:16:02 CST
  Universal time: Tue 2018-08-21 06:16:02 UTC
        RTC time: Tue 2018-08-21 06:16:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

//查看一下ntpdate由哪个包提供的
[root@ntp-server ~]# yum provides ntpdate
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
ntpdate-4.2.6p5-25.el7_3.2.x86_64 : Utility to set the date and time via NTP
Repo        : dvd

//如果没有请安装ntpdate
[root@ntp-server ~]# yum install ntpdate.x86_64 
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-25.el7_3.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================
 Package            Arch              Version                        Repository      Size
==========================================================================================
Installing:
 ntpdate            x86_64            4.2.6p5-25.el7_3.2             dvd             86 k

Transaction Summary
==========================================================================================
Install  1 Package

Total download size: 86 k
Installed size: 121 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ntpdate-4.2.6p5-25.el7_3.2.x86_64                                      1/1 
  Verifying  : ntpdate-4.2.6p5-25.el7_3.2.x86_64                                      1/1 

Installed:
  ntpdate.x86_64 0:4.2.6p5-25.el7_3.2                                                     

Complete!
2.配置时间同步服务器
[root@ntp-server ~]# vim /etc/chrony.conf
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst

allow 172.16.10.0/24

//验证服务端是否能同步时间
[root@ntp-server ~]# 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               
===============================================================================
^* 120.25.115.20                 2   6    17    22   -117us[ -948us] +/-   24ms

//看一下服务器时间
[root@ntp-server ~]# date
Tue Aug 21 15:31:29 CST 2018

//看一下客户端时间 时间不对需要同步
[root@ntp-client ~]# date
Fri Aug 20 14:10:28 CST 2010

//ntp服务器地址
ens35: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.10.20  netmask 255.255.255.0  broadcast 172.16.10.255
        inet6 fe80::da6c:394e:1ef7:c236  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:6e:66:65  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 480 (480.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21  bytes 1614 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

//客户端同步,时间已经同步正确
[root@ntp-client ~]# ntpdate 172.16.10.20
21 Aug 15:35:10 ntpdate[2421]: step time server 172.16.10.20 offset 252552056.617240 sec
You have new mail in /var/spool/mail/root
[root@ntp-client ~]# date
Tue Aug 21 15:35:15 CST 2018

//第一种方式设置定时任务
[root@ntp-client ~]# which ntpdate
/usr/sbin/ntpdate
[root@ntp-client ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 172.16.10.20 &>/dev/null


//第二种方式客户端编辑配置文件服务器指向172.168.10.20
[root@ntp-client ~]# vim /etc/chrony.conf 
server 172.16.10.20 iburst

//启动服务查看服务运行状态
[root@ntp-client ~]# systemctl restart chronyd
[root@ntp-client ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-08-21 15:52:11 CST; 6s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 3012 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 3009 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3011 (chronyd)
    Tasks: 1
   CGroup: /system.slice/chronyd.service
           └─3011 /usr/sbin/chronyd

Aug 21 15:52:10 ntp-client systemd[1]: Starting NTP client/server...
Aug 21 15:52:11 ntp-client chronyd[3011]: chronyd version 3.2 starting (+CMDMON +NTP ...G)
Aug 21 15:52:11 ntp-client chronyd[3011]: Frequency -37.840 +/- 1000000.000 ppm read ...ft
Aug 21 15:52:11 ntp-client systemd[1]: Started NTP client/server.
Aug 21 15:52:15 ntp-client chronyd[3011]: Selected source 172.16.10.20
Hint: Some lines were ellipsized, use -l to show in full.

//差不多一分钟同步了
[root@ntp-client ~]# date
Tue Aug 10 08:32:32 CST 2010
[root@ntp-client ~]# date
Tue Aug 10 08:32:33 CST 2010
[root@ntp-client ~]# date
Tue Aug 21 16:04:24 CST 2018
[root@ntp-client ~]# date
Tue Aug 21 16:04:25 CST 2018

转载于:https://www.cnblogs.com/qdlinux/p/9636573.html

### 回答1: Linux中的chrony时间同步命令是"chronyc"。它可以用来管理和监控chrony时间同步服务,包括手动同步时间、查看同步状态、修改配置等操作。常用的chronyc命令包括:chronyc sources、chronyc tracking、chronyc makestep等。 ### 回答2: Chrony是一款用于Linux操作系统时间同步软件。它使用以太网,无线电(wifi),PPPoE和IPv6等多种协议来同步系统和本地时钟,从而保证系统的时间准确性。下面我们将详细介绍使用Chrony进行时间同步的操作步骤。 首先,需要安装Chrony软件。不同发行版的Linux安装命令略有不同,这里以Debian/Ubuntu为例: ``` sudo apt-get install chrony ``` 安装完成后,打开Chrony配置文件/etc/chrony/chrony.conf。如果要同步到局域网内的时间服务器,可以将以下内容添加到配置文件中: ``` server time.server.com iburst ``` 其中"time.server.com"为你要同步的时间服务器的地址,"iburst"是用于快速同步的标志。 如果Chrony作为系统的唯一时间同步软件,在配置文件中将以下内容打开: ``` # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). #pool 2.debian.pool.ntp.org offline iburst ``` 在启动Chrony前,需要先关闭系统自带的时间同步服务。不同发行版的关闭命令略有不同,这里以Debian/Ubuntu为例: ``` sudo timedatectl set-ntp off ``` 最后,重新启动Chrony服务: ``` sudo systemctl restart chrony ``` 稍等片刻,系统的时间就会被同步到上面设置的时间服务器的时间了。如果需要检查Chrony是否正在运行和同步,可以运行以下命令: ``` sudo chronyc tracking ``` 需要注意的是,Chrony需要在/root/.chrony-keys文件中记录每个远程时间服务器的密钥,以保护同步信息的安全性。如果需要更多的Chrony配置选项,可以查看Chrony官方文档。 ### 回答3: Linux系统中,时间同步是非常关键的操作,如果系统时间不准确,会导致很多问题,比如日志记录不准确、排查问题困难等。为了保证系统时间的准确性,Linux系统提供了多种时间同步工具,其中 chrony 是一种非常常用的时间同步工具。 chrony 是一种实现 NTP 协议的时间同步软件,它与传统的 NTP(Network Time Protocol)相比,其优点是更加适合被部署于当今广泛存在的不同类型的网络中。它具有快速同步、稳定性和安全等特点。 使用 chrony 进行时间同步的步骤如下: 1. 安装 chrony 在大多数 Linux 发行版中,chrony 已经预装了,如果没有,则需要使用系统包管理器进行安装。 2. 配置 chrony chrony 的默认配置文件位于 /etc/chrony.conf。在该文件中,你可以修改多种设置,如要使用的 NTP 服务器等。修改完成后,保存并关闭该文件。 3. 启动 chrony 在大多数 Linux 发行版中,chrony 已经完成了自动启动,如果没有,则需要手动启动。可以使用以下命令完成启动: sudo systemctl start chronyd 4. 验证同步情况 可以使用以下命令验证 chrony 是否正在同步系统时间: chronyc tracking 该命令会显示系统时间与 NTP 服务器时间之间的偏差以及同步状态。 使用 chrony 进行时间同步,可以使系统时间更加准确,从而保证系统功能的正常运行。另外,在某些特殊场景下,比如高并发的服务环境下,时间同步更是关键。因此,我们必须保证在 Linux 系统中使用时间同步软件的正确性,并进行配置调整和监测跟踪,保证服务应用的数据一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值