admin 管理员组

文章数量: 1184232

在Linux系统中修改系统时间有多种方法,以下是常用的几种方式:
1. 使用 timedatectl 命令(推荐,适用于systemd系统)

# 查看当前时间设置
timedatectl status
# 设置日期和时间
sudo timedatectl set-time "2025-01-15 14:30:00"
# 只设置日期
sudo timedatectl set-time "2025-01-15"
# 只设置时间
sudo timedatectl set-time "14:30:00"
# 启用/禁用自动时间同步
sudo timedatectl set-ntp true   # 启用
sudo timedatectl set-ntp false  # 禁用

2. 使用 date 命令

# 设置特定日期和时间
sudo date -s "2025-01-15 14:30:00"
# 只设置时间
sudo date -s "14:30:00"
# 只设置日期
sudo date -s "2025-01-15"
# 设置年月日时分
sudo date --set="20250115 14:30"

3. 使用 hwclock 命令(硬件时钟)

# 查看硬件时钟
sudo hwclock --show
# 将系统时间写入硬件时钟
sudo hwclock --systohc
# 从硬件时钟设置系统时间
sudo hwclock --hctosys
# 手动设置硬件时钟
sudo hwclock --set --date="2025-01-15 14:30:00"

4. 使用NTP同步时间

# 安装ntpdate(如果未安装)
sudo apt install ntpdate  # Ubuntu/Debian
sudo yum install ntpdate   # CentOS/RHEL
# 使用NTP服务器同步时间
sudo ntpdate -s time.nist.gov
# 或使用ntp同步
sudo ntpd -gq

5. 手动编辑时间和日期(图形界面)

# 使用图形化工具
sudo system-config-date

注意事项
权限要求:修改系统时间需要root权限或sudo权限
时区设置:确保时区设置正确

# 查看时区
timedatectl list-timezones
   
# 设置时区
sudo timedatectl set-timezone Asia/Shanghai

验证时间修改

# 查看当前系统时间
date
# 查看硬件时钟
hwclock --show
# 查看详细时间信息
timedatectl status

本文标签: 使用 命令 查看当前