1.三个阶段的系统时间设定

1.1 内核启动阶段

这里是在menuconfig文件配置RTC设定系统时间选项。

     CONFIG_RTC_HCTOSYS_DEVICE="rtc1",或者直接在deconfig中添加选项。
     没选就不会设置,我猜默认1970-XX-XXX。(因为很久以前看的,没有笔记习惯,所以具体逻辑忘记了)
 
1.2 systemd启动
int clock_apply_epoch(ClockChangeDirection *ret_attempted_change) {
struct stat st;
struct timespec ts;
usec_t epoch_usec, now_usec; /* NB: we update *ret_attempted_change in *all* cases, both
* on success and failure, to indicate what we intended to do! */ assert(ret_attempted_change); if (stat(EPOCH_FILE, &st) < 0) {
if (errno != ENOENT)
log_warning_errno(errno, "Cannot stat " EPOCH_FILE ": %m"); epoch_usec = (usec_t) TIME_EPOCH * USEC_PER_SEC;
} else
epoch_usec = timespec_load(&st.st_mtim); now_usec = now(CLOCK_REALTIME);
if (now_usec < epoch_usec)
*ret_attempted_change = CLOCK_CHANGE_FORWARD;
else if (now_usec > usec_add(epoch_usec, CLOCK_VALID_RANGE_USEC_MAX))
*ret_attempted_change = CLOCK_CHANGE_BACKWARD;
else {
*ret_attempted_change = CLOCK_CHANGE_NOOP;
return 0;
} if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, epoch_usec)) < 0)
return -errno; return 1;
}
 
    systemd版本应该是250。
    默认的情况下,在编译systemd的时候会记录一个编译时间。
   没有特别设置而且是git下来的情况应该是git log中的最近的一次提交记录。
   如果不是git 的,使用的是NEW文件的修改时间。
   参照:meson.build(systemd的Build文件)
   
   上面记录的时间 > RTC的时间  就会重新设置系统时间。
   上面记录的时间 < RTC的时间  保持不变(具体还会判断一下可以超过太多也是不行的)。 
   参照:src/shared/clock-util.c(int clock_apply_epoch(ClockChangeDirection *ret_attempted_change))    
 
1.3 timesyncd启动
 
static int load_clock_timestamp(uid_t uid, gid_t gid) {
_cleanup_close_ int fd = -1;
usec_t min = TIME_EPOCH * USEC_PER_SEC;
usec_t ct;
int r; /* Let's try to make sure that the clock is always
* monotonically increasing, by saving the clock whenever we
* have a new NTP time, or when we shut down, and restoring it
* when we start again. This is particularly helpful on
* systems lacking a battery backed RTC. We also will adjust
* the time to at least the build time of systemd. */ fd = open(CLOCK_FILE, O_RDWR|O_CLOEXEC, 0644);
if (fd >= 0) {
struct stat st;
usec_t stamp; /* check if the recorded time is later than the compiled-in one */
r = fstat(fd, &st);
if (r >= 0) {
stamp = timespec_load(&st.st_mtim);
if (stamp > min)
min = stamp;
} if (geteuid() == 0) {
/* Try to fix the access mode, so that we can still
touch the file after dropping privileges */
r = fchmod_and_chown(fd, 0644, uid, gid);
if (r < 0)
log_warning_errno(r, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE);
} } else {
r = mkdir_safe_label(STATE_DIR, 0755, uid, gid,
MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE);
if (r < 0) {
log_debug_errno(r, "Failed to create state directory, ignoring: %m");
goto settime;
} /* create stamp file with the compiled-in date */
r = touch_file(CLOCK_FILE, false, min, uid, gid, 0644);
if (r < 0)
log_debug_errno(r, "Failed to create %s, ignoring: %m", CLOCK_FILE);
} settime:
ct = now(CLOCK_REALTIME);
if (ct < min) {
struct timespec ts;
char date[FORMAT_TIMESTAMP_MAX]; log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
format_timestamp(date, sizeof(date), min)); if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
log_error_errno(errno, "Failed to restore system clock, ignoring: %m");
} return 0;
}
   这里需要启用ntp服务。
   timedatectl set-ntp true
   没启用ntp时,是不会设置系统时间的。
   设置系统时间的逻辑和systemd相同,也是通过比较决定是否设定。    
1.4默认的ntp服务器
    默认的ntp服务器是google的,国内访问不了,注意改成国内的。
    /etc/systemd/timesysncd.conf
    ntp.aliyun.com

Linux系统时间的设定以及自带的timesync时间同步的更多相关文章

  1. Linux的硬件时间、校正Linux系统时间及系统时间调用流程

    第一部分: 一)概述: 事实上在Linux中有两个时钟系统,分别是系统时间和硬件时间 UTC是协调世界时(Universal Time Coordinated)英文缩写,它比北京时间早8个小时.   ...

  2. Linux_自动调整linux系统时间和时区与Internet时间同步

    调整linux系统时间和时区与Internet时间同步 一.修改时区:# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime修改为中国的东八区# v ...

  3. Linux系统时间同步方法小结

    在Windwos中,系统时间的设置很简单,界面操作,通俗易懂,而且设置后,重启,关机都没关系.系统时间会自动保存在BIOS时钟里面,启动计算机的时候,系统会自动在BIOS里面取硬件时间,以保证时间的不 ...

  4. 自动调整linux系统时间和时区与Internet时间同步

    调整linux系统时间和时区与Internet时间同步 一.修改时区:# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime修改为中国的东八区# v ...

  5. Linux学习之十-Linux系统时间

    Linux系统时间 1.date命令用于查看以及修改Linux系统的时间,关于date命令的详细帮助文档如下 [root@localhost ~]# date --help Usage: date [ ...

  6. linux 系统时间 硬件时间

    Linux时钟分为系统时钟(System Clock)和硬件时钟(Real Time Clock,简称RTC).系统时钟是指当前Linux Kernel中的时钟:而硬件时钟则是主板上由电池供电的时钟, ...

  7. Linux系统时间同步方法

    在Windwos中,系统时间的设置很简单,界面操作,通俗易懂,而且设置后,重启,关机都没关系.系统时间会自动保存在BIOS时钟里面,启动计算机的时候,系统会自动在BIOS里面取硬件时间,以保证时间的不 ...

  8. 修改linux系统时间的方法(date命令)

    修改linux系统时间的方法(date命令) 来源:互联网 作者:佚名 时间:11-18 23:22:27 [大 中 小] date命令不仅可以显示系统当前时间,还可以用它来修改系统时间,下面简单的介 ...

  9. Linux系统时间的设置

    1. Linux系统时间的设置 在Linux中设置系统时间,可以用date命令: //查看时间[root@node1 ~]# dateTue Feb 25 20:15:18 CST 2014//修改时 ...

  10. 时间篇之linux系统时间和RTC时间

    一.linux系统下包含两个时间:系统时间(刚启动时读取的是rtc时间)和RTC时间. 一般情况下都会选择芯片上最高精度的定时器作为系统时间的定时基准,以避免在系统运行较长时间后出现大的时间偏移.特点 ...

随机推荐

  1. JavaScript 中的 apply、call、bind

    在 JavaScript 中,apply.call.bind 是三个与函数调用相关的方法,它们都允许你在调用函数时手动设置函数的上下文(即 this 指向). 1.apply 方法:apply 方法允 ...

  2. JZOJ 7685. 【2022.10.06冲剌NOIP2022模拟】奇怪的函数(function)

    \(\text{Solution}\) 观察到关于 \(x\) 的函数在 \(n\) 个操作之后一定是这样的: 一段水平直线加上一段斜率为 \(1\) 的直线再加上一段水平直线 于是线段树维护这个分段 ...

  3. JZOJ 3469. 【NOIP2013模拟联考7】数列(sequence)

    \(\text{Solution}\) 明显的 \(\text{K-D Tree}\) 基操题 提前给出了数列,那么考虑提前建好树,省去重构 但还是要开 \(O\) \(\text{Code}\) # ...

  4. 【Gerald and Giant Chess】

    一道计数类DP例题~~~ ps:P党似乎不多了-- 我这只蒟蒻第一次写题解,而且计数类DP还是早上刚学的,现学现用,或者说是所谓的"浅谈"一番吧!况且这题写题解的人似乎并不多(大佬 ...

  5. 如何在Ubuntu 20.04上安装和保护phpMyAdmin

    转载csdn:cukw6666 https://blog.csdn.net/cukw6666/article/details/107985170

  6. vue .sync的理解

    假如父组件传给子组件的值,子组件接受之后,想要改变父组件传过来的值,就可以使用sync .sync是vue中用于实现简单的"双向绑定"的语法糖,在平时的开发中是非常使用的. vue ...

  7. LRU 居然翻译成最近最少使用?真相原来是这样!(附力扣题解)

    前言 相信有很多同学和我一样,第一次碰到 LRU(Least Recently Used) 的这个解释「最近最少使用」都不知道是什么意思,用汤家凤老师的话来说: 我真的感到匪夷所思啊! 最近是表示时间 ...

  8. day07-MyBatis的关联映射01

    MyBatis的关联映射 Mybatis的关联映射 实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系.针对多表之间的操作,MyBatis提供了关联映射, ...

  9. umi学习记录

    菜单路由: 方法1:在config中配置全部路由,由后台返的的菜单进行匹配 方法2:动态注册路由和添加菜单--目前仅尝试了umi@2 的版本 原理:使用umi的运行时配置patchRoutes和ren ...

  10. 2023 年 CCF 春季测试赛模拟赛 - 1

    T1 个人思路: 询问:求 \(1\) 到 \(t_i\) 路径上离 \(1\) 最远的 \(p\),使得 \(dis_{1,p} \times 2 \le d_i\).即 \(dis_{1,t} \ ...