question

How to get the Raspberry Pi to always have the correct time?

RP boot screen
  1. Found 192.168.1.70 ip:192.168.1.70 (Time server no: 1 of 6) My local timserver
  2. Date and time set to 2015-07-16 15:24:17 Sets the time
  3. Changed 1437053026.927226 seconds Difference from the "Hardware clock", that not exists.
  4. Starting ntpd Starting the Network Time Protocol daemon, to maintains the system time in synchronization with time servers using the Network Time Protocol (NTP)

To accomplish this

Edit the file '/etc/ntp.conf', In my file that you can download it says something like this:

# You have a number of time servers:

server pool.ntp.org
server ntp2.sp.se
server ntp1.chalmers.se
server ntp1.sp.se
server ntp2.gbg.netnod.se

# If no server is found (Internet connection discarded) synchronizes it against itself!

server 127.0.0.1

driftfile /etc/ntp/drift
logfile /var/log/ntpd.log

# 'restrict 127.0.0.1 mask 255.0.0.0' provide your own computer with unlimited access
# 'restrict 192.168.12.32 mask 255.255.255.224' If you have a local network, and want other computers to be able to set their clock for your computer, do the following. A good idea to reduce unnecessary Internet traffic. On the other computers, just enter the address of your computer in their 'ntpd.conf' file. You can also sync your Windows XP. NOTE! Change the network / mask to match with yours..
# There are lots of flags, 'nomodefy' 'nomodefy' means that the other machines in the network can not change your ntpd server. 'notrap' means they can not use your server for event logging (can mean big load) 'Code' means that the server tries to kill enemy attacks There is a lot of information on the Internet, for example:
# http://www.ece.udel.edu/~mills/ntp/html/accopt.html

restrict 127.0.0.1 mask 255.0.0.0

restrict 192.168.21.32 mask 255.255.255.224 nomodify notrap kod

In 'ntpd.conf' we referred to two files, now we have to create them, you do, for example, with the command
$>touch /etc/ntp/drift

As well as a log file:
$>touch /var/log/ntpd.log

And you need this log fil
$>touch /var/log/ntpusers.log

Download: ntp.conf.tar.gz

Place the ntp.conf in /etc


You can use my script

# settime
# Version 0.0.2
# Release date: 2006-11-10
# Updated: 2007-01-09
# Updated: 2015-07-16
# Copyright 2006-2015, Ingemar Ceicer.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# Add timehosts (2 or 3 would be sufficient). Select timehosts near your place.
# Here is the only place you need to change, add time servers according to the pattern you see below.
# Select any time-servers who are near your computer. Two or three is good enough
# time servers are almost never down.

TIMEHOST[0]="pool.ntp.org"
TIMEHOST[1]="ntp1.chalmers.se"
TIMEHOST[2]="ntp1.sp.se"
TIMEHOST[3]="ntp1.gbg.netnod.se"
TIMEHOST[4]="ntp2.gbg.netnod.se"

#++++++++++++++++++ Do not edit under this line ++++++++++++++++++#


i=1
if [ -x /usr/sbin/ntpdate ]; then
for s in ${TIMEHOST[@]}; do
ip_ofset=$(echo `/usr/sbin/ntpdate -b $s 2>/dev/null` | awk '{print $8 " " $10}')
ip=$(echo $ip_ofset | awk '{print $1}')
if [[ $ip && $ip != "" ]]; then
ofset=$(echo $ip_ofset | awk '{print $2}')
echo "Found "$s" ip: "$ip" (Timeserver no: "$i" of" `echo ${TIMEHOST[@]} | wc -w`")"
echo "Date and time set to "`date +%F` `date +%H:%M:%S`
echo "Changed: "$ofset" seconds"
if [ -x /usr/sbin/ntpd ]; then
echo "Starting ntpd"
/usr/sbin/ntpd
else
echo "Can't find /usr/sbin/ntpd!!"
fi
exit
fi
let "i=$i+1"
done
else
echo "Can't find /usr/sbin/ntpdate!!"
fi
echo "Can't find any timeservers, date and time could not be set"

Download: settime.tar.gz

Place the script (settime) in /usr/local/bin
Make it executable chmod +x /usr/local/bin/settime


To start the script when your Raspberry Pi 2 start

Edit /etc/rc.d/rc.local and add this to the file:

if [ -x /usr/local/bin/settime ]; then
   /usr/local/bin/settime
fi



Logrotate

The log files grows and grows ... A good way to get reliable log files from the ntpd is to let the script 'logrotate' take care of the log files so that they do not grow uncontrollably. By default Slackware logrotate every day at 04:40. Look in the folder '/etc/cron.daily'. There you will find the script 'logrotate'

What can there be in the configuration file?

An example:

/var/log/ntpd.log {
# Here we determine local, for ntpd.log specific rules.
daily # daily
rotate 7 # 7 files saved (ntpd.log.1, ntpd.log., 2 ...)
# 7 daily rotations means erasing. Change if you want!
notifempty # Does not rotate if the file is empty.
# ntpd can not close its log file, so the contents of the Old file is moved to a new one.
# Therefore, "copytruncate" may mean that something is lost.
copytruncate
}

/var/log/ntpusers.log {
daily
rotate 7
ifempty # Even if it is empty.
postrotate # Between "postrotate" and "endscript" says things that should be done after a
# rotation has taken place.
# Here we use the program 'sed' to sort all the "ntpusers", ie the servers
# you used and the clients that may have used your computer's clock
# to set the time. They are sorted according to the number of accesses they had.
ntpdc -c monlist \
| sed -n '3,${p;d;};w /var/log/ntpusers.log' \
| sort -k4nr >> /var/log/ntpusers.log
endscript
}

NOTE! Do not use the comments and beware of the tab and the space. logrotate is fussy!

Download: ntp.tar.gz

Place the file ntp in /etc/logrotate.d


If you are using DHCP

You must edit the file '/etc/rc.d/inet1.conf'

# Config information for eth0:
IFNAME[0]="eth0"
IPADDR[0]=""
NETMASK[0]=""
USE_DHCP[0]="yes"

# To prevent the file 'ntp.conf' to be overwritten on reboot
DHCP_KEEPNTP[0]="yes"

DHCP_HOSTNAME[0]=””


Do not use the built-in script to start ntpd

Slackware services
If you use my script, do not use the built-in script to start ntpd.


To check out how it works

The command ntpdate -d timeserver
(ntpdate -d pool.ntp.org)

Test your computer's time against a time server without reconfiguring the computer's clock.


ntpq - standard NTP query program

Thanks to http://slackpi.linicks.net/

If you run the command
ntpq -p
on your client computer, you get a list of time servers that your client PC sync against.

If you run the command
ntpq -c mru
mru, "most recently used", You get a list of hosts that contacted you on port 123. The port for Network Time Protocol (NTP) - used for time synchronization.



We welcome feedback and bug reports
epost
I gratefully accept any comments!