# 查看当前系统时间和日期
date
# 查看当前时区设置
timedatectl
# 列出所有可用时区
timedatectl list-timezones
# 设置时区(例如设为亚洲/上海)
sudo timedatectl set-timezone Asia/Shanghai
# 验证时区设置
timedatectl
# 查找可用时区文件
ls /usr/share/zoneinfo
# 创建时区链接(例如设为上海)
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 验证更改
date
# 设置日期和时间(格式:YYYY-MM-DD HH:MM:SS)
sudo timedatectl set-time "2023-11-15 14:30:00"
# 只设置日期
sudo timedatectl set-time "2023-11-15"
# 只设置时间
sudo timedatectl set-time "14:30:00"
# 启用 NTP 时间同步
sudo timedatectl set-ntp true
# 禁用 NTP 时间同步
sudo timedatectl set-ntp false
# 强制立即同步时间(需要安装ntp或chrony)
sudo ntpdate pool.ntp.org
# 或者使用chrony
sudo chronyc -a makestep
# 查看硬件时钟时间
sudo hwclock --show
# 将系统时间写入硬件时钟
sudo hwclock --systohc
# 将硬件时钟时间写入系统时间
sudo hwclock --hctosys
时间同步后仍然不正确:
systemctl status systemd-timesyncd
或 systemctl status chronyd
双系统时间不一致:
sudo timedatectl set-local-rtc 1
(将硬件时钟视为本地时间)时区文件不存在:
sudo apt install tzdata
(Debian/Ubuntu)或 sudo yum install tzdata
(RHEL/CentOS)tzselect
交互式工具dpkg-reconfigure tzdata
(Debian/Ubuntu)或 system-config-date
(RHEL GUI)通过以上方法,您可以准确设置和管理 Linux 系统的时间和时区。对于服务器环境,建议启用 NTP 服务以保持时间同步。