1、NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步:
直接同步:使用ntpdate命令,直接进行时间变更。如果服务器上存在一个12点运行的任务,当前服务器时间是13点,但标准时间时11点,使用此命令可能会造成任务重复执行。因此使用ntpdate同步可能会引发风险,该命令多用于配置时钟同步服务时第一次同步时间时使用。
平滑同步:使用ntpd进行时钟同步,可以保证一个时间不经历两次,它每次同步时间的偏移量不会太陡,是慢慢来的,这正因为这样,ntpd平滑同步可能耗费的时间比较长。
2、 环境情况
主机类型 | 主机IP | 作用 |
---|---|---|
ntpd服务器 | 192.168.5.136 | 与外部公共ntpd同步标准时间 |
ntpd客户端 | 192.168.5.135 | 与ntpd服务器同步时间 |
3、 检查服务是否安装
检查ntp包是否安装
# rpm -q ntp
ntp-4.2.6p5-19.el7.centos.3.x86_64
若未安装,则进行安装
yum -y install ntp
开机启动
systemctl enable ntpd
4、查看当前系统时区及其它关联信息
# timedatectl status
Local time: Wed 2022-05-18 19:12:39 PDT
Universal time: Thu 2022-05-19 02:12:39 UTC
RTC time: Thu 2022-05-19 02:12:40
Time zone: America/Los_Angeles (PDT, -0700)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2022-03-13 01:59:59 PST
Sun 2022-03-13 03:00:00 PDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2022-11-06 01:59:59 PDT
Sun 2022-11-06 01:00:00 PST
设置对应时区(这里设置为Asia/Shanghai (CST, +0800))
timedatectl set-timezone Asia/Shanghai
同步远程标准服务器
ntpdate -u cn.pool.ntp.org
将系统时间同步到硬件时间
hwclock --systohc
将系统时间同步到CMOS
clock -w
重启之后,硬件时间就不会跳变回原来的时间了,那同步系统时间到硬件时间,为何要多此一举?
5、设置ntp服务器配置
# vim /etc/ntp.conf
...
#restrict 192.168.1.0 mask255.255.255.0 nomodify notrap
# 添加下面一行我的ip是192.168.5.135和192.168.5.136,下面的设置是否正确?
restrict 192.168.5.0 mask255.255.255.0 nomodify notrap
...
# 注释server域名配置
#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
# 添加远程服务
server 2.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
restrict 2.cn.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 2.asia.pool.ntp.org nomodify notrap noquery
#设置本机为ntp服务端
server 127.127.1.0 # localclock
fudge 127.127.1.0 stratum 10
...
6、 设置ntp客户端配置
vim /etc/ntp.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
server 192.168.5.136
restrict 192.168.5.136 nomodify notrap noquery
server 127.127.1.0 # localclock
fudge 127.127.1.0 stratum 10
...
7、启动ntpd服务
systemctl start ntpd
查看网络中的ntp服务器
ntpq -p
查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步
刚启动时
# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
连接并同步后
# ntpstat
synchronised to NTP server(202.112.10.36) at stratum 10
time correct to within 275 ms
polling server every 256 s
部署完之后,还是迷糊状态,无法确定成功了没,哪位大佬可以指点完善,不胜感激!!
相关参数参考1
相关参数参考2
相关参数参考3