使用ntpdate + crond

  1. ntpdate ntp1.aliyun.com
  2. echo " */3 * * * * /usr/sbin/ntpdate -u ntp1.aliyun.com > /dev/null 2>&1 " >> /var/spool/cron/root

参考链接:
https://www.escapelife.site/posts/9587ae84.html

CentOS7 已经默认安装有 Chrony 工具,其既可作时间服务器服务端,也可作客户端。而且性能比 ntp 要好很多、配置简单、管理方便。

  • [1] 安装启动 ```shell

    安装服务

    yum install -y chrony

启动服务

systemctl start chronyd.service

设置开机自启动,默认就是enable的

systemctl enable chronyd.service

  1. - [2] 防火墙配置
  2. ```shell
  3. # 因NTP使用123/UDP端口协议,所以允许NTP服务即可
  4. firewall-cmd --add-service=ntp --permanent
  5. firewall-cmd --reload
  6. chronyc -a makestep
  • [3] 设置时区 ```shell

    查看日期时间、时区及NTP状态

    timedatectl

查看时区列表

timedatectl list-timezones

修改时区

timedatectl set-timezone Asia/Shanghai

修改日期时间

timedatectl set-time “2015-01-21 11:50:00”

设置完时区后,强制同步下系统时钟

chronyc -a makestep

  1. - [4] 图形化界面
  2. ```shell
  3. yum -y install system-config-date

主要配置

当 Chrony 启动时,它会读取 /etc/chrony.conf 配置文件中的设置,配置内容格式和 ntpd 服务基本相似。

  • 主要配置选项


image.png

  • 这里使用的是网络上提供的时间服务,如果本局域网内有对时服务开启的话,通过将上面的几条serer记录删除,增加指定局域网内的对时服务器并restart chrony服务即可。
  1. [root@localhost ~]# cat /etc/chrony.conf |grep -v ^#|grep -v ^$
  2. server 0.centos.pool.ntp.org iburst
  3. server 1.centos.pool.ntp.org iburst
  4. server 2.centos.pool.ntp.org iburst
  5. server 3.centos.pool.ntp.org iburst
  6. stratumweight 0
  7. driftfile /var/lib/chrony/drift
  8. rtcsync
  9. makestep 10 3
  10. bindcmdaddress 127.0.0.1
  11. bindcmdaddress ::1
  12. keyfile /etc/chrony.keys
  13. commandkey 1
  14. generatecommandkey
  15. noclientlog
  16. logchange 0.5
  17. logdir /var/log/chrony

查看状态

  • [1] 检查 ntp 源服务器状态
    1. [root@localhost ~]# chronyc sourcestats
    2. 210 Number of sources = 3
    3. Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
    4. ==============================================================================
    5. dns.sjtu.edu.cn 4 3 302 6.440 90.221 +13ms 694us
    6. dns1.synet.edu.cn 0 0 0 0.000 2000.000 +0ns 4000ms
    7. 202.118.1.130 7 5 323 -0.174 7.323 -8406ns 303us
  • [2] 检查 ntp 详细同步状态
  1. [root@localhost ~]# chronyc sources -v
  2. 210 Number of sources = 3
  3. .-- Source mode '^' = server, '=' = peer, '#' = local clock.
  4. / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
  5. | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
  6. || .- xxxx [ yyyy ] +/- zzzz
  7. || / xxxx = adjusted offset,
  8. || Log2(Polling interval) -. | yyyy = measured offset,
  9. || \ | zzzz = estimated error.
  10. || | |
  11. MS Name/IP address Stratum Poll Reach LastRx Last sample
  12. ===============================================================================
  13. ^- dns.sjtu.edu.cn 3 7 13 20 +11ms[ +11ms] +/- 98ms
  14. ^? dns1.synet.edu.cn 0 8 0 10y +0ns[ +0ns] +/- 0ns
  15. ^* 202.118.1.130 2 6 377 125 -122us[ -305us] +/- 31ms
  • [3] 设置硬件时间 ```shell

    硬件时间默认为UTC

    $ timedatectl set-local-rtc 1

启用或关闭NTP时间同步

$ timedatectl set-ntp yes|flase

校准时间服务器

$ chronyc tracking

  1. <a name="cr1om"></a>
  2. ## **工具使用**
  3. ```shell
  4. 可以通过运行 chronyc 命令来修改设置,以下是常用命令。
  5. accheck
  6. 检查 NTP 访问是否对特定主机可用
  7. activity
  8. 该命令会显示有多少 NTP 源在线/离线
  9. add server
  10. 手动添加一台新的 NTP 服务器
  11. clients
  12. 在客户端报告已访问到服务器
  13. delete
  14. 手动移除 NTP 服务器或对等服务器
  15. settime
  16. 手动设置守护进程时间
  17. tracking
  18. 显示系统时间信息
  19. help
  20. 可以查看更多 chronyc 的交互命令