Linux-功能模块-服务器时间同步

fansichao 2021-10-23 16:16:35
Categories: Tags:

tags: 时间同步 ntdp chrony Linux

说明:

ntpd 时间同步

前置依赖

1
2
# 安装 ntp
yum install ntp ntpdate -y

服务器配置

ntpd服务器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 配置文件 如图所示,增加 restrict,并注释原本的 server。
# 需要修改 restrict 的 IP 网段。其他不变

服务器 1 ------------------------------------ ntp 服务器
[root@ES1 ~]# vim /etc/ntp.conf
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
server 127.127.1.0
fudge 127.127.1.0
stratum 8
# 开启服务 & 查看服务状态
service ntpd restart && service ntpd status
[root@ES1 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 11 64 377 0.000 0.000 0.000
[root@ES1 ~]# ntpq -4p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 5 l 46 64 377 0.000 0.000 0.000
[root@ES1 ~]# ntpq -6p
remote refid st t when poll reach delay offset jitter
==============================================================================
\*LOCAL(0) .LOCL. 5 l 53 64 377 0.000 0.000 0.000

客户端配置

ntpd客户端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
服务器 2 ------------------------------------ 同步跟随于服务器 1 的时间
[root@ES2 ~]# yum install ntp -y
[root@ES2 ~]# vim /etc/ntp.conf
注释
# server 0
# server 1
# server 2
# server 3
server ES1 # ES1 是 服务器 1 的主机名称
[root@ES2 ~]# service ntpd restart
[root@ES2 ~]# ntpq -p ES1 或 ntpq -p local

# 设置开机自启
chkconfig ntpd on
systemctl enable ntpd
注:修改完后非立即生效,需要等待 15min 左右。

使用 date 查看当前服务器时间

chrony 时间同步

ntp ntpd 是古老的时间同步命令,chrony 是新出已经给应用到 CentoS7 之后的时间同步命令,
建议使用 chrony .

网络时间同步

修改配置文件 /etc/chrony.conf

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
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# pool 2.centos.pool.ntp.org iburst

## 上游公共 ntp 服务器
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 # 增加远程网络

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 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

# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 192.168.0.0/16 # 添加本机网段

# Serve time even if not synchronized to a time source.
#local stratum 10
local stratum 10 # 解除注释

配置 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

[root@fdm ~]# systemctl restart chronyd.service
[root@fdm ~]# systemctl enable chronyd.service
[root@fdm ~]# date
Fri Jun 12 00:58:00 CST 2020
[root@fdm ~]# chronyc sources -v
210 Number of sources = 1

.-- 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
===============================================================================


^? 203.107.6.88 0 6 0 - +0ns[ +0ns] +/- 0ns

[root@fdm ~]# timedatectl
Local time: Tue 2020-06-16 10:40:24 CST
Universal time: Tue 2020-06-16 02:40:24 UTC
RTC time: Thu 2020-06-11 11:12:33
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@fdm ~]# date
Tue Jun 16 10:40:31 CST 2020

集群时间同步

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
## 查看 ntp_servers 状态
chronyc sources -v

## 查看 ntp_sync 状态
chronyc sourcestats -v

## 查看 ntp_servers 是否在线
chronyc activity -v

## 查看 ntp 详细信息
chronyc tracking -v

附件

参考链接:CentOS 下 NTP 安装配置