早些时候Linux下使用NTP做时间同步。新版Cenots开始用chrony。
这里简单记录一下:
# 查看当前时间
$ date
2017年 08月 11日 星期五 03:28:44 UTC
# NOTE: 这里显示的时区为 UTC(通用协调时(UTC, Universal Time Coordinated)。UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同)应该改为:中国标准时区(CST, chinese standard time),类似如下:
$ date
2017年 08月 11日 星期五 11:28:44 CST
使用timedatectl
$ timedatectl
Local time: 五 2017-08-11 03:28:47 UTC
Universal time: 五 2017-08-11 03:28:47 UTC
RTC time: 五 2017-08-11 03:28:56
Time zone: UTC (UTC, +0000)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
查看时区:
$ timedatectl list-timezones
Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau
Asia/Aqtobe
Asia/Ashgabat
...
...
...
Pacific/Rarotonga
Pacific/Saipan
Pacific/Tahiti
Pacific/Tarawa
Pacific/Tongatapu
Pacific/Wake
Pacific/Wallis
$ timedatectl set-timezone Asia/Shanghai
$ timedatectl
Local time: 五 2017-08-11 11:41:03 CST
Universal time: 五 2017-08-11 03:41:03 UTC
RTC time: 五 2017-08-11 03:41:02
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
$ date
2017年 08月 11日 星期五 11:41:05 CST
chronyd
$ systemctl restart chronyd.service
# NOTE: 当Chrony启动时,它会读取/etc/chrony.conf配置文件中的设置
$ vi /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
# Ignore stratum in source selection.
stratumweight 0
...
查看时间同步源
$ chronyc sources -v
210 Number of sources = 4
.-- 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
|| / xxxx = adjusted offset,
|| Log2(Polling interval) -. | yyyy = measured offset,
|| \ | zzzz = estimated error.
|| | |
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ darwin.kenyonralph.com 2 6 77 23 +3801us[+5460us] +/- 114ms
^* ntp2.flashdance.cx 2 6 77 23 -5332us[-3671us] +/- 100ms
^+ ntp4.flashdance.cx 2 6 77 21 -46ms[ -46ms] +/- 246ms
^+ li734-36.members.linode.c 2 6 77 23 +31ms[ +31ms] +/- 85ms
查看时间同步源状态
$ chronyc sourcestats -v
210 Number of sources = 4
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
ntp2.itcompliance.dk 4 3 52m -11.356 123.763 -101ms 8974us
61-216-153-107.hinet-ip.h 6 4 43m +0.095 2.053 +2827us 497us
biisoni.miuku.net 6 5 43m -0.260 3.432 -11ms 752us
61-216-153-105.hinet-ip.h 7 6 51m -0.528 0.493 +2358us 162us
$ chronyc
chrony version 1.29.1
Copyright (C) 1997-2003, 2007, 2009-2013 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.
chronyc> help
Commands:
accheck : Check whether NTP access is allowed to
activity : Check how many NTP sources are online/offline
add peer ... : Add a new NTP peer
add server ... : Add a new NTP server
allow [] : Allow NTP access to that subnet as a default
allow all [] : Allow NTP access to that subnet and all children
burst / [/] : Start a rapid set of measurements
clients : Report on clients that have accessed the server
cmdaccheck : Check whether command access is allowed to
cmdallow [] : Allow command access to that subnet as a default
cmdallow all [] : Allow command access to that subnet and all children
cmddeny [] : Deny command access to that subnet as a default
cmddeny all [] : Deny command access to that subnet and all children
cyclelogs : Close and re-open logs files
delete : Remove an NTP server or peer
deny [] : Deny NTP access to that subnet as a default
deny all [] : Deny NTP access to that subnet and all children
dump : Dump all measurements to save files
local off : Disable server capability for unsynchronised clock
local stratum : Enable server capability for unsynchronised clock
makestep : Jump the time to remove any correction being slewed
manual off|on|reset : Disable/enable/reset settime command and statistics
manual list : Show previous settime entries
maxdelay : Modify maximum round-trip valid sample delay for source
maxdelayratio : Modify max round-trip delay ratio for source
maxdelaydevratio : Modify max round-trip delay dev ratio for source
maxpoll : Modify maximum polling interval of source
maxupdateskew : Modify maximum skew for a clock frequency update to be made
minpoll : Modify minimum polling interval of source
minstratum : Modify minimum stratum of source
offline [/] : Set sources in subnet to offline status
online [/] : Set sources in subnet to online status
password [] : Set command authentication password
polltarget : Modify poll target of source
reselect : Reselect synchronisation source
rtcdata : Print current RTC performance parameters
settime : Manually set the daemon time
sources [-v] : Display information about current sources
sourcestats [-v] : Display estimation information about current sources
tracking : Display system time information
trimrtc : Correct RTC relative to system clock
waitsync [max-tries [max-correction [max-skew]]] : Wait until synchronised
writertc : Save RTC parameters to file
authhash : Set command authentication hash function
dns -n|+n : Disable/enable resolving IP addresses to hostnames
dns -4|-6|-46 : Resolve hostnames only to IPv4/IPv6/both addresses
timeout : Set initial response timeout
retries : Set maximum number of retries
exit|quit : Leave the program
help : Generate this help
问题及解决
Failed to create bus connection: No such file or directory
问题:
在Docker容器中尝试修改时区,遇到下面的问题。
[root@43e8c77668a8 opt]# timedatectl
Failed to create bus connection: No such file or directory
解决:
直接修改时区配置文件。
[root@b378039ce558 /]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@b378039ce558 /]# cat /etc/localtime
TZif�\���'p�����Z��6i�p ~h�!Iap"^J�#)Cp$Gg%_�&'I&�A�(+(�#�~�p�CDTCSTTZif2
����~6C)�����\�������'p�������������Z��6i�p ~h�!Iap"^J�#)Cp$Gg%_�&'I&�A�(+(�#�q�~�pLMTCDTCST
CST-8
[root@b378039ce558 /]# date
Mon Oct 9 17:33:06 CST 2017
扩展阅读:
Failed to create bus connection: No such file or directory.
https://bbs.archlinux.org/viewtopic.php?id=218576
简介:
# ln -sf /usr/share/zoneinfo// /etc/localtime
扩展阅读:
RHEL7 -- 使用Chrony设置时间与时钟服务器同步
http://www.mamicode.com/info-detail-1676443.html
简介:
对 chronyd和chronyc 的介绍很好。
[置顶] CentOS7 的时间同步方法
http://blog.csdn.net/ghost_leader/article/details/52915833
简介:
CSDN 上的一篇文章,介绍的很好...
CHAPTER 2. CONFIGURING THE DATE AND TIME
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Configuring_the_Date_and_Time.html
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Using_chrony.html
简介:
redhadt 官网, 解释还是很详细的。
How to configure NTP on CentOS 7
https://blacksaildivision.com/ntp-centos
Set Timezone
https://www.server-world.info/en/note?os=CentOS_7&p=timezone
简介:
介绍了 timedatectl 的使用。
How To Change Timezone on a CentOS 6 and 7
https://www.cyberciti.biz/faq/centos-linux-6-7-changing-timezone-command-line/