linux兩條命令一起執行:linux顯示時間命令

Linux 部署時間同步服務Chrony(Centos7)

一、概述

常見的時間同步工具有:chrony、ntp、openntpd;RHEL7版本以後默認支持chrony;

三種工具的詳細區別參考
https://chrony.tuxfamily.org/comparison.html

chrony包含兩個程序:chronyd和chronyc,其中chronyd是守護進程,用於調整內核運行時間和時鐘伺服器同步;用於監視 chronyd 的性能並在運行時更改各種操作參數;

二、安裝Chrony服務

[root@kmi-dawufiep ~]# yum install chrony -y
[root@kmi-dawufiep ~]# systemctl enable chronyd.service
[root@kmi-dawufiep ~]# systemctl restart chronyd.service
[root@kmi-dawufiep ~]# systemctl status chronyd.service

在防火牆開啟的情況下,需要開發ntp服務通過(ntp使用UDP埠123)

[root@kmi-dawufiep ~]# firewall-cmd --add-service=ntp --permanent
[root@kmi-dawufiep ~]# firewall-cmd --reload

三、配置Chrony服務

chrony配置文件:/etc/chrony.conf

#時間同步公共伺服器地址。以server開頭,理論上可以添加多個時間伺服器地址
# These servers were defined in the installation:
server ntp.ntsc.ac.cn iburst
server cn.ntp.org.cn iburst
server time.pool.aliyun.com iburst
server time1.aliyun.com iburst
server time2.aliyun.com iburst
server time1.cloud.tencent.com iburst
server time2.cloud.tencent.com iburst
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 根據實際時間計算出伺服器增減時間的比率,然後記錄到一個文件中,在系統重啟後為系統做出最佳時間補償調整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# chronyd根據需求減慢或加速時間調整,
# 在某些情況下系統時鐘可能漂移過快,導致時間調整用時過長。
# 該指令強制chronyd調整時期,大於某個閥值時步進調整系統時鐘。
# 只有在因chronyd啟動時間超過指定的限制時(可使用負值來禁用限制)沒有更多時鐘更新時才生效。
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# 將啟用一個內核模式,在該模式中,系統時間每11分鐘會拷貝到實時時鐘(RTC)。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 通過使用hwtimestamp指令啟用硬體時間戳
# 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

# 指定一台主機、子網,或者網路以允許或拒絕NTP連接到扮演時鐘伺服器的機器
# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# 指定包含NTP驗證密鑰的文件。
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# 指定日誌文件的目錄。
# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

調整公共伺服器地址模塊,中國國內時間同步伺服器地址可參考:https://dns.icoa.cn/ntp/

四、配置時區

1.查看時間日期、時區、NTP狀態

[root@kmi-dawufiep ~]# timedatectl
      Local time: 四 2021-05-06 16:16:09 CST
  Universal time: 四 2021-05-06 08:16:09 UTC
        RTC time: 四 2021-05-06 08:16:09
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
      DST active: n/a

2.查看時區列表

[root@kmi-dawufiep ~]# timedatectl list-timezones |  grep  -E "Asia/S.*"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

3.修改時區為亞洲/上海時區

[root@kmi-dawufiep ~]# timedatectl set-timezone Asia/Shanghai

4.設置硬體時間(默認為UTC:0/1)

[root@kmi-dawufiep ~]# timedatectl set-local-rtc 0

5.啟用NTP時間同步

[root@kmi-dawufiep ~]# timedatectl set-ntp true

6.校準時間伺服器

[root@kmi-dawufiep ~]# chronyc tracking
Reference ID    : 8BC7D7FB (139.199.215.251)
Stratum         : 3
Ref time (UTC)  : Thu May 06 08:25:54 2021
System time     : 0.000144202 seconds fast of NTP time
Last offset     : +0.000068143 seconds
RMS offset      : 0.001500069 seconds
Frequency       : 5.699 ppm slow
Residual freq   : +0.007 ppm
Skew            : 1.656 ppm
Root delay      : 0.015710194 seconds
Root dispersion : 0.006367063 seconds
Update interval : 5.0 seconds
Leap status     : Normal

7.查看時間同步狀態

[root@kmi-dawufiep ~]# timedatectl status
      Local time: 四 2021-05-06 16:28:41 CST
  Universal time: 四 2021-05-06 08:28:41 UTC
        RTC time: 四 2021-05-06 08:28:40
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
      DST active: n/a

8.常用命令

查看時間同步源狀態
[root@kmi-dawufiep ~]# chronyc sourcestats -v
[root@kmi-dawufiep ~]# chronyc sources -v
查看ntp server 是否在線
[root@kmi-dawufiep ~]# chronyc activity -v
查看ntp詳細信息
[root@kmi-dawufiep ~]# chronyc tracking -v

調整配置/etc/chrony.conf,需要重啟服務生效

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/216977.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 00:25
下一篇 2024-12-09 00:25

相關推薦

發表回復

登錄後才能評論