早些時候Linux下使用NTP做時間同步。新版Cenots開始用chrony。
這里簡單記錄一下:
# 查看當(dāng)前時間
$ date
2017年 08月 11日 星期五 03:28:44 UTC
# NOTE: 這里顯示的時區(qū)為 UTC(通用協(xié)調(diào)時(UTC, Universal Time Coordinated)。UTC與格林尼治平均時(GMT, Greenwich Mean Time)一樣菲语,都與英國倫敦的本地時相同)應(yīng)該改為:中國標(biāo)準(zhǔn)時區(qū)(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
查看時區(qū):
$ 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: 當(dāng)Chrony啟動時送巡,它會讀取/etc/chrony.conf配置文件中的設(shè)置
$ 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
查看時間同步源狀態(tài)
$ 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 <address> : Check whether NTP access is allowed to <address>
activity : Check how many NTP sources are online/offline
add peer <address> ... : Add a new NTP peer
add server <address> ... : Add a new NTP server
allow [<subnet-addr>] : Allow NTP access to that subnet as a default
allow all [<subnet-addr>] : Allow NTP access to that subnet and all children
burst <n-good>/<n-max> [<mask>/<masked-address>] : Start a rapid set of measurements
clients : Report on clients that have accessed the server
cmdaccheck <address> : Check whether command access is allowed to <address>
cmdallow [<subnet-addr>] : Allow command access to that subnet as a default
cmdallow all [<subnet-addr>] : Allow command access to that subnet and all children
cmddeny [<subnet-addr>] : Deny command access to that subnet as a default
cmddeny all [<subnet-addr>] : Deny command access to that subnet and all children
cyclelogs : Close and re-open logs files
delete <address> : Remove an NTP server or peer
deny [<subnet-addr>] : Deny NTP access to that subnet as a default
deny all [<subnet-addr>] : 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 <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 <address> <new-max-delay> : Modify maximum round-trip valid sample delay for source
maxdelayratio <address> <new-max-ratio> : Modify max round-trip delay ratio for source
maxdelaydevratio <address> <new-max-ratio> : Modify max round-trip delay dev ratio for source
maxpoll <address> <new-maxpoll> : Modify maximum polling interval of source
maxupdateskew <new-max-skew> : Modify maximum skew for a clock frequency update to be made
minpoll <address> <new-minpoll> : Modify minimum polling interval of source
minstratum <address> <new-min-stratum> : Modify minimum stratum of source
offline [<mask>/<masked-address>] : Set sources in subnet to offline status
online [<mask>/<masked-address>] : Set sources in subnet to online status
password [<new-password>] : Set command authentication password
polltarget <address> <new-poll-target> : Modify poll target of source
reselect : Reselect synchronisation source
rtcdata : Print current RTC performance parameters
settime <date/time (e.g. Nov 21, 1997 16:30:05 or 16:30:05)> : 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 <name>: 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 <milliseconds> : Set initial response timeout
retries <n> : 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容器中嘗試修改時區(qū),遇到下面的問題盒卸。
[root@43e8c77668a8 opt]# timedatectl
Failed to create bus connection: No such file or directory
解決:
直接修改時區(qū)配置文件骗爆。
[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
擴(kuò)展閱讀:
Failed to create bus connection: No such file or directory.
https://bbs.archlinux.org/viewtopic.php?id=218576
簡介:
# ln -sf /usr/share/zoneinfo/<MYzone>/<MySubZone> /etc/localtime
擴(kuò)展閱讀:
RHEL7 -- 使用Chrony設(shè)置時間與時鐘服務(wù)器同步
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
redhadt 官網(wǎng)摘投, 解釋還是很詳細(xì)的。
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/