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.
Use the -u
option 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.
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.
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 :
lets do some examples:
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
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.
Fun fact: we can not use natpdate while ntp service is running:
/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:
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.
-n : Output all host addresses in dotted-quad numeric format rather than converting to the canonical host names.
the meaning :
tha's all.
.
.
.
https://developer.ibm.com/tutorials/l-lpic1-108-1/
https://www.geeksforgeeks.org/date-command-linux-examples/
https://jadi.gitbooks.io/lpic1/content/1081_maintain_system_time.html
https://www.geeksforgeeks.org/hwclock-command-in-linux-with-examples/
https://geek-university.com/ccna/network-time-protocol/
https://linux.die.net/man/8/ntpdate
https://docs.ntpsec.org/latest/ntpd.html
https://detailed.wordpress.com/2017/10/22/understanding-ntpq-output/
.
Last updated