關於樹莓派作爲NTP Client同步ntp.aliyun.com時鐘問題,在國內大部分的推薦是安裝NTP服務,然後配置/etc/ntp.conf.
按照這些教程,我折騰了好幾天都沒有進展,沒辦法去GOOGLE找打方案,在這裏分享,避免大家走彎路。
最新的樹莓派系統自帶時鐘同步功能,timedatectl,可以設置時區,UTC等參數。
pi@raspberrypi:~ $ sudo timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
--monitor Monitor status of systemd-timesyncd
-p --property=NAME Show only properties by this name
-a --all Show all properties, including empty ones
--value When showing properties, only print the value
Commands:
status Show current time settings
show Show properties of systemd-timedated
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization
systemd-timesyncd Commands:
timesync-status Show status of systemd-timesyncd
show-timesync Show properties of systemd-timesyncd
See the timedatectl(1) man page for details.
Timedatectl
Timedatectl introduction
The last Raspbian version (lite or desktop) includes timedatectl by default
It’s a tool to manage the date and time on the Raspberry Pi
The first command I’ll teach you is how to check the current status
timedatectl status
This should give you something like this:
So you’ll get:
- The local time
- The universal time (same thing by default)
- The RTC time if configured (module not included on Raspberry Pi)
- The current time zone (GMT by default)
- The current network time synchronization status
As you can see, my Raspberry Pi is already time synchronized by default (except the time zone)
Timedatectl commands
Before going further in the timedatectl configuration, I want to show you some useful commands you can use directly
List time zones
If you need to change the default time zone, you first need to know all available values
To do this, use this command:
timedatectl list-timezones
As the list is big, you can filter it with the grep command
timedatectl list-timezones | grep America
timedatectl list-timezones | grep Sydney
Note your local time zone and use it with the next command
Set time zone
To set the current time zone, use this command:
sudo timedatectl set-timezone <time zone>
For example:
sudo timedatectl set-timezone America/New_York
sudo timedatectl set-timezone Europe/Paris
sudo timedatectl set-timezone Australia/Sydney
Use again the timedatectl status to check that the current time is correct
You can also change the time zone in raspi-config > Localization options > Change time zone
Set the time manually
It’s not really in the state of mind of this post, but you can set the time manually with timedatectl
Here is how:
sudo timedatectl set-time 'Y:M:D HH:mm:ss'
sudo timedatectl set-time 'Y:M:D'
sudo timedatectl set-time 'HH:mm:ss'
For example:
sudo timedatectl set-time '12:00:00'
But to do this, you need to disable the time synchronization (see next paragraph)
Enable or disable the time synchronization
If you want to disable or enable the time synchronization, use these commands:
sudo timedatectl set-ntp false
sudo timedatectl set-ntp true
Note: you may need to reboot the Raspberry Pi to apply this change (see comments)
That’s it, you know the basic timedatectl commands
Timedatectl configuration
The last thing you need to know about timedatectl is how to change the synchronization server
You can do this in the configuration file:
- Open the file
sudo nano /etc/systemd/timesyncd.conf
- Comment out the last line and replace default servers by the servers you want to use (on Internet or from the local network)
- For example:
[Time]
NTP=ntp.aliyun.com
FallbackNTP=0.us.pool.ntp.org 1.us.pool.ntp.org
查看systemd-timesyncd.service的狀態
systemctl status systemd-timesyncd.service
查看日誌
journalctl -u systemd-timesyncd
pi@raspberrypi:~ $ journalctl -u systemd-timesyncd
-- Logs begin at Thu 2019-02-14 18:11:58 CST, end at Mon 2020-04-27 17:53:38 CST. --
Apr 27 17:46:30 raspberrypi systemd[1]: Starting Network Time Synchronization...
Apr 27 17:46:30 raspberrypi systemd[1]: Started Network Time Synchronization.
Apr 27 17:47:11 raspberrypi systemd-timesyncd[262]: Synchronized to time server for the first time 203.107.6.88:123 (203.107.6.8
lines 1-4/4 (END)
參考鏈接
https://feeding.cloud.geek.nz/posts/time-synchronization-with-ntp-and-systemd/
https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/1849156