Cytomax
Posts: 3
Joined: Thu Sep 01, 2016 1:57 am

Fix for Incorrect Time on the Raspberry Pi

Thu Sep 01, 2016 2:09 am

Just wanted to post this in hopes i can save someone else the trouble and time trying figuring out WHY IN GODS NAME the time on my raspberry pi doesnt sync with NTP.

tl;dr

sudo apt-get install ntpdate
sudo service ntp stop
sudo ntpdate -u time.nist.gov
sudo service ntp start

"ntpd and ntpdate run by default using a restricted port (UDP 123). If you are behind a firewall, ntpd will never work, but ntpdate can work with the -u option. For example: ntpdate -u 0.ubuntu.pool.ntp.org or ntpdate -u time.nist.gov should both work fine."

Now i need to figure out to make this permanent

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

EVERYTHING I TRIED

I forcibly Set the Date to within 5 minutes of the correct time with the following command

date MMDDHHMMCCYY.SS
date 083120002016.00

I then tried the all the following to get my clock to sync and NONE of them worked...

++++++++++++++++++++

sudo raspi-config

++++++++++++++++++++

dpkg-reconfigure tzdata

+++++++++++++++++++++++++++++++

sudo nano /etc/fake-hwclock.data
(i input the date/time to within 5 minutes)

++++++++++++++++++++++++++++++
sudo nano /etc/ntp.conf

server 0.pool.ntp.org

++++++++++++++++++++++++

sudo /etc/init.d/ntp stop
sudo ntpd -gq
sudo /etc/init.d/ntp start

++++++++++++++++++++++++++++++++++

sudo nano /etc/ntpd.conf

driftfile /var/lib/ntp/ntp.drift
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 3.pool.ntp.org

+++++++++++++++++++++++++++++++++++++++

sudo service ntp stop
sudo ntpdate -s time.nist.gov
sudo service ntp start

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Fix for Incorrect Time on the Raspberry Pi

Thu Sep 01, 2016 7:11 am

Set up your /etc/ntp.conf like this

Code: Select all

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
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.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
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1
Make sure you've got a good network connection

ip addr
ip route
ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3 8.8.8.8
ping -c3 0.debian.pool.ntp.org


Stop & restart NTP
sudo /etc/init.d/ntp stop
sudo ntpd -q -g
sudo /etc/init.d/ntp start
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Cytomax
Posts: 3
Joined: Thu Sep 01, 2016 1:57 am

Re: Fix for Incorrect Time on the Raspberry Pi

Fri Sep 02, 2016 2:10 am

Thank you for the reply...

i just compared my /etc/ntp.conf file with your recommendations
apart from all the commented out stuff i really cant see anything different with the currents settings as is....

also what am i supposed to do with

ip addr
ip route
ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3 8.8.8.8
ping -c3 0.debian.pool.ntp.org

Stop & restart NTP
sudo /etc/init.d/ntp stop
sudo ntpd -q -g
sudo /etc/init.d/ntp start

that looks like i need to create a script or something with it?

Thank in Advance

User avatar
rpdom
Posts: 17171
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Fix for Incorrect Time on the Raspberry Pi

Fri Sep 02, 2016 5:58 am

Cytomax wrote:what am i supposed to do with

ip addr
ip route
ping -c3 $(ip route | awk '/default/ {print $3}')
ping -c3 8.8.8.8
ping -c3 0.debian.pool.ntp.org

Stop & restart NTP
sudo /etc/init.d/ntp stop
sudo ntpd -q -g
sudo /etc/init.d/ntp start

that looks like i need to create a script or something with it?
Start a Terminal. Type in the lines that were in blue text. Copy and paste the output in your next reply.
They will help diagnose any network configuration issues.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Fix for Incorrect Time on the Raspberry Pi

Fri Sep 02, 2016 8:24 am


Cytomax
Posts: 3
Joined: Thu Sep 01, 2016 1:57 am

Re: Fix for Incorrect Time on the Raspberry Pi

Sat Sep 03, 2016 1:26 am

Running the new Raspberry pi 3B using gigabit ethernet connection

sudo lsb_release -a

Code: Select all

No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 8.0 (jessie)
Release:        8.0
Codename:       jessie
sudo uname -a

Code: Select all

Linux raspberrypi 4.4.13-v7+ #894 SMP Mon Jun 13 13:13:27 BST 2016 armv7l GNU/Linux
Behind a Asus RT-n16 running shibby firmware

ip addr

Code: Select all

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:8d:03:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.3/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::82ff:a814:87e9:c8e5/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether b8:27:eb:d8:56:52 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::bb4a:c5c6:4847:aa06/64 scope link tentative
       valid_lft forever preferred_lft forever
ip route

Code: Select all

default via 192.168.0.1 dev eth0  metric 202
192.168.0.0/24 dev eth0  proto kernel  scope link  src 192.168.0.3  metric 202
sudo ping -c3 $(ip route | awk '/default/ {print $3}')

Code: Select all

PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.469 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.335 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=0.371 ms

--- 192.168.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.335/0.391/0.469/0.061 ms
sudo ping -c3 8.8.8.8

Code: Select all

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=49 time=18.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=49 time=18.3 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=49 time=18.1 ms

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 18.175/18.284/18.371/0.081 ms
sudo ping -c3 0.debian.pool.ntp.org

Code: Select all

PING 0.debian.pool.ntp.org (128.2.1.21) 56(84) bytes of data.
64 bytes from AC-NTP1.NET.CMU.EDU (128.2.1.21): icmp_seq=1 ttl=44 time=47.5 ms
64 bytes from AC-NTP1.NET.CMU.EDU (128.2.1.21): icmp_seq=2 ttl=44 time=47.7 ms
64 bytes from AC-NTP1.NET.CMU.EDU (128.2.1.21): icmp_seq=3 ttl=44 time=47.8 ms

--- 0.debian.pool.ntp.org ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 47.518/47.714/47.877/0.292 ms

sudo /etc/init.d/ntp stop

Code: Select all

[ ok ] Stopping ntp (via systemctl): ntp.service.

sudo ntpd -q -g
does nothing

sudo /etc/init.d/ntp start

Code: Select all

[ ok ] Starting ntp (via systemctl): ntp.service.

I also went to the link provided.... and performed the following steps..

As workaround, add the following command

Code: Select all

/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 the sync state of NTP daemon as follows

ntpq -cpeers

Code: Select all

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 i5.smatwebdesig .INIT.          16 u    -   64    0    0.000    0.000   0.000
 104.131.53.252  .INIT.          16 u    -   64    0    0.000    0.000   0.000
 mail.coldnortha .INIT.          16 u    -   64    0    0.000    0.000   0.000
 host2.kingrst.c .INIT.          16 u    -   64    0    0.000    0.000   0.000


Since im on ethernet and not wifi i even changed the wlan0 to eth0

Code: Select all

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

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Fix for Incorrect Time on the Raspberry Pi

Sat Sep 03, 2016 10:31 am

What is that iptables stuff trying to do?

Your system needs to be able to connect to the remote time servers. Simply open all outbound traffic on UDP port 123 so you can connect to anywhere and hope that your upstream router(s) don't stupidly block that traffic.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: Fix for Incorrect Time on the Raspberry Pi

Sat Sep 03, 2016 9:38 pm

I think it would be better not to play with iptables when you not sure what are you doing. EDIT: I read the thread about TOS, so it`s a workaround. My Pi 3 could sync, I`m using ethernet.

The NTP need some time to sync with remote server. So when you boot up your Pi, it won't have the time immediately, you need to wait for some moments. If you wish to get a time as soon as possible, then you need a RTC.

To tell if the Pi is finished sync process, run ntpq -p -n.
It would output something like these:

Code: Select all

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 115.47.58.63    .STEP.          16 u    - 1024    0    0.000    0.000   0.000
 129.250.35.250  249.224.99.213   2 u  35m  128    0  175.183   36.970   0.000
*202.65.114.202  218.100.41.254   2 u   10  256  371  243.884  -47.913   6.822
See that asterisk before the third line? It means Pi is finished sync with this server.

Return to “General discussion”