设置时区
设置时区为中国
shell
#查看时区
timedatectl
#查看所有时区
timedatectl list-timezones
#设置时区
timedatectl set-timezone Asia/Shanghai时区文件
/etc/timezone 这个是保存当前时区的文件 /etc/localtime 这个软连接实际指向/usr/share/zoneinfo/xxx,应该是计算时间用的
以上两个文件不要手动修改,请用timedatectl命令设置时区
查看时间
shell
#查看系统时间
date
# 查看硬件时间
hwclock设置时间
shell
#设置年月日
date -s yyyy/mm/dd
#设置时分秒
date -s hh:mm:ss
#同步硬件时间
hwclock --systohc
timedatectl set-time "2025-07-28 14:30:00"
timedatectl status
timedatectl set-ntp false修改时间同步源
Ubuntu使用timesyncd同步时间
配置文件路径:/etc/systemd/timesyncd.conf
修改NTP=xxx.xxx.xxx.xxx
重启时间同步服务
shell
sudo systemctl restart systemd-timesyncd.service| 服务名 | 作用 | 是否负责同步时间 | 常由谁调用 |
|---|---|---|---|
| systemd-timedated.service | 修改时间、时区、NTP 设置接口 | ❌ | timedatectl 等 |
| systemd-timesyncd.service | 轻量 NTP 客户端(实际同步时间) | ✅ | systemd 自身 |
| systemd-time-wait-sync.service | 系统启动时等待同步完成 | ❌ | systemd 启动序列 |