言成言成啊 | Kit Chen's Blog

CentOS配置时钟服务

发布于2024-07-14 21:50:32,更新于2024-07-14 22:44:19,标签:devops  转载随意,文章会持续修订,请注明来源地址:https://meethigher.top/blog

一、ntp协议

1.1 基础

NTP(Network Time Protocol,网络时间协议)是用于同步计算机网络中各个设备时间的协议。

下面了解一下 ntp 的配置选项

1.) iburst

功能: 通过发送一组八个数据包来加速初始同步。

用法:

1
server 0.pool.ntp.org iburst

2.) burst

功能: 在正常操作期间(非启动时)发送一组八个数据包。

用法:

1
server 0.pool.ntp.org burst

3.) prefer

功能: 将服务器标记为首选来源。NTP 将优先选择此服务器。

用法:

1
server 0.pool.ntp.org prefer

4.) minpollmaxpoll

功能: 设置轮询间隔的最小值和最大值,以 2 的幂为单位。默认 minpoll 为 6(64 秒),maxpoll 为 10(1024 秒)。

用法:

1
server 0.pool.ntp.org minpoll 4 maxpoll 7

5.) nopeer

功能: 防止服务器配置对等关系。

用法:

1
restrict default nopeer

6.) noselect

功能: 配置服务器仅用于监控和诊断,不作为同步来源。

用法:

1
server 0.pool.ntp.org noselect

7.) true

功能: 强制 NTP 使用指定的服务器作为时间来源,即使它的时间显著偏差。

用法:

1
server 0.pool.ntp.org true

8.) driftfile

功能: 指定漂移文件的位置,该文件记录系统时钟的频率误差。

用法:

1
driftfile /var/lib/ntp/ntp.drift

9.) logfile

功能: 指定 NTP 事件的日志文件的位置。

用法:

1
logfile /var/log/ntp.log

二、ntp客户端

2.1 查询服务状态

CentOS 中实现 NTP 协议的客户端工具有如下

  1. ntpd:旧版的 NTP 守护进程
  2. chronyd:CentOS7 以上系统推荐使用
  3. system-timesyncd:新版本 CentOS 中使用

这三个工具异同如下

特性ntpdchronydsystemd-timesyncd
精确性
启动速度
配置复杂度
功能丰富性丰富中等基本
适用场景服务器、关键基础设施移动设备、虚拟机桌面系统、轻量级服务器
网络环境适应性较差中等
资源消耗较高较低最低

检查系统当前的时间同步状态

1
timedatectl status

如果我们看到时间同步状态是开启的,那么就需要使用如下命令去确定时间同步的工具了。

1
systemctl list-units --type=service | grep -E 'ntp|chrony|timesyncd'

grep -E 表示启用 grep 的正则表达式。

2.2 ntpd

基操命令如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 安装NTP
sudo yum install ntp

# 编辑配置文件
sudo nano /etc/ntp.conf

# 添加或修改NTP服务器,例如:
# server pool.ntp.org iburst

# 启动并启用NTP服务
sudo systemctl start ntpd
sudo systemctl enable ntpd

# 检查NTP同步状态
ntpq -p

# 手动同步时间
sudo ntpdate pool.ntp.org

# 查看ntpd服务状态
systemctl status ntpd

2.3 chronyd

基操命令如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 安装chrony(如果尚未安装)
sudo yum install chrony

# 编辑配置文件
sudo nano /etc/chrony.conf

# 添加或修改NTP服务器,例如:
# server pool.ntp.org iburst

# 启动并启用chronyd服务
sudo systemctl start chronyd
sudo systemctl enable chronyd

# 检查同步状态
chronyc tracking
chronyc sources

# 手动同步时间
sudo chronyc makestep

# 查看chronyd服务状态
systemctl status chronyd

2.4 system-timesyncd

基操命令如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 编辑配置文件
sudo nano /etc/systemd/timesyncd.conf

# 添加或修改NTP服务器,例如:
# NTP=pool.ntp.org

# 启动并启用timesyncd服务
sudo systemctl start systemd-timesyncd
sudo systemctl enable systemd-timesyncd

# 检查同步状态
timedatectl status

# 手动触发同步
sudo systemctl restart systemd-timesyncd

# 查看timesyncd服务状态
systemctl status systemd-timesyncd
发布:2024-07-14 21:50:32
修改:2024-07-14 22:44:19
链接:https://meethigher.top/blog/2024/centos-synctime/
标签:devops 
付款码 打赏 分享
shift+ctrl+1可控制目录显示