0.前言
对于很多服务而言,服务器时间的准确性是很重要的,今天我们就来看下rocky9 linux系统有哪些和时间相关的命令。
1.手动设置时间
手动设置时间可以用date命令,举例如下:
date 051723052070
2.配置时区
2.1 查看时间信息
timedatectl status
可以看到本地时间,世界时间,RTC时间和时区等信息.
2.2 查看可用时区
timedatectl list-timezones
可用时区展示如上图所示。
2.3 设置时区
根据服务器展示的时区,可以根据需要将调整服务器时区,命令如下:
timedatectl set-timezone Asia/Shanghai
以上命令可以将时区调整为上海时区。
3.时间同步
今天我们就基于chrony服务,做一下服务器的时间同步。
3.1 安装chrony
安装chrony非常简单,一条命令即可,如下所示:
dnf install chrony -y
3.2 配置chrony
chrony配置文件位置/etc/chrony.conf,可以往配置文件里面添加可用的时间同步服务器地址,这里以server ntp.ntsc.ac.cn iburst,server ntp1.aliyun.com为例. 编辑一下chrony配置文件:
vim /etc/chrony.conf
server ntp.ntsc.ac.cn iburst //中国科学院时间同步服务器
server ntp1.aliyun.com // 阿里云时间同步服务器
# 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
# Specify directory for log files.
logdir /var/log/chrony
3.3 启动chrony服务
配置修改完成之后,重启一下chrony服务,就可以完成时间同步了,命令如下:
systemctl restart chronyd
重启之后,查看下当前时间,命令如下:
date
Mon May 19 03:03:24 PM CST 2025
可以看到,时间已经同步完成.