在 Linux 服务器上校对时间的常用命令如下:
# 设置系统时间(格式:YYYY-MM-DD HH:MM:SS)
sudo date -s "2023-11-20 14:30:00"
# 将系统时间同步到硬件时钟(重启后生效)
sudo hwclock --systohc
ntpdate
(需安装 ntp
或 ntpdate
包)# 安装工具(根据发行版选择)
sudo apt install ntpdate # Debian/Ubuntu
sudo yum install ntpdate # CentOS/RHEL
# 手动同步(以阿里云NTP服务器为例)
sudo ntpdate ntp.aliyun.com
# 常见NTP服务器:
- ntp.aliyun.com
- time.windows.com
- pool.ntp.org
- cn.pool.ntp.org
chrony
(现代Linux默认工具)# 安装(如未预装)
sudo apt install chrony # Debian/Ubuntu
sudo yum install chrony # CentOS/RHEL
# 启动服务并同步
sudo systemctl start chronyd
sudo systemctl enable chronyd
# 检查同步状态
chronyc tracking
chronyc sources -v
systemd-timesyncd
(轻量级方案)# 启用服务(部分发行版默认使用)
sudo timedatectl set-ntp true
# 检查状态
timedatectl status
# 查看系统时间、时区、NTP状态
timedatectl
# 查看硬件时钟时间
hwclock --show
# 列出可用时区
timedatectl list-timezones
# 设置时区(例如亚洲上海)
sudo timedatectl set-timezone Asia/Shanghai
root
或 sudo
权限。chrony
或 systemd-timesyncd
持续同步,避免手动修改。如有其他需求(如配置NTP服务器),可进一步补充说明。