Powered By GitBook
108.1. Maintain system time

108.1 Maintain system time

Weight: 3
Description: Candidates should be able to properly maintain the system time and synchronize the clock via NTP.
Key Knowledge Areas:
    Set the system date and time
    Set the hardware clock to the correct time in UTC
    Configure the correct timezone
    Basic NTP configuration
    Knowledge of using the pool.ntp.org service
    Awareness of the ntpq command
Terms and Utilities:
    /usr/share/zoneinfo/
    /etc/timezone
    /etc/localtime
    /etc/ntp.conf
    date
    hwclock
    ntpd
    ntpdate
    pool.ntp.org
When we install a Linux® system graphically, we set the clock and choose a time zone suitable for our needs ,we can also choose to use the Network Time Protocol (NTP) to set your clock automatically. In this lesson we show how to go below the graphical interfaces and configure the various time-related aspects of Linux system.

How Linux keep tack of time

there are 2 clocks in each computer. The first is the Hardware Clock. This is the clock on a motherboard chip that keeps time even when the machine is powered off.
The other clock is the virtual System Clock. Linux asks the Hardware Clock chip what time it is on power up and then keeps track of the time itself with software.
Hardware clock can be the localtime (your computers timezone) or UTC time (standard time).
We can determine which one is set by checking /etc/adjtime . This file is empty unless the Hardware has been set manually.
Usually the hardware clock is set on UTC , so when ever system boots up, Software clock reads Hardware clock and then calculates the difference based on our timezone.

Setting The System Clock

date

date command is used to display the system date and time. By default the date command displays the date in local time, even if your hardware clock keeps UTC. Use the -u option to display UTC.
2
Mon Feb 17 19:17:52 +0330 2020
Copied!
Use the -u option to display UTC:
1
2
Mon Feb 17 15:50:20 UTC 2020
Copied!
date command is also used to set date and time of the system ( Automatic Adjustment (ntp) should not be enabled, otherwise it won't work):
2
Mon Feb 17 19:37:30 +0330 2020
3
[email protected]:~# date --set="20170413 21:14:00"
4
Thu Apr 13 21:14:00 +0430 2017
6
Thu Apr 13 21:14:02 +0430 2017
Copied!
Although we can set time using date command, the big problem with this idea is that time change will only last until the next reboot. Unless we somehow set the system time to the hardware clock.

Setting The Hardware Clock

hwclock

To change the Hardware Clock, you can use the motherboard’s BIOS utility at startup, but if you miss that opportunity, there is still hope. The "hwclock" command.
hwclock is a utility for accessing the hardware clock, also referred to as the Real Time Clock (RTC). As we mentioned it is independent of the operating system you use and works even when the machine is shut down.
1
2
Mon 17 Feb 2020 08:53:26 PM +0330 .972851 seconds
4
Mon Feb 17 20:53:33 +0330 2020
Copied!
hwclock date shows the date in the localtime (time after adding the timezone to the UTC time) , even when the hardware clock is set on UTC!
hwclock syntax : hwclock [function] [option...] where :
1
### Functioms:
2
-r, --show read hardware clock and print result
3
--get read hardware clock and print drift corrected result
4
--set set the RTC to the time given with --date
5
-s, --hctosys set the system time from the hardware clock
6
-w, --systohc set the hardware clock from the current system time
7
--systz set the system time based on the current timezone
8
### Options:
9
--localtime the hardware clock is kept local time
10
-u, --utc the hardware clock is kept in UTC
Copied!
lets do some examples:
1
### lets set hardware clock in utc
2
[email protected]:~# hwclock --set --date="01/02/1999 10:20:30"
3
4
Sat 02 Jan 1999 10:20:34 AM +0330 .274999 seconds
5
[email protected]:~# cat /etc/adjtime
6
0.000000 915259830 0.000000
7
915259830
8
UTC
9
[email protected]:~# hwclock
10
Sat 02 Jan 1999 10:21:48 AM +0330 .259334 seconds
12
Mon Feb 17 21:36:41 +0330 2020
13
14
15
### lets fix it by setting local time to hardware clock
16
17
[email protected]:~# hwclock
18
Sat 02 Jan 1999 10:31:10 AM +0330 .009381 seconds
20
Mon Feb 17 21:39:56 +0330 2020
21
[email protected]:~# hwclock --localtime --set --date="02/17/2020 21:40:00"
22
[email protected]:~# cat /etc/adjtime
23
0.000000 1581963000 0.000000
24
1581963000
25
LOCAL
26
[email protected]:~# hwclock
27
Mon 17 Feb 2020 09:40:08 PM +0330 .290810 seconds
29
Mon Feb 17 21:40:55 +0330 2020
30
Copied!
the hwclock --localtime -w would do the same thing , but setting hardware clock to your local time is not a good idea, so use hwclock -u -w instead.

NTP

Network Time Protocol (NTP) is an application layer protocol used for clock synchronization between hosts on a TCP/IP network. The goal of NTP is to ensure that all computers on a network agree on the time, since even a small difference can create problems.
NTP uses a hierarchical system of time sources. At the top of the structure are highly accurate time sources – typically atomic or GPS clocks. These clocks are known as stratum 0 servers. Stratum 1 servers are directly linked to stratum 0 servers and computers run NTP servers that deliver the time to stratum 2 servers, and so on (image source: Wikipedia):
NTP uses a client-server architecture; one host is configured as the NTP server and all other hosts on the network are configured as NTP clients.

pool.ntp.org

The pool.ntp.org project is a big virtual cluster of timeservers providing reliable easy to use NTP service for millions of clients.
The pool is being used by hundreds of millions of systems around the world. It's the default "time server" for most of the major Linux distributions and many networked appliances

ntpdate

ntpdate sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the server arguments to determine the correct time. It must be run as root on the local host. (you might need to install it). -v : verbose
1
[email protected]:~# ntpdate -v pool.ntp.org
2
17 Feb 22:59:34 ntpdate[4365]: ntpdate [email protected] Tue Jan 7 15:08:24 UTC 2020 (1)
3
17 Feb 22:59:46 ntpdate[4365]: adjust time server 194.225.150.25 offset -0.005153 sec
Copied!
After this, we need to set the hwclock to the just corrected system time by sudo hwclock -w or hwclock -u -w to make sure you are setting that on utc .
-q switch will query for time and just show the result with out setting that.

ntpd

Instead of manually setting the time each time, we can use a linux service called ntp. The ntpd utility is an operating system daemon which sets and maintains the system time of day in synchronism with Internet standard time servers.
1
[email protected]:~# apt install ntp
2
[email protected]:~# systemctl start ntp
3
[email protected]:~# systemctl enable ntp
Copied!
Fun fact: we can not use natpdate while ntp service is running:
1
[email protected]:~# ntpdate pool.ntp.org
2
17 Feb 23:15:34 ntpdate[5740]: the NTP socket is in use, exiting
Copied!

/etc/ntp.cpnf

The ntpd configuration file is located at /etc/ntp.conf. It is read at initial startup by the ntpd daemon in order to specify the appropriate synchronization sources:
1
[email protected]:~# cat /etc/ntp.conf
2
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
3
4
driftfile /var/lib/ntp/ntp.drift
5
6
# Enable this if you want statistics to be logged.
7
#statsdir /var/log/ntpstats/
8
9
statistics loopstats peerstats clockstats
10
filegen loopstats file loopstats type day enable
11
filegen peerstats file peerstats type day enable
12
filegen clockstats file clockstats type day enable
13
14
# Specify one or more NTP servers.
15
16
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
17
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
18
# more information.
19
pool 0.ubuntu.pool.ntp.org iburst
20
pool 1.ubuntu.pool.ntp.org iburst
21
pool 2.ubuntu.pool.ntp.org iburst
22
pool 3.ubuntu.pool.ntp.org iburst
23
24
# Use Ubuntu's ntp server as a fallback.
25
pool ntp.ubuntu.com
26
27
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
28
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
29
# might also be helpful.
30
#
31
# Note that "restrict" applies to both servers and clients, so a configuration
32
# that might be intended to block requests from certain clients could also end
33
# up blocking replies from your own upstream servers.
34
35
# By default, exchange time with everybody, but don't allow configuration.
36
restrict -4 default kod notrap nomodify nopeer noquery limited
37
restrict -6 default kod notrap nomodify nopeer noquery limited
38
39
# Local users may interrogate the ntp server more closely.
40
restrict 127.0.0.1
41
restrict ::1
42
43
# Needed for adding pool entries
44
restrict source notrap nomodify noquery
45
46
# Clients from this (example!) subnet have unlimited access, but only if
47
# cryptographically authenticated.
48
#restrict 192.168.123.0 mask 255.255.255.0 notrust
49
50
51
# If you want to provide time to your local subnet, change the next line.
52
# (Again, the address is an example only.)
53
#broadcast 192.168.123.255
54
55
# If you want to listen to time broadcasts on your local subnet, de-comment the
56
# next lines. Please do this only if you trust everybody on the network!
57
#disable auth
58
#broadcastclient
59
60
#Changes recquired to use pps synchonisation as explained in documentation:
61
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918
62
63
#server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS
64
#fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware
65
66
#server 127.127.22.1 # ATOM(PPS)
67
#fudge 127.127.22.1 flag3 1 # enable PPS API
Copied!
You can change the ntp servers to the ntp server(s) you want. Do not forget to restart the service after any modifications.

ntpq

The ntpq utility program is used to monitor NTP daemon ntpd operations and determine performance.
-p :Print a list of the peers known to the server as well as a summary of their state.
1
2
remote refid st t when poll reach delay offset jitter
3
==============================================================================
4
0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
5
1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
6
2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
7
3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
8
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
9
-77.104.104.100 37.156.28.13 3 u 20 64 345 5.641 -40.539 13.503
10
+77.104.70.70 194.225.150.25 3 u 25 64 377 4.835 11.343 7.680
11
-5-144-132-2.sta 119.28.183.184 3 u 28 64 377 4.803 -7.711 14.597
12
+ntp5.mobinnet.n 62.12.173.12 2 u 46 64 377 4.920 -11.823 6.280
13
*golem.canonical 17.253.34.251 2 u 59 64 377 85.408 11.230 8.712
Copied!
-n : Output all host addresses in dotted-quad numeric format rather than converting to the canonical host names.
1
[email protected]:~# ntpq -pn
2
remote refid st t when poll reach delay offset jitter
3
==============================================================================
4
0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
5
1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
6
2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
7
3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000
8
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
9
-77.104.104.100 37.156.28.13 3 u 8 64 313 4.862 -20.085 13.553
10
+77.104.70.70 194.225.150.25 3 u 8 64 377 4.835 11.343 6.174
11
-5.144.132.2 119.28.183.184 3 u 6 64 377 5.062 3.365 5.087
12
+37.156.28.13 62.12.173.12 2 u 13 64 377 4.920 -11.823 4.775
13
*91.189.89.199 17.253.34.251 2 u 18 64 377 85.408 11.230 6.346
Copied!
the meaning :
1
* Synchronized to this peer
2
# Almost synchronized to this peer
3
+ Peer selected for possible synchronization
4
– Peer is a candidate for selection
5
~ Peer is statically configured
Copied!
tha's all.
.
.
.
.
Last modified 1yr ago