【树莓派不吃灰】命令篇⑧ 校准树莓派时间

在这里插入图片描述

  • ❤️ 博客主页 单片机菜鸟哥,一个野生非专业硬件IOT爱好者 ❤️
  • ❤️ 本篇创建记录 2022-11-20 ❤️
  • ❤️ 本篇更新记录 2022-11-20 ❤️
  • 🎉 欢迎关注 🔎点赞 👍收藏 ⭐️留言 📝
  • 🙏 此博客均由博主单独编写,不存在任何商业团队运营,如发现错误,请留言轰炸哦!及时修正!感谢支持!
  • 🔥 Arduino ESP8266教程累计帮助过超过1W+同学入门学习硬件网络编程,入选过选修课程,刊登过无线电杂志🔥

1. systemd-timesyncd

在Linux系统中存在两个时钟时间,分别是

  • 硬件时钟RTC(Real Time Clock,RTC)
  • 系统时钟(System Clock)

硬件时钟是指的在主板上的时钟设备,也就是通常可以在BIOS画面设置的时钟,即使关机状态也可以计算时间。

而系统时钟则是指Kernel中的时钟,其值是由1970年1月1日00:00:00 UTC时间至当前时间所经历的秒数总和。当Linux启动的时候,系统时钟会读取硬件时钟的设定,之后系统时钟独立运作。长时间运行两者可能将会产生误差。另外所有的Linux相关指令都是读取系统时钟指定的,如date。

debian使用 systemd-timesyncd 服务来做时间同步。

systemd-timesyncd是断点式更新时间,也就是时间不同立即更新,这样会对某些服务产生影响,所以在生产环境尽量不要用,在桌面环境或者是系统刚开机时来进行时间同步还是很好的。timesyncd 替代了 ntpd 的客户端的部分。默认情况下 timesyncd 会定期检测并同步时间。它还会在本地存储更新的时间,以便在系统重启时做时间单步调整。如果是虚拟机环境,应该把与主机时间同步功能关闭后在启用systemd-timesyncd,否则可能会有问题,systemd-timesyncd只能作为客户端,不能作为NTP服务器,要成为NTP服务器,可以安装chrony、ntpd,或者open-ntp。

1.1 systemd-timesyncd 客户端

systemd 提供了 systemd-timesyncd 客户端,它是一个用于跨网络同步系统时钟的守护服务。

它实现了一个 SNTP 客户端。

与NTP的复杂实现相比,这个服务简单的多,它只专注于从远程服务器查询然后同步到本地时钟。

在/etc/systemd/timesyncd.conf 中配置你的(时间)服务器。大多数 Linux 发行版都提供了一个默认配置,它指向发行版维护的时间服务器上。

systemd-timesyncd只会更改系统时间不会更改硬件时间,可以通过hwclock -w命令将系统时间同步到硬件时间。
|

1.2 systemd-timesyncd 服务

命令:

systemctl status systemd-timesyncd.service

在这里插入图片描述
默认的ntp服务器是 0.debian.pool.ntp.org。我们也可以改成国内地址。

1.3 systemd-timesyncd 配置文件

systemd-timesyncd 启动时会读取 /etc/systemd/timesyncd.conf 配置文件,内容如下:

pi@raspberrypi:~ $ cat /etc/systemd/timesyncd.conf 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
#NTP=
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

默认虽然上面的选项都处于注释状态,但是systemd-timesyncd还是会去默认的NTP服务器进行同步,如果要更改NTP服务器请修改上面的选项,选项解释如下:

  • NTP=
    一个空格分隔的NTP服务器列表, 可以使用主机名,也可以使用IP地址。在运行时, 此处设置的列表将与 systemd-networkd.service中已配置的NTP服务器列表合并在一起。 systemd-timesyncd 将会依次尝试列表中的每个NTP服务器, 直到同步成功为止。 如果为此选项设置一个空字符串, 那么表示清空所有此选项先前已设置的NTP服务器列表。 此选项的默认值为空。

  • FallbackNTP=
    一个空格分隔的NTP服务器列表,用作备用NTP服务器。 可以使用主机名,也可以使用IP地址。 如果所有已配置在 systemd-networkd.service中的NTP服务器以及上述 NTP= 中设置的NTP服务器都尝试失败, 那么将尝试此处设置的备用NTP服务器。 如果为此选项设置一个空字符串, 那么表示清空所有此选项先前已设置的NTP服务器列表。 若未设置此选项, 则使用编译时设置的默认备用NTP服务器(猜测就是0.debian.pool.ntp.org)。

  • RootDistanceMaxSec=
    最大可接受的"root distance"秒数(最大误差)。 默认值为 5 秒。

  • PollIntervalMinSec=, PollIntervalMaxSec=
    NTP消息的 最小/最大轮询间隔秒数。 PollIntervalMinSec= 必须不小于 16 秒。 PollIntervalMaxSec= 必须大于 PollIntervalMinSec= 。 PollIntervalMinSec= 默认为 32 秒, PollIntervalMaxSec= 默认为 2048 秒。

1.4 timedatectl命令

timedatectl命令可以查询和更改系统时钟和设置,你可以使用此命令来设置或更改当前的日期,时间和时区,或实现与远程NTP服务器的自动系统时钟同步。

常用命令:

命令作用
timedatectl 或者 timedatectl status查看系统时间状态
timedatectl set-time YY-MM-DD设定系统日期
timedatectl set-time HH:mm:SS设定系统时间
timedatectl set-time “YY-MM-DD HH:mm:SS”设定系统日期时间
timedatectl list-timezones显示系统的所有时区
timedatectl set-timezone xxxxx设定系统时区,例如 timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0/1设定本地rtc时间计算方式,0表示使用utc时间,1表示本地时间
timedatectl set-ntp true 或者 falsetrue 启用NTP时间同步,false 禁用NTP时间同步

2. 校准时间

2.1 查看时间状态

输入命令:

timedatectl
或者
timedatectl status

pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2022-11-20 09:04:48 GMT
           Universal time: Sun 2022-11-20 09:04:48 UTC
                 RTC time: n/a
                Time zone: Europe/London (GMT, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
pi@raspberrypi:~ $ 

参数说明:

  • Local time 当地时间
  • Universal time 伦敦时间
  • RTC time 硬件时间
  • Time zone 时区
  • System clock synchronized 和远程NTP服务器是否成功同步,显示为yes
  • NTP service NTP时间同步是否开启,active表示是,systemd-timesyncd服务为活动也就是开启了NTP时间同步
  • RTC in local TZ no表示硬件时钟设置为协调世界时(UTC),yes表示硬件时钟设置为本地时间

目前看这个时区Europe/London没有对上。

2.2 校准时区

timedatectl list-timezones | grep Asia/Shanghai

找到上海 Asia/Shanghai,然后校准时区

timedatectl set-timezone Asia/Shanghai

pi@raspberrypi:~ $ timedatectl list-timezones | grep Asia/Shanghai
Asia/Shanghai
pi@raspberrypi:~ $ timedatectl set-timezone Asia/Shanghai
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-timezone ===
Authentication is required to set the system timezone.
Authenticating as: ,,, (pi)
Password: 
==== AUTHENTICATION COMPLETE ===
pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2022-11-20 17:21:11 CST
           Universal time: Sun 2022-11-20 09:21:11 UTC
                 RTC time: n/a
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
pi@raspberrypi:~ $ 

这下子时间对上。

2.3 没有时钟同步服务器,手工设置时间

正常情况下我们依赖于ntp自动同步就好。

如果网络不好,没有时钟同步服务器,也可以手工设置时间。

timedatectl set-time YY-MM-DD HH:mm:SS

pi@raspberrypi:~ $ sudo timedatectl set-time 20:58:00
Failed to set time: Automatic time synchronization is enabled
pi@raspberrypi:~ $ 

直接设置无法成功(需要禁止掉ntp自动同步)。

2.3.1 禁止ntp自动同步

timedatectl set-ntp no

pi@raspberrypi:~ $ timedatectl set-ntp no
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ===
Authentication is required to control whether network time synchronization shall be enabled.
Authenticating as: ,,, (pi)
Password: 
==== AUTHENTICATION COMPLETE ===
pi@raspberrypi:~ $ 

2.3.2 设置时间

设置Linux中的时间。只设置时间的话,我们可以使用set-time开关以及HH:MM:SS(小时,分,秒)的时间格式。

pi@raspberrypi:~ $ timedatectl set-time 20:58:00
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-time ===
Authentication is required to set the system time.
Authenticating as: ,,, (pi)
Password: 
polkit-agent-helper-1: pam_authenticate failed: Authentication failure
==== AUTHENTICATION FAILED ===
Failed to set time: Access denied
pi@raspberrypi:~ $ sudo timedatectl set-time 20:58:00
pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2022-11-20 20:58:05 CST
           Universal time: Sun 2022-11-20 12:58:05 UTC
                 RTC time: n/a
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
pi@raspberrypi:~ $ 
2.3.3 设置日期

在Linux中设置日期。只设置日期的话,我们可以使用set-time开关以及YY-MM-DD(年,月,日)的日期格式。

pi@raspberrypi:~ $ sudo timedatectl set-time 2023-1-1
pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2023-01-01 00:00:03 CST
           Universal time: Sat 2022-12-31 16:00:03 UTC
                 RTC time: n/a
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
pi@raspberrypi:~ $ 
2.3.4 设置日期和时间
pi@raspberrypi:~ $ sudo timedatectl set-time "2023-1-1 11:11:11"
pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2023-01-01 11:11:14 CST
           Universal time: Sun 2023-01-01 03:11:14 UTC
                 RTC time: n/a
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
pi@raspberrypi:~ $ 

2.4 再次启用NTP同步网络时间

上面 2.3 关闭了ntp,正常情况下不应该关闭它,这样可以利用ntp同步网络时间。

pi@raspberrypi:~ $ timedatectl set-ntp true
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ===
Authentication is required to control whether network time synchronization shall be enabled.
Authenticating as: ,,, (pi)
Password: 
==== AUTHENTICATION COMPLETE ===
pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2023-01-01 11:15:51 CST
           Universal time: Sun 2023-01-01 03:15:51 UTC
                 RTC time: n/a
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no
pi@raspberrypi:~ $ sudo systemctl restart systemd-timesyncd.service
pi@raspberrypi:~ $ timedatectl
               Local time: Sun 2022-11-20 19:35:35 CST
           Universal time: Sun 2022-11-20 11:35:35 UTC
                 RTC time: n/a
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
pi@raspberrypi:~ $ 

2.5 配置国内ntp服务器

更改 /etc/systemd/timesyncd.conf 文件

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=ntp.ntsc.ac.cn cn.ntp.org.cn
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

重启systemd-timesyncd服务生效

pi@raspberrypi:~ $ sudo nano /etc/systemd/timesyncd.conf
pi@raspberrypi:~ $ sudo systemctl restart systemd-timesyncd.service
pi@raspberrypi:~ $ sudo systemctl status systemd-timesyncd.service

在这里插入图片描述

2.6 校准RTC

树莓派为了节约成本以及减小体积,没有板载的实时时钟(real-time clock,RTC),或者叫作硬件时钟。

在某些场合下,如果树莓派无法联网,但是它上面运行的程序又和时间紧密相关,要求系统时间是正确的,在这种情况下,我们就可以为树莓派添加一个外部的RTC,使之重启之后也能保持时间正确。

所以略。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

单片机菜鸟爱学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值