User avatar
carriba
Posts: 187
Joined: Tue Nov 18, 2014 5:55 pm

Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 1:38 pm

In my last article viewtopic.php?t=178763 I've shown how the NTP daemon can be replaced with the "systemd-timesyncd" service to synchronise the OS system time with an Internet clock source.

Here, I will show how the "fake-hwclock" service can be easly replaced with the "systemd-timesyncd" service in reading the last saved clock upon an OS reboot:

The "systemd-timesyncd" service keeps track of the system time and updates the file "/var/lib/systemd/clock" with the last time stamp. See also https://unix.stackexchange.com/question ... tp-syncing for more info.

The last updated time stamp of the file "/var/lib/systemd/clock" will be used when starting up the OS, and sets the system time accordingly. Thus, no need to have the "fake-hwclock" running in parallel and periodically store the OS running system time again.

The "fake-hwclock" package can be deinstalled as follows:

Code: Select all

sudo systemctl stop fake-hwclock
sudo apt-get -y remove fake-hwclock
sudo apt-get -y purge fake-hwclock
More details about the deinstallation of the fake-hwclock service can be read under https://www.howtoinstall.co/en/debian/s ... ion=remove.

Next, we will upate the respective configuration file for the "systemd-timesyncd" service with the following commands:

Code: Select all

sudo cp /etc/systemd/timesyncd.conf /etc/systemd/timesyncd.bak
sudo sed -i '/^\[Time/,+2d' /etc/systemd/timesyncd.conf
Then, for Raspbian Stretch confirm the NTP sources with the following command:

Code: Select all

sudo sh -c 'cat >> /etc/systemd/timesyncd.conf' << EOF
[Time]
NTP=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
EOF
Or, for Raspbian Jessie confirm the NTP sources with the following command:

Code: Select all

sudo sh -c 'cat >> /etc/systemd/timesyncd.conf' << EOF
[Time]
Servers=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
EOF
Reinitialise and activate the time services with the following commands:

Code: Select all

sudo systemctl restart systemd-timesyncd
sudo systemctl daemon-reload
Once the "systemd" dameon has been reloaded, the status of the system time and its syncronisation with an NTP source can be queried by firing the command:

Code: Select all

timedatectl
Optionally, you may add the following commands in the "/etc/rc.local" file to ensure that the "systemd-timesyncd" service synchonises with an NTP source during bootup and creates the /var/lib/systemd/clock file, if non-existant:

Code: Select all

sudo sed -i '/^exit 0/d' /etc/rc.local
sudo sh -c 'cat >> /etc/rc.local' << EOF 
[ ! -e /var/lib/systemd/clock -a "\`systemctl is-active systemd-timesyncd | grep -i active\`" ] && timedatectl set-ntp 1 > /dev/null 2>&1
sleep 2
EOF
Hope this helps! And maybe with the next update of the Raspbian image, the "fake-hwclock" package can be excluded ;-)
Last edited by carriba on Wed Dec 20, 2017 3:04 pm, edited 3 times in total.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 1:44 pm

Hope this helps!
Just out of curiosity, how would it help? What change/improvement would the user see having done this procedure?

I.e., what would be the motivation for doing this?

(I'm not saying there isn't. I'm saying it would be more complete if there was a "And this is how you benefit!" section at the end of your post.)
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6392
Joined: Fri Jul 29, 2011 5:36 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 1:55 pm

Oh, interesting. By the sounds of it, we can just remove fake-hwclock altogether, since we're already using systemd-timesyncd in stretch.

User avatar
carriba
Posts: 187
Joined: Tue Nov 18, 2014 5:55 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 2:15 pm

n67 wrote:
Wed Dec 20, 2017 1:44 pm
Hope this helps!
Just out of curiosity, how would it help? What change/improvement would the user see having done this procedure?

I.e., what would be the motivation for doing this?

(I'm not saying there isn't. I'm saying it would be more complete if there was a "And this is how you benefit!" section at the end of your post.)
With "Hope this helps!", I mean the instructions provided ;-)

This said, regarding the technical benefits you may get with this setup, simply refer to the article https://help.ubuntu.com/lts/serverguide/NTP.html, and there may be other related articles available regarding "ntp vs timesyncd".

Hope this (reference provided) helps!

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 3:05 pm

Hope this (reference provided) helps!
It doesn't. I tend not to follow random URLs.

Can you say a bit here about the benefits?

Note: This is not just for me - but for anyone else who happens to read this thread and may be asking themselves (as I am) "Why bother???"
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 31815
Joined: Sat Jul 30, 2011 7:41 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 3:13 pm

n67 wrote:
Wed Dec 20, 2017 3:05 pm
Hope this (reference provided) helps!
It doesn't. I tend not to follow random URLs.

Can you say a bit here about the benefits?

Note: This is not just for me - but for anyone else who happens to read this thread and may be asking themselves (as I am) "Why bother???"
Just follow the link and save our bandwidth.
Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Dec 20, 2017 3:28 pm

OK, I just won't bother with this.

No worries.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
carriba
Posts: 187
Joined: Tue Nov 18, 2014 5:55 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Mon Apr 23, 2018 8:28 am

Since on many deployments of a Raspberry Pi device, I have re-applied on new SD cards flashed Raspbian Stretch the same steps as described with the original post "Raspbian systemd-timesyncd saving system time instead of using fake-hwclock", and am wondering what could be the use (or even benefit) of still using fake-hwclock alongside with systemd-timesyncd?

Upon a reboot, the systemd-timesyncd daemon takes care of restoring the OS system time...

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6392
Joined: Fri Jul 29, 2011 5:36 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Mon Apr 23, 2018 8:40 am

carriba wrote:
Mon Apr 23, 2018 8:28 am
am wondering what could be the use (or even benefit) of still using fake-hwclock alongside with systemd-timesyncd?
Probably none

fred44nl
Posts: 292
Joined: Sat Jun 25, 2016 11:59 am
Location: Scharendijke, NL

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Mon Apr 23, 2018 8:45 am

ShiftPlusOne wrote:
Wed Dec 20, 2017 1:55 pm
Oh, interesting. By the sounds of it, we can just remove fake-hwclock altogether, since we're already using in stretch.
I have updated Jessie to Stretch and I have a file "/lib/systemd/systemd-timesyncd", but fake-hwclock is still running.
headless RPi 3B running from usbhdd.

alberg
Posts: 3
Joined: Thu Sep 29, 2016 5:47 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Wed Nov 21, 2018 5:20 am

Many thanks for the detailed and useful article!

I have just one little qustion. In 239 systemd there are parameters PollIntervalMinSec and PollIntervalMaxSec to regulate the frequency of synchronizations. Is there any way to set sync interval in the Stretch's 232 systemd?

/Al

alberg
Posts: 3
Joined: Thu Sep 29, 2016 5:47 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Fri Nov 23, 2018 7:19 am

I just set up a timer which runs systemd-timesyncd once a day. I think it would be worthile to mention in the article that systemd-timesyncd in 232 systemd will be writing to their storage every 5 minutes or so. (The strangeness of such choice seems to be the cause of subsequential adding the possibility for systemd users to change the frequency of syncs.)

kolsi
Posts: 62
Joined: Wed Jan 23, 2019 10:40 am

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Sat Feb 22, 2020 9:08 am

While optimizing boot process for our device with Raspberry Pi4 (+Buster), I came across this older topic.
I like the idea of removing all unneeded services, especially when their task is already doubled by another existing service.

I'm not sure if I haven't forgotten something, but although systemd-timesyncd restores last known system time on boot, the simply removing fake-hwclock service is not correct. The problem is that systemd-timesyncd executes too late in boot process and thus systemd-fsck-root.service will complain about wrong time and will perform complete file system check on every boot.

The solution would be modifying systemd-timesyncd.service to execute earlier before systemd-fsck-root.service but I'm not sure about the correct steps. Simple adding "Before=systemd-fsck-root.service" completely messes up the boot process.

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6392
Joined: Fri Jul 29, 2011 5:36 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Sat Feb 22, 2020 11:28 am

Since we don't use initramfs, systemd just doesn't work properly in this regard. They said it would be too tricky to fix.

kolsi
Posts: 62
Joined: Wed Jan 23, 2019 10:40 am

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Sat Feb 22, 2020 4:46 pm

So we should rather stay with fake-hwclock for saving/loading last system time on boot before systemd-fsck-root.service and use systemd-timesyncd.service for network time synchronization only?

Then it could be better to change systemd-timesyncd.service dependencies to load after network-online.target only to save some time during the boot.

Code: Select all

[Unit]
DefaultDependencies=no
#After=systemd-remount-fs.service systemd-sysusers.service
#Before=time-sync.target sysinit.target shutdown.target
After=network-online.target
Before=shutdown.target
Conflicts=shutdown.target
#Wants=time-sync.target

...

[Install]
#WantedBy=sysinit.target
WantedBy=multi-user.target

Yosef
Posts: 3
Joined: Fri Jul 29, 2016 12:04 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Sun Oct 25, 2020 8:18 am

IMO fake-hwclock cannot be replaced by systemd-timesyncd in an embedded system whithout internet connection.
Without internet connection, /var/lib/systemd/timesync/clock, the timestamp of the last successful synchronization,
would never be updated.
See manpages of fake-hwclock and systemd-timesyncd.

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6392
Joined: Fri Jul 29, 2011 5:36 pm

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Mon Dec 07, 2020 11:35 am

That mechanism of systemd-timesyncd is broken on systems without initramfs

User avatar
dickon
Posts: 2290
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, in Towcester

Re: Raspbian systemd-timesyncd saving system time instead of using fake-hwclock

Mon Dec 07, 2020 12:53 pm

ShiftPlusOne wrote:
Mon Dec 07, 2020 11:35 am
That mechanism of systemd
You can stop there.
As it is apparently board policy to disallow any criticism of anything, as it appears to criticise something is to criticise all the users of that something, I will no longer be commenting in threads which are not directly relevant to my uses of the Pi.

Return to “Advanced users”