CentOS8时间同步

CentOS8中默认已经不再支持ntpd软件包,同时也无法通过官方软件仓库安装, CentOS8上使用Chrony配置NTP服务器,用于同步时间。 它有两个程序,chrony和chronyd, chronyd是守护进程 ,chrony是一个命令行界面程序,可以用来监控chronyd的性能,并修改各种操作参数。

安装chrony
系统会默认安装并启动,如果系统不存在则需要安装此工具库。

仓库中直接安装

dnf -y install chrony

 启动守护进程

systemctl enable –now chronyd

时间同步配置

设置时区
联网的情况下一般设置时区后会通过网络同步时间

timedatectl set-timezone "Asia/Shanghai"

配置chrony.conf

修改配置加入aliyun pool网络时间,也可以加入多个pool服务器
vim /etc/chrony.conf

# 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

pool ntp.aliyun.com 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

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

重新加载配置

systemctl restart chronyd.service

时间同步

执行时间同步命令

chronyc sources -v

[root@k8smaster ~]# chronyc sources -v

210 Number of sources = 5

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

^- ntp5.flashdance.cx 2 6 377 6 +8255us[+8255us] +/- 139ms
^- 185.216.231.25 2 7 13 6 +10ms[ +10ms] +/- 98ms
^- 119.28.206.193 2 6 377 9 +1016us[+1001us] +/- 62ms
^* dns1.synet.edu.cn 2 6 377 8 -1423us[-1438us] +/- 9294us
^+ 203.107.6.88 2 6 377 7 +2107us[+2107us] +/- 17ms

验证查看时间
查看当前系统时间
date

Centos8同步时间(阿里云NTP服务为例)

简介: Centos8同步时间(阿里云NTP服务为例)

一、安装chrony

sudo dnf install chrony 

 

二、使用 sed 命令一键完成配置

#pool 2.centos.pool.ntp.org iburst给这一行加注释
sudo sed -i 's/^pool 2.centos.pool.ntp.org iburst/#&/' /etc/chrony.conf
#添加3个阿里云NTP服务器
echo -e "server ntp1.aliyun.com iburst\nserver ntp2.aliyun.com iburst\nserver ntp3.aliyun.com iburst" | sudo tee -a /etc/chrony.conf
 
  三、重启 chronyd 服务以应用更改
sudo systemctl restart chronyd
 

四、设置开机自启

sudo systemctl enable chronyd
 

五、检查 NTP 同步状态

chronyc tracking
 

 

六、强制同步时间

chronyc -a makestep

 

posted @ 2022-03-15 11:22  人生苦短,知足常乐!  阅读(1148)  评论(0)    收藏  举报