54

I don't have to set the clock (besides the timezone) on the Raspberry Pi. Where is it getting the time from? About how accurate is it?

Scoop
  • 2,571
  • 7
  • 26
  • 25
  • Fresh installs have it pick up time from a variety of sources. For details, and for the reason why you should change it to time.nist.gov, see this Q&A: https://raspberrypi.stackexchange.com/questions/68811/how-do-i-set-raspbian-to-use-the-primary-time-server-time-nist-gov – SDsolar Jun 24 '17 at 04:48

6 Answers6

49

Raspbian gets the time from an NTP Server (a "time server").

Unplug your Raspberry Pi completely, pull out the network cable and start the Raspberry Pi up again. You will see that the date and time are incorrect.

If you want/need the date and time to not be reset without being plugged into the internet, you'll need a Real Time Clock (RTC) - for example, something like this - which will keep the time current, by using a battery to run a clock. This is, also, how your computer keeps the time when it is not being powered.

UPDATE Just a side note that the above RTC clock is not the only way to keep the time accurate between power losses. You could also use a GPS module and hook that up to the GPIO pins.

Accuracy It's pretty accurate, although if your clock's time is off by quite a bit. It may take up to 3 hours to correct itself, as changes from the server are applied gradually to your local clock. Network stability has the biggest impact on the accuracy, as a unpredictable network (something more wireless, like 3G) will make it very hard to be accurate. To put it simply: The accuracy is pretty good, the time difference between your computer and the actual time (from the NTP Server) is normally less than 100ms.

Vincent P
  • 2,190
  • 13
  • 24
  • 1
    nice answer. thanks for the link about hardware clock – Scoop Jan 07 '13 at 21:47
  • 1
    Nice answer; it could be improved by mentioning the accuracy of the NTP protocol. – Alex Chamberlain Jan 09 '13 at 13:02
  • @AlexChamberlain Thanks for the heads up, I was going to add it but it completely slipped my mind. I've added it now. – Vincent P Jan 10 '13 at 05:41
  • While my Pi is network connected, I'm noticing that time has drifted by 30mins now. Is the ntpd enabled by default or do I need to do this manually? – andig Jun 06 '13 at 10:41
  • Maybe so, until Raspberry Pi 3. I bought one two months ago and NTP time update is screwed, well at least on official Raspbian Jessie. I opened three threads with that problem and everyone are clueless. I had to buy hardware module for time keeping to solve the problem. – Pygmalion May 23 '16 at 07:47
  • Upvote. And make sure you are reaching a Stratum-1 server instead of the default Stratum-16 pools that come with stock Raspbian and most ubuntu and Windows installs. Whenever you see the word pool you know there are a lot of computers between you and the Master Clock. So do this instead: https://raspberrypi.stackexchange.com/questions/68811/how-do-i-set-raspbian-to-use-the-primary-time-server-time-nist-gov/68812#68812 – SDsolar Jun 23 '17 at 07:23
  • While ntp servers are still used in Raspbian Stretch to update the time, systemd now takes care of this rather than the ntp application. – b_laoshi Mar 30 '18 at 01:26
  • "if you clock's time is off by quite a bit" Typically system time is set using current NTP time before starting the NTP daemon process that "qualifies" local time to keep it within a few milliseconds of actual time. – Chad Farmer Apr 26 '19 at 18:28
22

Morgan's answer gave me the hint I needed to solve the problem.

  1. Go to http://support.ntp.org/bin/view/Servers/NTPPoolServers
    Select your region, then your country and a list of servers will be display.

  2. Edit your /etc/ntp.conf file e.g.

    sudo vim /etc/ntp.conf

    Replace the list of servers with the one you found in the webpage. e.g.

    server 0.dk.pool.ntp.org iburst
    server 1.dk.pool.ntp.org iburst
    server 2.dk.pool.ntp.org iburst
    server 3.dk.pool.ntp.org iburst

    Save and exit.

  3. Restart the ntp deamon.

    sudo /etc/init.d/ntp restart

  4. check that the date is correct.

    date
    Thu Jun 20 13:39:20 CEST 2013

In one of my servers the date was changed inmediately. In the other I had to wait a couple of minuts.

user1682960
  • 321
  • 2
  • 3
  • 2
    If you're in the UK (or another country) then you can just change the two letter country code to suit .e.g. `server 0.uk.pool.ntp.org iburst` – Pierz Apr 13 '15 at 12:05
  • Step 4 did not work for me until after about 15 minutes. Or perhaps because I ran `ntpq -p`. I am using: "Raspbian GNU/Linux 7 (wheezy)" and "Linux raspberrypi 4.1.7+ #817 PREEMPT Sat Sep 19 15:25:36 BST 2015 armv6l GNU/Linux" – Peter Mortensen Nov 26 '16 at 14:47
  • Perhaps [`hwclock` or `fake-hwclock` is interfering](http://raspberrypi.stackexchange.com/questions/266/how-can-i-keep-system-time/267#267) (but the "/etc/rc.conf" part is out of date) – Peter Mortensen Nov 26 '16 at 14:57
  • The `ntpq -p` part is from [an answer to *How is time kept on the Raspberry Pi?*](http://raspberrypi.stackexchange.com/questions/1601/how-is-time-kept-on-the-raspberry-pi/24697#24697). – Peter Mortensen Nov 26 '16 at 15:11
  • And after a 5 minute shutdown it was 5 minutes behind (`ntpq -p` did not make a difference). – Peter Mortensen Nov 26 '16 at 15:25
  • Forget the pool servers. Use time.nist.gov to get is from the horse's mouth: https://raspberrypi.stackexchange.com/questions/68811/how-do-i-set-raspbian-to-use-the-primary-time-server-time-nist-gov/68812 – SDsolar Jun 23 '17 at 07:23
18

If you have the fake-hwclock package, it will also do a timestamp saving trick to prevent the clock from being reset. That won't advance time while the device is off, so it will lag behind. The internal clock will also drift a tiny bit while on.

If you have an Internet connection (or lan with a server) and run ntp (ntpd/ntpdate), you can get the time over network, as other answers point out. When synchronized, it is really quite accurate. Online servers will also give you the consensus "world time".

XTL
  • 1,358
  • 9
  • 19
  • interesting about `fake-hwclock`. Why would someone want to freeze the clock when the machine is off? I can't think of a use case for that. Is there? – Scoop Jan 11 '13 at 22:44
  • 2
    @Scoop Because the alternative is forgetting the time, so it will reset. This will cause any recorded timestamps appear to be from the future etc. "Freezing" the clock makes sure time keeps moving forward, even if the count is off :) – XTL Jan 14 '13 at 08:03
  • 1
    Having ntp or other way to set the clock of course will still work and fake-hwclock only runs at boot and shutdown. – XTL Jan 14 '13 at 08:08
  • 1
    for `fake-hwclock` on Arch Linux, use http://archplusplus.co.uk/post/40202081414/fake-hwclock-for-arch-linux-arm-on-raspberry-pi-using – levinalex Apr 08 '13 at 12:19
  • 2
    This package now seems to be installed as default on Raspbian now. – Pierz Apr 14 '15 at 07:59
  • The fake-hwclock package is not installed by default in Ubuntu for Raspberry Pi. It's well worth installing it! Thanks to this answer, I have just installed it on to my Ubuntu-Pi's, makes a world of difference to reboots. Thank you David! – bazza Mar 30 '22 at 09:37
11

The Raspberry Pi doesn't have an integrated real time clock (RTC) for cost-effectiveness: see the question "Why is there no real time clock (RTC)?" in the official FAQ. It means that when you unplug the Raspberry Pi, it loses its time. When you power it back, the time is initialized to 1 January 1970 (aka Unix epoch).

The Raspberry Pi sets its time over the network with NTP, a protocol for clock synchronization between computers. This protocol is widely used over the Internet to make sure the computers have the same time and is highly reliable since some machines are dedicated to the time calculation with atomic clocks.

ntpd is a daemon (a software in background) launched a boot time, and it is responsible for synchronizing time using the NTP protocol. ntpd can be configured with the file /etc/ntp.conf. For instance, this is where you can set the servers to request the time.

Peter Mortensen
  • 1,984
  • 2
  • 14
  • 17
Morgan Courbet
  • 3,643
  • 3
  • 20
  • 37
  • Doesn't it remember the last time? I shutdown a Raspberry Pi a week ago and `date` now shows that week-old date plus, I think, some extra hours from the uptime yesterday and today (after some restarts yesterday and today). (Raspbian, corresponding to mid 2015). – Peter Mortensen Dec 11 '15 at 14:46
  • Run `cat /etc/fake-hwclock.data` and you will see the last time `fake-hwclock` saved the time (obviously, at shutdown time). `fake-hwclock` is used on the RPi to mimic a real clock. I think you booted your RPi without Internet access or without `ntpd` started to update the time. Can you confirm that? – Morgan Courbet Dec 11 '15 at 15:00
11

Raspbian Stretch: NTP not installed by default

It would appear that there was a bit of change-up with Raspbian Stretch. In short, ntp is no longer installed. Running dpkg -l | grep ntp returns nothing.

Apparently, Stretch abandoned the ntp application in favor of systemd. Check the man timesyncd entry for specifics on configuration settings. timesyncd still uses ntp servers to update the time. If you want to configure these servers manually, you can do so in the /etc/systemd/timesyncd.conf file.

b_laoshi
  • 351
  • 4
  • 9
  • 1
    After doing the edit, restart the service by `sudo systemctl restart timesyncd` and after that check the logs by `journalctl -u timesyncd -f` sometimes corporate firewalls blocks the port. In such cases, use the provided corporate ntp server URL. – truthadjustr Feb 05 '19 at 19:01
  • 1
    I believe the correct name of the daemon is `systemd-timesyncd`, so that the command to restart would be `systemctl restart systemd-timesyncd` – chb Aug 02 '19 at 16:49
3

If logging or keeping time when offline is what you're looking for, you may want to add a Real Time Clock chip, like the DS1307.

Marius Butuc
  • 925
  • 3
  • 11
  • 20
  • There's fairly reasonable RTC solutions like this to buy e.g. http://www.cjemicros.co.uk/micros/individual/newprodpages/prodinfo.php?prodcode=4D-RaspberryPi-RealTimeClock-RTC – Pierz Apr 13 '15 at 12:09