HallMark
Posts: 8
Joined: Fri Sep 29, 2017 11:18 am

Sync Time from Internet

Mon Oct 02, 2017 1:38 pm

Hello,

I am using 4G LTE USB Stick with my raspberry Pi. Which usually remains OFF only. Once every 2-4 days I turn it ON for task to do and at the same time when I turn on Stick internet I want to sync my time from internet.
What is best way to get this working?

I am using Python to record my data from my I2C Sensors. So What is best way/command I can execute from python and sync time.

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sync Time from Internet

Mon Oct 02, 2017 8:59 pm

There's network time support in a standard Stretch install.

sudo timedatectl set-time '2017-10-02 21:59'
will reset the clock if the automatic sync isn't enabled.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

HallMark
Posts: 8
Joined: Fri Sep 29, 2017 11:18 am

Re: Sync Time from Internet

Tue Oct 03, 2017 1:40 am

DougieLawson wrote:
Mon Oct 02, 2017 8:59 pm
There's network time support in a standard Stretch install.

sudo timedatectl set-time '2017-10-02 21:59'
will reset the clock if the automatic sync isn't enabled.
Network time? I just want to sync time with some time server

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sync Time from Internet

Tue Oct 03, 2017 6:18 am

That's precisely what "network time" defines.

Start by running timedatectl with no parameters.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

HallMark
Posts: 8
Joined: Fri Sep 29, 2017 11:18 am

Re: Sync Time from Internet

Tue Oct 03, 2017 7:54 am

DougieLawson wrote:
Tue Oct 03, 2017 6:18 am
That's precisely what "network time" defines.

Start by running timedatectl with no parameters.
Ok, I see. So just timedatectl command is going to get time from the GSM network and set as Rpi's time - Correct?

HallMark
Posts: 8
Joined: Fri Sep 29, 2017 11:18 am

Re: Sync Time from Internet

Tue Oct 03, 2017 11:15 am

I just did one test

Code: Select all

[email protected]:~/test $ date
Mon  3 Oct 17:45:18 UTC 2017
[email protected]:~/test $ sudo timedatectl set-time '2017-10-02 21:59'
[email protected]:~/test $ date
Mon  2 Oct 21:59:01 UTC 2017
[email protected]:~/test $ timedatectl
      Local time: Mon 2017-10-02 21:59:09 UTC
  Universal time: Mon 2017-10-02 21:59:09 UTC
        RTC time: n/a
       Time zone: Etc/UTC (UTC, +0000)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[email protected]:~/test $ date
Mon  2 Oct 21:59:17 UTC 2017
[email protected]:~/test $
It has not sync time from time server I think.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Sync Time from Internet

Tue Oct 03, 2017 1:06 pm

I've never used timedatectl before (but like learning things) so what's below is just my observations/research and Dougie can correct me if I get it wrong:

The command has done exactly what it's meant to do - you have set the time to the specified time and date (see the man page for an explanation).

Notice that your NTP (network time) is not currently enabled, you might want to make sure that's enabled to get network time enabled.

I would assume (Dougie, correct me if I'm wrong) that the service will automatically update the time as soon as an internet connection is available (as the service can then contact the NTP server to request the current time).
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

HallMark
Posts: 8
Joined: Fri Sep 29, 2017 11:18 am

Re: Sync Time from Internet

Tue Oct 03, 2017 1:24 pm

elParaguayo wrote:
Tue Oct 03, 2017 1:06 pm
I would assume (Dougie, correct me if I'm wrong) that the service will automatically update the time as soon as an internet connection is available (as the service can then contact the NTP server to request the current time).
I see ! I am using Dongle with very limited data plan. So I think need something so I can enable NTP Server and sync time and then again disable it right away so I can avoid unwanted data transfer?

jahboater
Posts: 4840
Joined: Wed Feb 04, 2015 6:38 pm

Re: Sync Time from Internet

Tue Oct 03, 2017 4:47 pm

DougieLawson wrote:
Tue Oct 03, 2017 6:18 am
Start by running timedatectl with no parameters.
Thats a handy command I didn't know about! Thanks.

I've added it to the .bashrc on my Pi Zero W digital clock so I can see all is well when I log in.

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sync Time from Internet

Wed Oct 04, 2017 6:16 am

HallMark wrote:
Tue Oct 03, 2017 1:24 pm
elParaguayo wrote:
Tue Oct 03, 2017 1:06 pm
I would assume (Dougie, correct me if I'm wrong) that the service will automatically update the time as soon as an internet connection is available (as the service can then contact the NTP server to request the current time).
I see ! I am using Dongle with very limited data plan. So I think need something so I can enable NTP Server and sync time and then again disable it right away so I can avoid unwanted data transfer?
It changed with Raspbian stretch. NTP isn't installed by default. Timedatectl includes a service to set the clock from a time service (port 123 somewhere out in the public internet). To get the NTP piece working you need
sudo apt install ntp
sudo systemctl enable ntp
sudo timedatectl set-ntp 1


The way I run NTP is my main public webserver has a stratum 2/3 NTP server using internet time sources. All thirteen other raspberries use that as a stratum 3/4 NTP server. It means that all of my raspberries are on the same timebase.

The stuff in timedatectl in Stretch is a bit primitive and relies on the hardware clock not drifting, it doesn't sync as often as NTP.

In the grand scheme of things the number of bytes flying round my network for ntp timesync isn't even a drop in the ocean of video megabytes that my youngest gets from YouTube.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

bluewhale9
Posts: 17
Joined: Sun May 06, 2018 1:18 am

Re: Sync Time from Internet

Sun May 13, 2018 3:21 am

I'm not sure this is working correctly.

I'm sitting next to my RasPI 3B+ and a windows server. I have the option to sync windows to time.windows.com or time.nist.gov

After trying the linux commands in this thread, and enabling internet time sync on windows, both machines are still off by 25-50 seconds

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sync Time from Internet

Sun May 13, 2018 8:28 am

What do you get from
ntptime
ntpq -pn

on your RPi

I get

Code: Select all

[email protected]:/var/log$ ntptime
ntp_gettime() returns code 0 (OK)
  time dea2752f.3ecbea50  Sun, May 13 2018  8:26:23.245, (.245299784),
  maximum error 181717 us, estimated error 280 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 13.170 us, frequency -10.012 ppm, interval 1 s,
  maximum error 181717 us, estimated error 280 us,
  status 0x2001 (PLL,NANO),
  time constant 10, precision 0.001 us, tolerance 500 ppm,
[email protected]:/var/log$ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+46.101.50.70    140.203.204.77   2 u 1058 2048  377    9.800    0.347   0.397
-95.215.175.2    217.114.59.3     3 u    7 2048  377   11.984   -0.960   0.298
*185.182.62.7    85.199.214.98    2 u  441 2048  377    9.973    0.072   0.115
+185.53.93.157   195.66.241.2     2 u 1131 2048  377   14.234   -0.451   0.554
-91.189.91.157   132.246.11.231   2 u 1922 1024  266   77.477   -2.123   1.027
[email protected]:/var/log$
It's the low values of "estimated error" from the first command and "offset" from the second that are important.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Mon Jan 07, 2019 8:37 am

Hello,
I installed the latest stretch version without being connected to the internet. After everything was set, I connected to the internet and did a reboot. Since then I can't synchronise the clock with an ntp server, probably because it's to far away from the correct date.
How do I force to synchronise my ntp client? This is what I did so far:
-installation of the ntpd client
- reboot
- timedatectl status
result:
Local time: Sa 2018-11-17 18:22:43 CET
Universal time: Sa 2018-11-17 17:22:43 UTC
RTC time: n/a
Time zone: Europe/Luxembourg (CET, +0100)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no

...as we have today 07.01.2019 you can clearly see, that the ntp client didn't synchronise the clock.

Before I redo the installation procedure again (this time connected to the internet), I'd like to find a way how to force my clock to synchronise with an ntp server in a smart way. I could also set date and time close to the current value...this might help to...
Greetings and have a good day!
Oliver

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Mon Jan 07, 2019 1:14 pm

Hello again,
I observed now something strange. I set the system time and date manually close to the real time (given by German PTB atom clock: https://uhr.ptb.de). You can check the deviation of your local system clock to the PTB reference clock by clicking on "Abweichung".
However, I discovered, that after each reboot the difference between my clock and the PTB clock was about 7 seconds smaller (my clock went ahead). During several reboots I came as close as 0.5 seconds...and I thought, now the ntp client would finally work...but it didn't. Continous rebooting result in the situation that the clock goes behind now (again about additional 7 seconds after each rebooting).
Greetings!
Oliver

edit: the about 7 seconds difference is the delay during rebooting. It's simply that the clock stops at the time I shut down. I made the test with a normal shutdown, waited 2 minutes and booted again...the clock is now about 2 minutes behind.
Last edited by olio on Mon Jan 07, 2019 1:46 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sync Time from Internet

Mon Jan 07, 2019 1:22 pm

Have you really replaced timesyncd with ntp (DebIan changed to timesyncd with Stretch)?

My ntp set-up uses this:

Code: Select all

driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
server 0.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 1.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 2.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 3.ubuntu.pool.ntp.org burst iburst maxpoll 11
server ntp.ubuntu.com
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.1.0 mask 255.255.255.0
restrict -6 2001:pppp:pppp:pppp:: mask fffff:fffff:fffff:ffff::
Tell timesync you're using ntp with sudo timedatectl set-ntp 1.

The initial ntp sync is done with sudo systemctl stop ntp; sudo ntpd -q -g; sudo systemctl start ntp if the clock is more than 1000 seconds off.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Mon Jan 07, 2019 2:04 pm

Hello Dougie,
thanks for your reply! Yes I did this also before I wrote my first post in this thread. I repeat the commands again. But as soon as I enter "sudo ntpd -q -g" I get this:

Code: Select all

 7 Jan 14:53:07 ntpd[901]: ntpd [email protected] Sat Mar 10 18:03:33 UTC 2018 (1): Starting
 7 Jan 14:53:07 ntpd[901]: Command line: ntpd -q -g
 7 Jan 14:53:07 ntpd[901]: proto: precision = 1.406 usec (-19)
 7 Jan 14:53:07 ntpd[901]: Listen and drop on 0 v6wildcard [::]:123
 7 Jan 14:53:07 ntpd[901]: Listen and drop on 1 v4wildcard 0.0.0.0:123
 7 Jan 14:53:07 ntpd[901]: Listen normally on 2 lo 127.0.0.1:123
 7 Jan 14:53:07 ntpd[901]: Listen normally on 3 eth0 10.150.0.82:123
 7 Jan 14:53:07 ntpd[901]: Listen normally on 4 lo [::1]:123
 7 Jan 14:53:07 ntpd[901]: Listen normally on 5 eth0 [fe80::703a:b5eb:d63d:4c57%2]:123
 7 Jan 14:53:07 ntpd[901]: Listening on routing socket on fd #22 for interface updates
 7 Jan 14:53:08 ntpd[901]: Soliciting pool server 185.137.97.5
 7 Jan 14:53:09 ntpd[901]: Soliciting pool server 80.90.43.162
 7 Jan 14:53:10 ntpd[901]: Soliciting pool server 185.137.97.4
 7 Jan 14:53:11 ntpd[901]: Soliciting pool server 85.93.216.115
 7 Jan 14:54:13 ntpd[901]: Soliciting pool server 94.242.208.130
 7 Jan 14:54:15 ntpd[901]: Soliciting pool server 213.167.241.72
 7 Jan 14:54:15 ntpd[901]: Soliciting pool server 46.29.177.16
 7 Jan 14:54:16 ntpd[901]: Soliciting pool server 46.29.177.17
it keeps on adding lines even if I start "sudo systemctl start ntp" in another terminal.

Doing a reboot doesn't change anything. Maybe there is a bug with my local settings and the date settings? I use "Luxembourg" with language "Luxembourgish" in order to avoid French or German as system language. This setting kept English for almost everything, but the names for the days in the Calender are in fact in Luxembourgish.

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Mon Jan 07, 2019 2:13 pm

if I enter
ntptime
ntpq -pn
I get the following

Code: Select all

ntptime
ntp_gettime() returns code 0 (OK)
  time dfdddb00.6162f000  Mon, Jan  7 2019 15:04:48.380, (.380416),
  maximum error 225016 us, estimated error 16 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 0.000 us, frequency 0.000 ppm, interval 1 s,
  maximum error 225016 us, estimated error 16 us,
  status 0x1 (PLL),
  time constant 7, precision 1.000 us, tolerance 500 ppm,
[email protected]_granules:~ $ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 1.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 2.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 3.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
Obviously the client didn't receive anything from the pools of ntp servers

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Mon Jan 07, 2019 2:27 pm

maybe another important detail:

sudo systemctl enable ntp
results in the following message:

Code: Select all

ntp.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ntp
after I executed the redirected command line, I get no error message.

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sync Time from Internet

Mon Jan 07, 2019 2:29 pm

Something is firewalling your access to the DebIan pool servers or blocking port 123.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Mon Jan 07, 2019 2:41 pm

OK...that's again a question to our network administrators.

olio
Posts: 35
Joined: Tue Feb 02, 2016 8:55 am

Re: Sync Time from Internet

Tue Jan 08, 2019 8:25 am

just a short feedback. I have still no reply from our admin, but I was able to apply the following commands as Dougie suggested on another Pi in a different network (but it's a model 3B with Jessie, the internal clock was a few days behind todays date and time):

Code: Select all

sudo systemctl stop ntp
sudo ntpd -q -g
Then I get the following:

Code: Select all

ntpd: time set +316881.974623s
The forced synchronising of the internal clock with an ntp server is working well in this case. As you can see the difference was largely outside the limit range for the synch (1000 seconds).

If it's possible I will connect the new 3B+ with Stretch to the same network and see, if it's working

Have a great day!
Oliver

Return to “General programming discussion”