第 30 章 使用 RHEL 系统角色配置时间同步
Network Time Protocol(NTP)和 Precision Time Protocol (PTP)是通过网络同步计算机时钟的标准。网络中准确的时间同步非常重要,因为某些服务依赖它。例如,Kerberos 只容许服务器和客户端之间很少的时间差异,以防止重播攻击。
您可以在 playbook 的 timesync_ntp_provider
变量中设置要配置的时间服务。如果没有设置此变量,角色会根据以下因素决定时间服务:
-
在 RHEL 8 及更新版本上:
chronyd
-
在 RHEL 6 和 7 上:
chronyd
(默认)或者如果已安装了ntpd
。
30.1. 使用 timesync RHEL 系统角色配置通过 NTP 的时间同步
Network Time Protocol(NTP)通过网络,将主机的时间与 NTP 服务器同步。在 IT 网络中,服务依赖于正确的系统时间,例如,出于安全和日志记录目的。通过使用 timesync
RHEL 系统角色,您可以在网络中自动配置 Red Hat Enterprise Linux NTP 客户端,并保持时间同步。
timesync
RHEL 系统角色在受管主机上替换了指定的或检测到的提供商服务的配置。因此,如果没有在 playbook 中指定,所有设置都会丢失。
先决条件
- 您已准备好控制节点和受管节点。
- 以可在受管主机上运行 playbook 的用户登录到控制节点。
-
用于连接到受管节点的帐户具有
sudo
权限。
流程
创建一个包含以下内容的 playbook 文件,如
~/playbook.yml
:--- - name: Managing time synchronization hosts: managed-node-01.example.com tasks: - name: Configuring NTP with an internal server (preferred) and a public server pool as fallback ansible.builtin.include_role: name: rhel-system-roles.timesync vars: timesync_ntp_servers: - hostname: time.example.com trusted: yes prefer: yes iburst: yes - hostname: 0.rhel.pool.ntp.org pool: yes iburst: yes
示例 playbook 中指定的设置包括以下内容:
pool: <yes|no>
- 将源标记为 NTP 池,而不是单个主机。在这种情况下,服务预期名称解析为可能会随时间变化的多个 IP 地址。
iburst: yes
- 启用快速初始同步。
有关 playbook 中使用的所有变量的详情,请查看控制节点上的
/usr/share/ansible/roles/rhel-system-roles.timesync/README.md
文件。验证 playbook 语法:
$ ansible-playbook --syntax-check ~/playbook.yml
请注意,这个命令只验证语法,不会防止错误但有效的配置。
运行 playbook:
$ ansible-playbook ~/playbook.yml
验证
显示时间源的详情:
如果受管节点运行
chronyd
服务,请输入:# ansible managed-node-01.example.com -m command -a 'chronyc sources' MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* time.example.com 1 10 377 210 +159us[ +55us] +/- 12ms ^? ntp.example.org 2 9 377 409 +1120us[+1021us] +/- 42ms ^? time.us.example.net 2 9 377 992 -329us[ -386us] +/- 15ms ...
如果受管节点运行
ntpd
服务,请输入:# ansible managed-node-01.example.com -m command -a 'ntpq -p' remote refid st t when poll reach delay offset jitter ============================================================================== *time.example.com .PTB. 1 u 2 64 77 23.585 967.902 0.684 - ntp.example.or 192.0.2.17 2 u - 64 77 27.090 966.755 0.468 +time.us.example 198.51.100.19 2 u 65 64 37 18.497 968.463 1.588 ...
其他资源
-
/usr/share/ansible/roles/rhel-system-roles.time_sync/README.md
file -
/usr/share/doc/rhel-system-roles/time_sync/
directory - 红帽是否支持 rhel.pool.ntp.org NTP 服务器?(红帽知识库)