我是靠谱客的博主 虚心流沙,这篇文章主要介绍Linux 时间同步服务 详解(附件时区修改)1、时间同步服务介绍2、Chrony3、时间服务器示例4、Linux CentOS 7 时区修改,现在分享给大家,希望可以做个参考。

Linux 时间同步服务 详解

  • 1、时间同步服务介绍
  • 2、Chrony
    • 2.1 介绍
    • 2.2 程序环境
    • 2.3 配置文件(/etc/chrony.conf)
    • 2.4 程序安装及启动
    • 2.4 Chrony 交互式命令查看
    • 2.5 Chrony 命令查看
  • 3、时间服务器示例
    • 3.1 时间同步服务器设置
    • 3.2 时间同步客户端设置
  • 4、Linux CentOS 7 时区修改
    • 4.1 查看时区列表
    • 4.2 查看本机时区
    • 4.3 设置本机时区

1、时间同步服务介绍

NTP 和 Chrony 都是时间同步服务,但 NTP 因程序缺陷,在同步时间时会有时间空白,使用 Chrony 服务时,能精巧实现时间同步。

一般会设置 Chrony 为时间同步服务器,客户端可使用 Chrony 和 NTP 进行时间同步。

2、Chrony

2.1 介绍

chrony is a versatile implementation of the Network Time Protocol (NTP). It can synchronize the system clock with NTP servers, reference clocks (e.g. GPS receiver), and manual input using wristwatch and keyboard. It can also operate as an NTPv4 (RFC 5905) server and peer to provide a time service to other computers in the network.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@Tang-Neo ~]# yum info chrony Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirror.jdcloud.com * updates: mirror.jdcloud.com Installed Packages Name : chrony Arch : x86_64 Version : 3.4 Release : 1.el7 Size : 491 k Repo : installed From repo : base Summary : An NTP client/server URL : https://chrony.tuxfamily.org License : GPLv2 Description : A client/server for the Network Time Protocol, this program keeps your : computer's clock accurate. It was specially designed to support : systems with intermittent internet connections, but it also works well : in permanently connected environments. It can use also hardware reference : clocks, system real-time clock or manual input as time references.
复制代码
1
2
3
4
5
6
[root@Tang-Neo ~]# rpm -ql chrony /etc/NetworkManager/dispatcher.d/20-chrony /etc/chrony.conf ... ... /var/log/chrony

2.2 程序环境

  • 配置文件:/etc/chrony.conf
  • 主程序文件:chronyd
  • 工具程序:chronyc
  • unit file: chronyd.service

2.3 配置文件(/etc/chrony.conf)

关键参数含义:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
server # 指明时间服务器地址 allow NETADD/NETMASK allow all # 允许所有客户端主机 deny NETADDR/NETMASK deny all # 拒绝所有客户端 bindcmdaddress # 命令管理接口监听的地址,一般为了安全,只设置为环回地址 local stratum 10 # 即使自己未能通过网络时间服务器同步到时间,也允许将本地时间作为标准时间授时给其它客户端

2.4 程序安装及启动

复制代码
1
2
3
[root@neo ~]# yum install chrony -y # 程序安装
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@neo ~]# systemctl stop chronyd.service # 停止程序 [root@neo ~]# systemctl start chronyd.service # 启动程序 [root@neo ~]# systemctl restart chronyd.service # 重启程序 [root@neo ~]# systemctl status chronyd.service # 状态查看 ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2019-09-22 09:32:07 EDT; 11s ago Docs: man:chronyd(8) man:chrony.conf(5) Process: 13140 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) Process: 13137 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 13139 (chronyd) CGroup: /system.slice/chronyd.service └─13139 /usr/sbin/chronyd Sep 22 09:32:07 neo systemd[1]: Starting NTP client/server... Sep 22 09:32:07 neo chronyd[13139]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +...UG) Sep 22 09:32:07 neo chronyd[13139]: Frequency -48.864 +/- 0.832 ppm read from /var/lib/chr...ift Sep 22 09:32:07 neo systemd[1]: Started NTP client/server. Sep 22 09:32:11 neo chronyd[13139]: Selected source 192.168.1.9 Hint: Some lines were ellipsized, use -l to show in full.

2.4 Chrony 交互式命令查看

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
[root@Tang-Neo ~]# chronyc # 进入交互模式 chrony version 3.4 Copyright (C) 1997-2003, 2007, 2009-2018 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 # 查看命令帮助 chronyc> sources # 查看时间同步服务器 210 Number of sources = 4 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ undefined.hostname.local> 2 7 77 50 +31ms[ +31ms] +/- 146ms ^* sv1.ggsrv.de 2 7 237 117 -11ms[ -11ms] +/- 98ms ^+ ntp7.flashdance.cx 2 7 327 119 -6561us[-6588us] +/- 125ms ^+ ns.rail.eu.org 2 7 365 53 +1066us[+1066us] +/- 135ms 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 || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || | | zzzz = estimated error. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ undefined.hostname.local> 2 7 77 54 +31ms[ +31ms] +/- 146ms ^* sv1.ggsrv.de 2 7 237 121 -11ms[ -11ms] +/- 98ms ^+ ntp7.flashdance.cx 2 7 327 123 -6561us[-6588us] +/- 125ms ^+ ns.rail.eu.org 2 7 365 57 +1066us[+1066us] +/- 135ms chronyc> sourcestats # 时间同步服务器状态 210 Number of sources = 4 Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== undefined.hostname.local> 17 10 20m +0.145 0.648 +32ms 258us sv1.ggsrv.de 20 10 21m -0.070 0.546 -9759us 244us ntp7.flashdance.cx 17 11 21m -5.482 6.966 -8652us 2617us ns.rail.eu.org 18 8 20m -0.643 5.139 -6010us 1715us 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 ============================================================================== undefined.hostname.local> 17 10 20m +0.145 0.648 +32ms 258us sv1.ggsrv.de 20 10 21m -0.070 0.546 -9759us 244us ntp7.flashdance.cx 17 11 21m -5.482 6.966 -8662us 2617us ns.rail.eu.org 18 8 20m -0.643 5.139 -6011us 1715us chronyc> exit

2.5 Chrony 命令查看

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
[root@Tang-Neo ~]# chronyc sources 210 Number of sources = 4 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ undefined.hostname.local> 2 7 377 20 +32ms[ +32ms] +/- 146ms ^* sv1.ggsrv.de 2 7 177 84 -9294us[-9636us] +/- 97ms ^+ ntp7.flashdance.cx 2 7 137 89 -12ms[ -13ms] +/- 135ms ^+ ns.rail.eu.org 2 7 327 21 +1776us[+1776us] +/- 138ms [root@Tang-Neo ~]# 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 || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || | | zzzz = estimated error. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ undefined.hostname.local> 2 7 377 22 +32ms[ +32ms] +/- 146ms ^* sv1.ggsrv.de 2 7 177 86 -9294us[-9636us] +/- 97ms ^+ ntp7.flashdance.cx 2 7 137 91 -12ms[ -13ms] +/- 135ms ^+ ns.rail.eu.org 2 7 327 23 +1776us[+1776us] +/- 138ms [root@Tang-Neo ~]# chronyc sourcestat -v Unrecognized command [root@Tang-Neo ~]# 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 ============================================================================== undefined.hostname.local> 19 9 24m +0.284 0.539 +32ms 281us sv1.ggsrv.de 21 11 23m -0.021 0.436 -9346us 236us ntp7.flashdance.cx 18 12 23m -6.041 6.341 -9851us 2641us ns.rail.eu.org 20 10 24m -0.555 4.055 -5670us 1736us [root@Tang-Neo ~]# chronyc sourcestats 210 Number of sources = 4 Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== undefined.hostname.local> 19 9 24m +0.284 0.539 +32ms 281us sv1.ggsrv.de 21 11 23m -0.021 0.436 -9346us 236us ntp7.flashdance.cx 18 12 23m -6.041 6.341 -9866us 2641us ns.rail.eu.org 20 10 24m -0.555 4.055 -5671us 1736us

3、时间服务器示例

3.1 时间同步服务器设置

复制代码
1
2
3
4
5
6
7
8
[root@Tang-Neo ~]# vim /etc/chrony.conf 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 allow 192.168.1.0/24 # 只允许 192.168.1 段的客户端进行时间同步
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@Tang-Neo ~]# systemctl restart chronyd.service [root@Tang-Neo ~]# chronyc sources 210 Number of sources = 4 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^+ 0.centos.pool.ntp.org 2 6 35 38 -12ms[ -12ms] +/- 82ms ^? 1.centos.pool.ntp.org 2 7 10 43 +25ms[ +30ms] +/- 270ms ^* 2.centos.pool.ntp.org 3 6 17 43 +528us[+5884us] +/- 92ms ^- ntp1.flashdance.cx 2 6 71 38 -12ms[ -12ms] +/- 123ms

3.2 时间同步客户端设置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@neo ~]# cat /etc/chrony.conf | grep 192.168.1.9 server 192.168.1.9 iburst [root@neo ~]# cat /etc/hosts | grep 192.168.1.9 192.168.1.9 www.neotang.com [root@neo ~]# chronyc sources # * 号代表同步完成 210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* www.neotang.com 4 6 377 45 -11ms[ -10ms] +/- 93ms

4、Linux CentOS 7 时区修改

4.1 查看时区列表

复制代码
1
2
3
4
5
6
[root@Tang ~]# timedatectl list-timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers
复制代码
1
2
3
[root@Tang ~]# timedatectl list-timezones | grep Asia/Shanghai Asia/Shanghai

4.2 查看本机时区

复制代码
1
2
3
[root@Tang ~]# ll /etc/localtime lrwxrwxrwx. 1 root root 33 Oct 17 10:20 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai

4.3 设置本机时区

复制代码
1
2
3
[root@Tang ~]# timedatectl set-timezone Asia/Shanghai [root@Tang ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

最后

以上就是虚心流沙最近收集整理的关于Linux 时间同步服务 详解(附件时区修改)1、时间同步服务介绍2、Chrony3、时间服务器示例4、Linux CentOS 7 时区修改的全部内容,更多相关Linux内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部