首先保证系统联网,再执行下面的代码,可以更改时区和当前时间,并且重启系统后不会失效。
代码
#!/bin/bash
echo "change timezone to Asia/Shanghai (UTC)"
sudo timedatectl set-timezone "Asia/Shanghai" #修改时区为上海
sudo timedatectl set-timezone UTC #使用和设置协调世界时间
sudo timedatectl set-local-rtc 0 #将硬件时钟设置为协调世界时(UTC)
echo "restart ntp for update localtime,please wait 10s"
sudo service ntp restart #重启ntp时间同步服务,等待时间更新,一般需要3~6秒
sleep 10s
echo "change Hardware clock"
sudo hwclock -w #将系统时间写入到硬件时钟,防止重启系统后更改失效
sudo timedatectl status
echo "Everything is OK! Please Restart system"
使用方法
将代码保存为.sh文件,使用root用户给执行权限,运行截图。