linux顯示當前時間命令 Linux顯示時間



文章插圖
linux顯示當前時間命令 Linux顯示時間

文章插圖
Linux 部署時間同步服務Chrony(Centos7)
一、概述
常見的時間同步工具有:chrony、ntp、openntpd;RHEL7版本以后默認支持chrony;
三種工具的詳細區別參考https://chrony.tuxfamily.org/comparison.html
chrony包含兩個程序:chronyd和chronyc,其中chronyd是守護進程,用于調整內核運行時間和時鐘服務器同步;用于監視 chronyd 的性能并在運行時更改各種操作參數;
二、安裝Chrony服務
[[email protected] ~]# yum install chrony -y[[email protected] ~]# systemctl enable chronyd.service[[email protected] ~]# systemctl restart chronyd.service[[email protected] ~]# systemctl status chronyd.service在防火墻開啟的情況下,需要開發ntp服務通過(ntp使用UDP端口123)
[[email protected] ~]# firewall-cmd --add-service=ntp --permanent[[email protected] ~]# firewall-cmd --reload三、配置Chrony服務
chrony配置文件:/etc/chrony.conf
#時間同步公共服務器地址 。以server開頭,理論上可以添加多個時間服務器地址# These servers were defined in the installation:serverntp.ntsc.ac.cniburstservercn.ntp.org.cniburstservertime.pool.aliyun.comiburstservertime1.aliyun.com iburstservertime2.aliyun.com iburstservertime1.cloud.tencent.com iburstservertime2.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狀態
[[email protected] ~]# timedatectlLocal time: 四 2021-05-06 16:16:09 CSTUniversal time: 四 2021-05-06 08:16:09 UTCRTC time: 四 2021-05-06 08:16:09Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yesNTP synchronized: yesRTC in local TZ: noDST active: n/a2.查看時區列表
[[email protected] ~]# timedatectl list-timezones |grep-E "Asia/S.*"Asia/SakhalinAsia/SamarkandAsia/SeoulAsia/ShanghaiAsia/SingaporeAsia/Srednekolymsk3.修改時區為亞洲/上海時區
[[email protected] ~]# timedatectl set-timezone Asia/Shanghai4.設置硬件時間(默認為UTC:0/1)
[[email protected] ~]# timedatectl set-local-rtc 05.啟用NTP時間同步
[[email protected] ~]# timedatectl set-ntp true6.校準時間服務器
[[email protected] ~]# chronyc trackingReference ID: 8BC7D7FB (139.199.215.251)Stratum: 3Ref time (UTC): Thu May 06 08:25:54 2021System time: 0.000144202 seconds fast of NTP timeLast offset: +0.000068143 secondsRMS offset: 0.001500069 secondsFrequency: 5.699 ppm slowResidual freq: +0.007 ppmSkew: 1.656 ppmRoot delay: 0.015710194 secondsRoot dispersion : 0.006367063 secondsUpdate interval : 5.0 secondsLeap status: Normal【linux顯示當前時間命令 Linux顯示時間】7.查看時間同步狀態
[[email protected] ~]# timedatectl statusLocal time: 四 2021-05-06 16:28:41 CSTUniversal time: 四 2021-05-06 08:28:41 UTCRTC time: 四 2021-05-06 08:28:40Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yesNTP synchronized: yesRTC in local TZ: noDST active: n/a8.常用命令
查看時間同步源狀態[[email protected] ~]# chronyc sourcestats -v[[email protected] ~]# chronyc sources -v查看ntp server 是否在線[[email protected] ~]# chronyc activity -v查看ntp詳細信息[[email protected] ~]# chronyc tracking -v調整配置/etc/chrony.conf,需要重啟服務生效