Page 1 of 1

Setting the correct time

Posted: Mon Jul 15, 2013 2:18 pm
by marian42
Hi,
i want to set the correct time on my Pi for a datalogging project. It has a permanent internet connection.

I tried ntpd, tcselect, putting my timezone in /home/pi/.config and rebooting. It is always exactly 10 minutes and some hours too late.
It is 16:16 and the Pi displays 13:26. Errors less than a few minutes are no problem for me.

How can I make the Pi get the correct time on startup?

Re: Setting the correct time

Posted: Mon Jul 15, 2013 2:30 pm
by RaTTuS
set your timezone
sudo raspi-config
as to the weird 10 mins or so ...
are you sure your other sources are correct
the clock should auto correct when it's connected to the internet

Re: Setting the correct time

Posted: Mon Jul 15, 2013 5:44 pm
by marian42
Okay so I set the timezone and it is now closer to the actual time.
The date command says that it's 18:53 CEST while the actual CEST is 18:43.

Re: Setting the correct time

Posted: Tue Jul 16, 2013 6:31 pm
by crazyskeggy
What distribution are you using? I know Raspbian get's it's time from the internet, and all others should, too.

Re: Setting the correct time

Posted: Tue Jul 16, 2013 6:38 pm
by marian42
I'm using Raspian.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 7:24 am
by crazyskeggy
Check your internet connection - if anybody knows how to manually update NTP (time server), then it would be useful to know.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 8:21 am
by rpdom
crazyskeggy wrote:Check your internet connection - if anybody knows how to manually update NTP (time server), then it would be useful to know.
You can check that NTP is set up correctly with ntpq -p

Code: Select all

pi@raspi3 ~ $ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*myntpserver     94.23.228.130    3 u   61   64    1    0.509   -1.523   1.447
That is showing that my pi is synchronised to "myntpserver" which got its time from 94.23.228.130. The last update was 61 seconds ago, it checks every 64 seconds. The server takes about 0.509ms to reply, is 1.523ms different to the current clock and varies by 1.447ms.

The list of servers to use is held in /etc/ntp.conf. It is usually set to a number of server names, each of which redirects to a set of servers in a round-robin list.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 9:53 am
by dspolleke
The way NTP works is that it does not make the clock jump to the correct time but speeds up the cpu / hardware clock or slows it down. If i recall correctly the pi does not have a built in "clock" device and you need ntp to adjust the time constantly...

if you would like to set te time at startup FIRST configure the ntp deamon in /etc/ntpd.conf
make sure you have a reachable NTP server
let's say nl.pool.ntp.org (for i live in the Netherlands) but if you set up a local ntp server that is even better..
make sure ntp allows the ntp server to help you out... (adjust to match)
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

next write a small script like this

Code: Select all

#!/bin/bash
TIMESERVER=nl.pool.ntp.org
/etc/init.d/ntp stop
ntpdate $TIMESERVER
/etc/init.d/ntp start

Have it started via or just add the above to /etc/rc.local
more reading about NTP
http://www.ntp.org/ntpfaq/NTP-s-def.htm

Re: Setting the correct time

Posted: Wed Jul 17, 2013 10:32 am
by rpdom
dspolleke wrote:The way NTP works is that it does not make the clock jump to the correct time but speeds up the cpu / hardware clock or slows it down. If i recall correctly the pi does not have a built in "clock" device and you need ntp to adjust the time constantly...

if you would like to set te time at startup FIRST configure the ntp deamon in /etc/ntpd.conf
make sure you have a reachable NTP server
let's say nl.pool.ntp.org (for i live in the Netherlands) but if you set up a local ntp server that is even better..
make sure ntp allows the ntp server to help you out... (adjust to match)
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

next write a small script like this

Code: Select all

#!/bin/bash
TIMESERVER=nl.pool.ntp.org
/etc/init.d/ntp stop
ntpdate $TIMESERVER
/etc/init.d/ntp start
Actually ntpdate is deprecated and shouldn't really be used any more. ntpd can provide everything that ntpdate did, and more.

ntpd is given the "-g" flag at start-up, which means it is allowed to make set the time directly, just once, if it is over 1000 seconds out. This replaces the need for ntpdate.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 1:33 pm
by marian42
ntpq -p returns this:

Code: Select all

pi@raspberrypi ~ $ ntpq -p
ntpq: read: Connection refused
But the Pi does have a working internet connection. I just tested it with the browser.

There is no /etc/ntp.conf but a ntpd.conf. I made a copy of it so now there are both. But the ntpq -p command still has no success.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 3:00 pm
by rpdom
marian42 wrote:ntpq -p returns this:

Code: Select all

pi@raspberrypi ~ $ ntpq -p
ntpq: read: Connection refused
But the Pi does have a working internet connection. I just tested it with the browser.

There is no /etc/ntp.conf but a ntpd.conf. I made a copy of it so now there are both. But the ntpq -p command still has no success.
Ok, important question. Are you running a Raspbian system, or one of the other Linux distros?

Re: Setting the correct time

Posted: Wed Jul 17, 2013 3:28 pm
by marian42
I'm running Raspbian.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 3:47 pm
by rpdom
marian42 wrote:I'm running Raspbian.
Very odd then. You should have an ntp.conf file and it should "just work".

Re: Setting the correct time

Posted: Wed Jul 17, 2013 4:30 pm
by RaTTuS
either start again from a blank image
or try and uninstall ntp then reinstall it...
as /etc/ntp.conf should exist

Re: Setting the correct time

Posted: Wed Jul 17, 2013 5:35 pm
by rpdom
RaTTuS wrote:try and uninstall ntp then reinstall it...
as /etc/ntp.conf should exist

Code: Select all

sudo apt-get purge ntp
sudo apt-get update
sudo apt-get install ntp
The "purge" option will make sure that all traces of ntp are removed, including any configuration files. With the usual "remove" option, they are left behind so they can be used for any new version that gets installed.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 7:34 pm
by marian42
I still get a connection refused error.
My raspberry pi is only two weeks old so the Raspbian image should be up to date.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 8:12 pm
by FTrevorGowen
Like rpdom I've never had to do "anything special" for ntp to work for raspbian so, FWIW (and for reference) here's the contents of /etc/ntp.conf ...

Code: Select all

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

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


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst


# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrict
ions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

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

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
and ntpq - p reports ...

Code: Select all

pi@raspigrey ~ $ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*diane.ensma.fr  131.188.3.221    2 u   19   64  377   34.773    0.922   5.370
+callisto.mysnip 130.149.17.8     2 u   13   64  377   54.417    4.743   4.532
+notary1.converg 192.53.103.104   2 u   57   64  377   52.559   -8.367   5.808
-rad.22pf.org    130.88.200.6     3 u   56   64   37   29.582   11.393   6.060
Exactly where are you? (O.K. I should be able to find the CEST timzone :-) but ...) just in case there's an issue with ntp servers "there". The only other thing that comes to mind is the possibility that your router's firewall (or other settings) is blocking ntp access (or something like that).
Trev

Re: Setting the correct time

Posted: Wed Jul 17, 2013 8:24 pm
by marian42
My ntp.conf looks just the same. I live in Germany which has only one timezone, the CEST.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 8:36 pm
by FTrevorGowen
marian42 wrote:My ntp.conf looks just the same. I live in Germany which has only one timezone, the CEST.
It's too hot here :-), I should have recognised Central European Standard Time of course :oops:
Since my Pi(s) is (are) picking up an ntp server in FRance and I know Debian is well supported in Germany it's looking even more like a router/firewall issue. Unless your internet access if via a proxy server? (Which also could be blocking access/requests).
Sorry I can't be of more help.
Trev.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 11:16 pm
by drgeoff
marian42 wrote:My raspberry pi is only two weeks old so the Raspbian image should be up to date.
Is that a clue that your SD card came pre-written with your RPi? If yes, I'm afraid it is no guarantee at all that the OS is up to date. On the other hand, if you flashed it yourself you will know which version of Raspbian wheezy you downloaded.

Re: Setting the correct time

Posted: Wed Jul 17, 2013 11:29 pm
by marian42
I downloaded the image myself via bittorrent from the official site.
The first line in ssh says: Linux raspberrypi 3.6.11+ #474 PREEMPT Thu Jun 13 17:14:42 BST 2013 armv61

Is there a way to check if my router blocks the ntp connection?