62

The Raspberry Pi lacks a hardware clock. How do I force NTPD to update date/time immediately after every boot?

I'm running Raspbian and the Raspberry Pi is connected using an Ethernet cable.

  • Mine already does that out of the box. What disto are you using? – Gerben Jul 1 '13 at 12:05
  • Raspbian Wheezy – Alessandro Da Rugna Jul 1 '13 at 13:16
  • works as it is, somehow – lenik Jul 1 '13 at 13:57
  • You need Internet connectivity for ntpd to have servers to ask. – Thorbjørn Ravn Andersen Aug 2 '13 at 18:44
  • 1
    @Thorb Pi is connected with ethernet cable / dhcp. It simply does not get a valid date after booting, it takes a while (10 to 45 minutes) – Alessandro Da Rugna Aug 5 '13 at 7:31

11 Answers 11

40

Do an apt-get install ntpdate .

ntpdate will run when an Ethernet interface is brought up, and set the time from an ntp server (see /etc/default/ntpdate).

If ntpd is running, ntpdate will do nothing, however ntpdate will run prior to ntpd at bootup - so this should work out to set the time at bootup as long as there's an Ethernet connection.

  • stock Raspbian will have all this already. – scruss Jul 4 '13 at 1:27
  • 7
    @scruss mine didn't. (2013-02-09 wheezy rasbian) – Pete Kirkham Feb 23 '14 at 14:14
  • 1
    Mine also did not, had to do it manually. – vaindil Feb 28 '15 at 21:31
  • If it is set up correctly it will do it automatically - although it may take a few minutes. – SDsolar Oct 21 '17 at 20:24
  • Linux raspberrypi 4.14.30-v7+ #1102 SMP Mon Mar 26 16:45:49 BST 2018 armv7l GNU/Linux didn't have it too. – emix Apr 19 '18 at 13:50
34

If your clock is way off, you may need to force ntp to sync by doing:

sudo /etc/init.d/ntp stop
sudo ntpd -q -g
sudo /etc/init.d/ntp start
  • 1
    It returns error "sudo: ntpd: command not found" – Pygmalion May 18 '16 at 9:10
  • 1
    ntpd must be installed to use the command – thaspius May 18 '16 at 19:24
  • 3
    It is installed, I am not that ignorant :) – Pygmalion May 18 '16 at 20:04
  • Could you please visit raspberrypi.stackexchange.com/questions/47542/… ? I have posted details there. – Pygmalion May 18 '16 at 20:05
  • 3
    This was absolutely the solution to my problem, but I found it to be temporary until I rebooted. Eventually I caught an error in the shutdown that it couldn't update the hwclock because the hw clock had a future date (no idea how that happened). But the solution was a hwclock update force. That forced the update to the fake hardware clock and then on boot the time was at least close to the current so ntp could do it's job normally. – Piwaf Sep 20 '16 at 18:36
31

After searching around, this method worked for me. As you know, the Raspberry Pi 3 has NTP disabled by default. So by just typing this, the NTP support will be enabled:

sudo timedatectl set-ntp True

Check result with timedatectl status

In case of warning, you may have to run sudo timedatectl set-local-rtc true too.

  • Thanks, this finally helped me out, and seems to be the proper way, despite previous answers. – CharlieS Jun 30 '17 at 1:12
  • 1
    That is the best answer, helped me a lot. You can use 'sudo' to avoid typing your password (useful for scripts that run it). What is the purpose of the seconds command? set-local-rtc – Arad Nov 29 '17 at 13:38
12

If you set the Time Zone in raspi-config the Raspberry Pi will automatically update the time on boot, if connected to the internet.

  1. sudo raspi-config
  2. Select Internationalisation Options
  3. Select I2 Change Timezone
  4. Select your Geographical Area
  5. Select your nearest City
  6. Select Finish
  7. Select Yes to reboot now
  • this worked well for me – AlexG Feb 17 '17 at 6:41
  • I2 Change Timezone option doesn't exist any more – mrded Jun 25 at 21:59
12

See forum post Time does not sync on Pi 3 and with official dongle.

ntpd emits IP/UDP packets with the ToS field set to 0xc0. So that is quite similar to the other issue I (and many others) have with Raspberry Pi3 when operated over a Wi-Fi internal interface.

As a workaround, add the command

/sbin/iptables -t mangle -I POSTROUTING 1 -o wlan0 -p udp --dport 123 -j TOS --set-tos 0x00

to the file

/etc/rc.local

before the exit 0 line. Next, reboot and check.

  • 1
    I had the same issue as Iinstalled the RPi3 I got for XMas,and you solved my problem. Great !!! – jfmessier Dec 27 '16 at 1:49
8

It should already be updating the time. The NTPD server should start just after the swap file and before SSHD.

Check your /etc/ntp.conf file to make sure it is configured.

It should list at least one server. I use time.nrc.ca for my server.

Manual page for ntp.conf is here: http://linux.die.net/man/5/ntp.conf

3

My son's Rasberry Pi would not update the time out of the box and after trying all the suggestions above I could get it to update manually but not automatically. Finally after looking elsewhere as well without success, I found that the ntp.conf file that came with the Rasberry Pi had the following lines commented out:

#restrict 127.0.0.1
#restrict ::1

I used

sudo nano /etc/ntp.conf

in the terminal to edit the ntp conf file to now be:

# Local users may interrogate the ntp server more closely. 
restrict 127.0.0.1
restrict ::1

That solved the problem for us. It updates with both the WiFi and the ethernet cable. I don't know if it would update with WiFi without the change to rc.local suggested above, as by the time I found the solution above I had already made that change and I have not undone it.

1

I found the timedatectl application to help me when I was having a similar issue. It's installed by default and '--help' gives a pretty good and simple overview of how to use the tool.

Note: make sure your timezone is set properly, which this tool can do as well.

1

This is the approach I ended up taking. Many of these answers seemed to indicate things that just didn't check out when I looked at my Raspian system:

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

I had nothing that appeared in logs indicating that timedatectl was doing anything, or that ntp was syncing on the NIC's up state, or that NTPD was even installed/configured.

In the end I merely added this to my /etc/rc.local:

$ cat /etc/rc.local
logger "Beginning force syncing NTP..."
service ntp stop
ntpd -gq
service ntp start
logger "Finished force syncing NTP..."

And installed ntp:

$ sudo apt-get install ntp

Which results in this in /var/log/syslog:

Aug  5 09:10:20 pi-hole ntp[526]: Starting NTP server: ntpd.
Aug  5 09:10:20 pi-hole ntpd[555]: proto: precision = 1.458 usec (-19)
Aug  5 09:10:20 pi-hole ntpd[555]: Listen and drop on 0 v6wildcard [::]:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 2 lo 127.0.0.1:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 3 eth0 192.168.1.85:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 4 lo [::1]:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 5 eth0 [fe80::f5ea:7663:4ec:784d%2]:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listening on routing socket on fd #22 for interface updates
Aug  5 09:10:21 pi-hole ntpd[555]: ntpd exiting on signal 15 (Terminated)
Aug  5 09:10:21 pi-hole ntp[661]: Stopping NTP server: ntpd.
Aug  5 09:10:21 pi-hole ntpd[674]: ntpd 4.2.8p10@1.3728-o Sat Mar 10 18:03:33 UTC 2018 (1): Starting
Aug  5 09:10:21 pi-hole ntpd[674]: Command line: ntpd -gq
Aug  5 09:10:21 pi-hole ntpd[674]: proto: precision = 0.625 usec (-21)
Aug  5 09:10:21 pi-hole ntpd[674]: Listen and drop on 0 v6wildcard [::]:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 2 lo 127.0.0.1:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 3 eth0 192.168.1.85:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 4 lo [::1]:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 5 eth0 [fe80::f5ea:7663:4ec:784d%2]:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listening on routing socket on fd #22 for interface updates
Aug  5 09:10:34 pi-hole ntpd[674]: Soliciting pool server 198.98.57.16
Aug  5 09:10:35 pi-hole ntpd[674]: Soliciting pool server 74.82.59.149
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 23.131.160.7
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 52.37.26.163
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 204.2.134.163
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 96.126.100.203
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 199.223.248.101
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 66.228.42.59
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 208.75.89.4
Aug  5 09:10:37 pi-hole ntpd[674]: Soliciting pool server 204.2.134.164
Aug  5 09:10:37 pi-hole ntpd[674]: Soliciting pool server 104.236.116.147
Aug  5 09:10:38 pi-hole ntpd[674]: Soliciting pool server 204.9.54.119
Aug  5 09:10:48 pi-hole ntpd[674]: ntpd: time set +6.769186 s
Aug  5 09:10:49 pi-hole ntpd[853]: ntpd 4.2.8p10@1.3728-o Sat Mar 10 18:03:33 UTC 2018 (1): Starting
Aug  5 09:10:49 pi-hole ntpd[853]: Command line: /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:114
Aug  5 09:10:49 pi-hole ntp[844]: Starting NTP server: ntpd.
Aug  5 09:10:49 pi-hole ntpd[856]: proto: precision = 0.625 usec (-21)
Aug  5 09:10:49 pi-hole ntpd[856]: Listen and drop on 0 v6wildcard [::]:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 2 lo 127.0.0.1:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 3 eth0 192.168.1.85:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 4 lo [::1]:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 5 eth0 [fe80::f5ea:7663:4ec:784d%2]:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listening on routing socket on fd #22 for interface updates
Aug  5 09:10:49 pi-hole root: Finished force syncing NTP...
  • Doesn't work on minimal raspbian: Failed to start ntp.service: Unit ntp.service not found. And ntpd doesn't exist either. The Pi definitely eventually gains time. (I'm looking into this because my HTTPS curls fail without -k when run prior to that) – Steven Lu Aug 9 '18 at 18:32
  • @StevenLu - I'll update the A'er to include the install of ntp as well. – slm Aug 9 '18 at 20:27
  • @StevenLu - apt-get install ntp – slm Aug 10 '18 at 12:23
1

You don't need to use ntp to solve the issue. There is a script that reads the date from a server and you only need set the date to be the result of that script (your Raspberry Pi needs to be connected to the internet). Then, you need to run that command at startup.

Step1: Retrieve the date from a server.

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

Step2: Run the command at startup by adding it to the file /etc/rc.local (Worked on Ubuntu 16.04).

sudo gedit /etc/rc.local

CREDITS:

1- https://askubuntu.com/a/655528/572294

2- https://askubuntu.com/a/1199/572294

0

I use the following on the default image to sync time without installing additional packages:

timedatectl set-local-rtc 0
timedatectl set-ntp 1
timedatectl status

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.