108.1. Maintain system time
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:
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.
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.
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
-uoption to display UTC.
-uoption to display UTC:
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):
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.
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.
hwclockis 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.
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 :
-r, --show read hardware clock and print result
--get read hardware clock and print drift corrected result
--set set the RTC to the time given with --date
-s, --hctosys set the system time from the hardware clock
-w, --systohc set the hardware clock from the current system time
--systz set the system time based on the current timezone
--localtime the hardware clock is kept local time
-u, --utc the hardware clock is kept in UTC
lets do some examples:
### lets set hardware clock in utc
[email protected]:~# hwclock --set --date="01/02/1999 10:20:30"
Sat 02 Jan 1999 10:20:34 AM +0330 .274999 seconds
0.000000 915259830 0.000000
Sat 02 Jan 1999 10:21:48 AM +0330 .259334 seconds
Mon Feb 17 21:36:41 +0330 2020
### lets fix it by setting local time to hardware clock
Sat 02 Jan 1999 10:31:10 AM +0330 .009381 seconds
Mon Feb 17 21:39:56 +0330 2020
[email protected]:~# hwclock --localtime --set --date="02/17/2020 21:40:00"
0.000000 1581963000 0.000000
Mon 17 Feb 2020 09:40:08 PM +0330 .290810 seconds
Mon Feb 17 21:40:55 +0330 2020
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.
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.
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 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).
After this, we need to set the hwclock to the just corrected system time by sudo
hwclock -u -wto make sure you are setting that on utc .
-q switch will query for time and just show the result with out setting that.
Instead of manually setting the time each time, we can use a linux service called ntp. The
ntpdutility is an operating system daemon which sets and maintains the system time of day in synchronism with Internet standard time servers.
Fun fact: we can not use natpdate while ntp service is running:
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:
[email protected]:~# cat /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
# Enable this if you want statistics to be logged.
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
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst
# Use Ubuntu's ntp server as a fallback.
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# 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 limited
restrict -6 default kod notrap nomodify nopeer noquery limited
# Local users may interrogate the ntp server more closely.
# Needed for adding pool entries
restrict source notrap nomodify noquery