学科教研
当前位置: 网站首页 >> 学科教研 >> 技术教研 >> 技术交流 >> 正文
技术交流

Ubuntu时间设置

2011年12月15日 10:53 佚名 点击:[]

每次安装Ubuntu的时候,Ubuntu时间设置总是一个比较麻烦的事情,除了选时区之外,会被UTC和本地时间弄得头大。还有之前装双系统的时候,会出现Windows时间和Linux时间相差8小时的问题。这里需要解决的问题是bios时间和系统时间的同步问题:在开机时,系统从bios获得时间;关机时,系统向bios写回时间。出现相差8小时的问题猜测是因为UTC和本地时间相差8小时(我们处在东八区)。

可以用hwclock来同步。hwclock常用选项如下:
Usage: hwclock [function] [options...]
Functions:
--show read hardware clock and print result
--set set the rtc to the time given with --date
--hctosys set the system time from the hardware clock
--systohc set the hardware clock to the current system time
--adjust adjust the rtc to account for systematic drift since the clock was last set or adjusted
Options:
--utc the hardware clock is kept in coordinated universal time
--localtime the hardware clock is kept in local time
--date specifies the time to which to set the hardware clock

我的目标是为了实现bios和系统时间都使用本地时间。
首先Ubuntu时间设置区:
sudo dpkg-reconfigure tzdata

然后查看系统时间和bios时间:
date; hwclock --show

之后的操作稍微有点技巧,需要判断bios时间比本地时间早还是晚,一般而言,bios时间很可能被设置成UTC了,也就是说会比本地时间晚8小时,那么此时可以用下面的命令把Ubuntu时间设置系统先设置成bios(按照UTC显示的)时间对应的本地时间:
sudo hwclock --localtime --hctosys

再次用 date; hwclock --show 查看会发现当前系统时间被改变了(正确改变成当前时间),但是bios时间仍然不变。

此时再把系统时间写到bios时间上,注意这里的hwclock 不再使用 --localtime 选项:
sudo hwclock --systohc

关闭