20240503解决Ubuntu20.04和WIN10双系统下WIN10的时间异常的问题

20240503解决Ubuntu20.04和WIN10双系统下WIN10的时间异常的问题
2024/5/3 9:33


缘起:因为工作需要,编译服务器上都会安装Ubuntu20.04。
但是因为WINDOWS强悍的生态系统,偶尔还是有必须要用WINDOWS的时候,于是也安装了WIN10。

双系统的通病,先安装WIN10,再安装Ubuntu20.04之后就会出现WIN10的系统时间晚8个小时的问题!
百度之后:WIN10 ubuntu20.04 时间

1. 原因分析
出现这种情况的原因是 Windows 和 Ubuntu它们在默认情况下看待硬件时间(主板上的BOIS显示的时间)的方式 不一样。

我们先来了解一下关于时间的概念 :
UTC,即Universal Time Coordinated, 协调世界时
GMT,即Greenwich Mean Time, 格林尼治平时
RTC时间,即Real-time clock,是电脑的硬件时间
Windows 与 Linux 缺省看待系统硬件时间的方式是不一样的:

Linux 时钟分为系统时钟(System Clock)和硬件(RTC)时钟。Linux把硬件时间当作 UTC,系统时间是UTC时间经过换算得来的。比如说北京时间是GMT+8,则系统中显示时间是硬件时间+8。
Windows把系统硬件(RTC)时间当作本地时间(local time),即系统时间跟BIOS中显示的时间(RTC)是一样的。


rootroot@rootroot-desktop:~$ sudo apt-get update
rootroot@rootroot-desktop:~$ timedatectl set-local-rtc 1 --adjust-system-clock
rootroot@rootroot-desktop:~$ sudo apt-get install ntpdate
rootroot@rootroot-desktop:~$ sudo ntpdate time.windows.com
rootroot@rootroot-desktop:~$ sudo hwclock --localtime --systohc

rootroot@rootroot-desktop:~$ sudo reboot

# 推荐使用
$ sudo ntpdate ntp.ntsc.ac.cn

https://blog.youkuaiyun.com/ZChen1996/article/details/106131224/?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4--blog-110142773.235^v43^pc_blog_bottom_relevance_base4&spm=1001.2101.3001.4242.3&utm_relevant_index=7
Win10 + Ubuntu20.04双系统下时间不同步问题

rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo apt-get update
[sudo] password for rootroot: 
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [2,892 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [743 kB]                                                                                                                              
Get:7 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [436 kB]                                                                                                                             
Get:8 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [957 kB]                                                                                                                         
Get:9 http://security.ubuntu.com/ubuntu focal-security/universe i386 Packages [656 kB]                                                                                                                          
Get:10 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [201 kB]                                                                                                                        
Hit:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease                                                                                                                                                
Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates InRelease [114 kB]                                                                                                                               
Hit:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports InRelease                                                                                                                                      
Get:11 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 Packages [3,269 kB]                                                                                                                  
Get:12 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main i386 Packages [968 kB]                                                                                                                     
Get:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main Translation-en [518 kB]                                                                                                                    
Get:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe amd64 Packages [1,183 kB]                                                                                                              
Get:15 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe i386 Packages [783 kB]                                                                                                                 
Get:16 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe Translation-en [284 kB]                                                                                                                
Fetched 13.1 MB in 25s (524 kB/s)                                                                                                                                                                               
Reading package lists... Done
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ timedatectl set-local-rtc 1 --adjust-system-clock
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo apt-get install ntpdate
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ntpdate is already the newest version (1:4.2.8p12+dfsg-3ubuntu4.20.04.1).
The following packages were automatically installed and are no longer required:
  chromium-codecs-ffmpeg-extra gstreamer1.0-vaapi libgstreamer-plugins-bad1.0-0 libva-wayland2
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 64 not upgraded.
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo ntpdate time.windows.com
 3 May 09:03:29 ntpdate[3002]: adjust time server 20.189.79.72 offset -0.013272 sec
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo hwclock --localtime --systohc
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo reboot

效果图:


参考资料:
【这个方法时搞不定的!】
https://blog.youkuaiyun.com/X_T_S/article/details/110142773
解决Ubuntu(20.04)和Windows10双系统时间不同步问题

rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo apt-get install ntpdate
[sudo] password for rootroot: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  chromium-codecs-ffmpeg-extra gstreamer1.0-vaapi libgstreamer-plugins-bad1.0-0 libva-wayland2
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  ntpdate
0 upgraded, 1 newly installed, 0 to remove and 56 not upgraded.
Need to get 48.8 kB of archives.
After this operation, 178 kB of additional disk space will be used.
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe amd64 ntpdate amd64 1:4.2.8p12+dfsg-3ubuntu4.20.04.1 [48.8 kB]
Fetched 48.8 kB in 21s (2,364 B/s) 
Selecting previously unselected package ntpdate.
(Reading database ... 194529 files and directories currently installed.)
Preparing to unpack .../ntpdate_1%3a4.2.8p12+dfsg-3ubuntu4.20.04.1_amd64.deb ...
Unpacking ntpdate (1:4.2.8p12+dfsg-3ubuntu4.20.04.1) ...
Setting up ntpdate (1:4.2.8p12+dfsg-3ubuntu4.20.04.1) ...
Processing triggers for man-db (2.9.1-1) ...
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo ntpdate time.windows.com
 3 May 08:54:53 ntpdate[3089]: adjust time server 20.189.79.72 offset -0.021819 sec
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo hwclock --localtime --systohc
rootroot@rootroot-desktop:~$ 
rootroot@rootroot-desktop:~$ sudo reboot

### 配置Ubuntu 20.04Windows系统间的时间同步 #### 安装ntpdate并更新时间 为了使Ubuntu能够获取到准确的时间,可以安装`ntpdate`并通过指定的时间服务器来校准本地时间。 ```bash sudo apt-get install ntpdate sudo ntpdate time.windows.com ``` 上述命令会从Windows的时间服务器下载当前的标准时间,并调整Ubuntu系统的软件时钟以匹配这个标准时间[^1]。 #### 同步硬件时钟 一旦软件时钟被成功设置为正确的时间,则需要将此更改保存至硬件时钟(Hardware Clock),以便于下次启动操作系统时依然保持准确性。这一步骤对于解决双系统环境下的时间差异至关重要: ```bash sudo hwclock --localtime --systohc ``` 这条指令告诉系统按照本地时间而非UTC时间去写入新的时间戳到硬件时钟里,从而确保两个不同的操作系统读取相同的物理时钟值。 #### 设置正确的时区 除了保证时间的一致性外,还需要确认Ubuntu所使用的时区是否正确无误。可以通过下面的方法查看现有的时区配置以及可用选项列表;接着利用`timedatectl`工具设定恰当的目标时区。 ```bash timedatectl status timedatectl list-timezones sudo timedatectl set-timezone Asia/Shanghai ``` 通过这些操作可有效防止由于跨地区造成的额外误差[^4]。 #### 修改GRUB引导菜单中的默认启动项 当涉及到多操作系统共存的情况时,有时可能因为其中一个OS改变了BIOS/UEFI里的RTC(实时时钟)模式而导致另一个出现问题。因此建议在完成以上步骤之后,考虑进入原有的Linux发行版中编辑GRUB配置文件(`/etc/default/grub`),适当调整其中有关Windows的条目位置,使其成为首选加载目标之一。这样做可以在一定程度上减少潜在冲突的发生几率[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值