System Time

To see what time it is type:

date

And to set it:

date<MMDDhhmmYYYY> where the syntax follows (Month, Day, hour, minute and Year).

Time is vital for handling files since versions are often determined on time. Having the systems clock running behind actual time will be very problematic since files would appear as created in the future. Warnings as the following appear:

make[1]: warning: Clock skew detected. Your build may be incomplete.

make[1]: Warning: File `scripts/kconfig/.zconf.tab.o.cmd' has modification time 4.1e+03 s in the future

UTC

UTC is not a timezone.

To allow working globally, the following sequence could occur: Saving a file in Europe and then another file gets saved some seconds later in New York. Even the clock in New York is hours behind, it should be made sure that everybody on the globe sees that the file in New York got saved after the one in Europe.

Linux uses for this UTC (Universal Time Coordinated). GMT is equal to UTC except when daylight saving time is active. So all Computers save time in UTC on their hard disks. When showing the date on screen, the computer takes UTC saving time of on the hard disk, knowing the local computers timezone and daylight saving time, it can calculate the date to be shown.

A question is what time shows the local real time clock on the motherboard.

On Linux computer this should be set to UTC.

However Microsoft Windows assumes that the real time clock on the motherboard shows local time.

If running Windows and Linux on the same computer and the clock jumps back an forward one hour then make sure Linux uses local time instead of UTC by setting

clock="local"

in /etc/conf.d/hwclock.otherwise leave it to

clock="UTC"

Set also the timezone of the computer by something as:

cp /usr/share/zoneinfo/GMT /etc/localtime or create an absolute or relative symbolic link ln -s /usr/share/zoneinfo/GMT /etc/localtime

The symlink shows what timezone selected, zdump /etc/localtime or strings /etc/localtime show it also on a copied file.

NTP

NTP synchronizes the UTC time via Internet from a time server as https://time.is/UTC using UDP port 123.

Get the ntp packet

ntpdate is a ntp client that sets the time. Reading man ntpdate shows a disclaimer telling that it is easy to be used but lacks of maintenance.

The pc could also act as a ntp server using the ntpd the ntp daemon. It actually acts as client as well to get the time in the first place.

There is also sntp the simple network time protocol client.

The term stratum is used to show how directly the time is determined.

  1. Stratum 1 are the most accurate network time servers (they use stratum 0 reference clocks)

  2. Stratum 2 are time servers synchronized from stratum 1 network servers

  3. Stratum 3 are ntp clients, they can act as stratum 4 time servers and this can repeat until stratum 15. Stratum 16 means not synchronized at all.

To configure use the pool of ntp time servers.

http://www.pool.ntp.org/

As when being in Switzerland:

server <n>.ch.pool.ntp.org
server <n>.europe.pool.ntp.org

<n> can be omitted or be a number of 0,1,2,3. It defines a random set of time servers. Often 4 lines are added

server 0.ch.pool.ntp.org
server 1.ch.pool.ntp.org
server 2.ch.pool.ntp.org
server 3.ch.pool.ntp.org

Using DNS IP addresses of pool ntp time servers are assigned

Verify how close the time servers are:

install traceroute

traceroute ch.pool.ntp.org

or get tracepath (for gentoo USE="tracepath" emerge -1 iputils )

tracepath ch.pool.ntp.org

ntpdate

Edit /etc/conf.d/ntp-client to point to something close to the computer

NTPCLIENT_CMD="ntpdate"
NTPCLIENT_OPTS=" -b -u \
               0.ch.pool.ntp.org \
               1.ch.pool.ntp.org \
               2.ch.pool.ntp.org \
               3.ch.pool.ntp.org"

To set your time at boot run

rc-update add ntp-client default

Now start it:

/etc/init.d/ntp-client start

ntpd

ntpd is usually used together with ntpdate. ntpdate is used first to set the time roughly to some minutes precision. Then ntpd takes over for the synchronization. ntpd gets more accurate time than ntpdate since it synchronizes the kernel time and not just sets the time.

rc-update add ntpd default

Now start it:

/etc/init.d/ntpd start

To test it use ntpq the ntp query program. The -c option runs a command and exits

Read the variables using the rv command

ntpq -c rv

Print list of peers

ntpq -p -w one of the peers should start with tally code character * indication that time comes from this server (other codes + is a candidate, space unreachable)

ntpq -c sysinfo or ntpq -c sysinfo | grep stratum to see just the stratum

There are the config file /etc/ntp.conf file and the openRC file /etc/conf.d/ntpd.

Alternative time source

The standard way for ntp is taking time from a time server on the Internet. If none is available ntp offers to get time from other sources.

This means that a ntp server will be created and other devices might become connected as ntp clients. If this is the case, then it could happen that the alternative time source fails. In this case it is advisable to add the following lines to /etc/ntp.conf to switch to the local internal Linux time (coming at boot from the RTC of the motherboard).

server 127.127.1.0                      
fudge 127.127.1.0 stratum 7             
ntp and gps

Gps satellites send time down to earth. Usually they use NMEA sentences on a serial port to the PC. There might also a PPS pulse on DCD (Data Carrier Detect pin1 of a sub D9 RS232 connector) that gives a precise clock signal.

Gps receiver must receive enough satellites to lock position and use the time information. Gps suited well where the antenna can be placed to open sky.

Set the correct baudrate stty -F /dev/ttyUSB0 38400

verify it stty -F /dev/ttyUSB0

To see what comes from the GPS receiver cat /dev/ttyUSB0

Ctrl + Z, ps and kill -9 <n> to stop the cat command

When having gpsd installed gpscat -s 38400N1 /dev/ttyUSB0 other alternatives are programs as minicom can also be used to test it.

In ntp there are different drivers that allow to use gps:

generic nmea and ntp

See http://0x9900.com/build-you-own-stratum-1-ntp-server-for-less-than-100/

This is enabled by adding

server 127.127.20.0 mode <x> prefer
fudge 127.127.20.0 flag1 1 flag2 0

as server.

The 127.127 means not using Internet and use a clock driver instead.

20 is the clock driver number 20 assigned to the Generic NMEA GPS Receiver see https://www.eecis.udel.edu/~mills/ntp/html/drivers/driver20.html

0 is /dev/gps0 where the receiver will be read from

<x> is a bit map defining the details of this clock driver as:

$GPRMC is a NMEA sentence that contains both UTC and day/month/year => 0x01

38400 baud => 0x30

<x> is therefore 49 (0x01 plus 0x30)

If everything works fine ntpq -p shows a * in front of GPS

*GPS_NMEA(0) .GPS.

and ntpq -c rv shows stratum=1

ntpq -c clockvar shows the last NMEA sentence received

ntpq -c sysinfo gives a more human friendly view

ntp and gpsd

To use gps data simultaneously in different applications, gpsd the gps daemon can be installed. This removes also the burden to have ntp dealing with the gps hardware. The communication between gpsd and ntpd is done via shared memory. Problematic might be permissions for ntpd to access the shared memory. ps -eF | grep ntpd shows how ntpd is running

and start gpsd /dev/ttyUSB0

test cgps

add to /etc/ntp.conf

# GPS Serial data reference
server 127.127.28.0 minpoll 4 maxpoll 4
fudge 127.127.28.0 time1 0.0 refid GPS

# GPS PPS reference
server 127.127.28.1 minpoll 4 maxpoll 4 prefer
fudge 127.127.28.1 refid PPS

restart ntpd /etc/init.d ntpd restart

gpsd comes with ntpshmmon a tool to monitor the shared memory.

check ntpq -p and ntpq -c rv

ntpq -p 127.127.28.0 check if access to shared memory works

ipcs -m

ps aux | grep ntp

ntpq -c clockvar shows the last GPS sentence

DCF77

DCF77 is a long wave 77.5kHz time transmitter in Germany https://www.ptb.de/cms/en/ptb/fachabteilungen/abt4/fb-44/ag-442/dissemination-of-legal-time/dcf77.html that can be received all over Europe and is used by many watches. It sends pulses once a second and has date and time coded by different pulse lengths (100ms and 200ms). It takes longer than 1min to decode time and date.

Since it is simple, some concerns exist of being too open for cyber attacks sending intentionally wrong time. This might be easily possible on a very short distance but for long distances an antenna big as the one in Germany covering a couple of football fields would be required.

A simply DCF77 receiver is connected directly to the RX line of a serial port of a PC. This is a trick to not require to poll the signal level of the DCF77 receiver. If a pulse arrives the UART interprets it as start bit and samples the following 8 bits. With the correct baud-rate the 100ms pulse returns to hight during those 8bits and therefore different characters get received for the short 100ms pulse and the long 200ms pulse.

Check out https://www.google.de/maps/place/Mainflingen,+Mainhausen/@50.3750485,9.3278463,7z/data=!4m2!3m1!1s0x47bd3f059ce2631b:0xa224352a99a4560 point with your smart phone there and have the ferrite antenna 90° horizontal to it. See http://blog.debuglevel.de/raspberry-pi-und-dcf77-empfaenger-von-conrad/

Generic Reference Driver number 8 is used for that.

RTC chip on the motherboard

As seen in /etc/conf.d/hwclock the Linux time is saved to the motherboards rtc chip at shutdown.

clock_systohc = "YES"

Assuming the kernel has been compiled with the appropriate real time clock device (RTC) driver.

Gateways and Routers

Gateways have often a clock inside and can make use of ntp as protocol (RFC1305) and type in a sever name as ch.pool.ntp.org.


Linurs Hosttech startpage