Accurate timekeeping is crucial for various system operations, including logging, synchronization, and scheduling tasks. In this tutorial, we’ll explore how to maintain precise time in Linux using timedatectl, configure timezones, utilize NTP (Network Time Protocol), employ the chronyd system service, and verify the correctness of time configurations.
Setting Timezone
# List available timezones
server2 ~]# timedatectl list-timezones | grep Cocos
Indian/Cocos
# Set the desired timezone
server2 ~]# timedatectl set-timezone Indian/Cocos
# Verifying Time Configuration
server2 ~]# timedatectl
Local time: Sat 2024-02-10 22:53:46 +0630
Universal time: Sat 2024-02-10 16:23:46 UTC
RTC time: Sat 2024-02-10 16:23:46
Time zone: Indian/Cocos (+0630, +0630)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
Configuring NTP
# Enable NTP synch
timedatectl set-ntp true
# Or Disable NTP synch
timedatectl set-ntp false
Using chronyd service for Time Synchronization
# Install chrony if not already installed
# dnf install chrony -y
Package chrony-4.3-1.el9.x86_64 is already installed.
# Start and enable the chronyd service:
# systemctl start chronyd
# systemctl enable chronyd
# Check status.
# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; preset: enabled)
Active: active (running) since Sat 2024-02-10 22:23:19 +0630; 24min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 637 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 644 (chronyd)
Tasks: 1 (limit: 11036)
Memory: 4.4M
CPU: 74ms
CGroup: /system.slice/chronyd.service
└─644 /usr/sbin/chronyd -F 2
Feb 10 22:23:19 server2.lab.hackelarre.cc chronyd[644]: chronyd version 4.3 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDN>
Feb 10 22:23:19 server2.lab.hackelarre.cc chronyd[644]: Frequency -14.674 +/- 28.457 ppm read from /var/lib/chrony/drift
Feb 10 22:23:19 server2.lab.hackelarre.cc chronyd[644]: Using right/UTC timezone to obtain leap second data
Feb 10 22:23:19 server2.lab.hackelarre.cc chronyd[644]: Loaded seccomp filter (level 2)
Feb 10 22:23:19 server2.lab.hackelarre.cc systemd[1]: Started NTP client/server.
Feb 10 22:23:26 server2.lab.hackelarre.cc chronyd[644]: Selected source 162.159.200.1 (2.rocky.pool.ntp.org)
Review NTP synchronization status with chronyc
server2 ~]# chronyc tracking
Reference ID : A29FC801 (time.cloudflare.com)
Stratum : 4
Ref time (UTC) : Sat Feb 10 16:24:47 2024
System time : 0.002985260 seconds fast of NTP time
Last offset : +0.001273302 seconds
RMS offset : 0.036962572 seconds
Frequency : 15.999 ppm slow
Residual freq : +0.156 ppm
Skew : 11.798 ppm
Root delay : 0.104762405 seconds
Root dispersion : 0.013802481 seconds
Update interval : 257.4 seconds
Leap status : Normal
[root@server2 ~]# chronyc sources -v
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 185.90.148.209 2 8 377 214 +49ms[ +50ms] +/- 180ms
^* time.cloudflare.com 3 8 377 158 +66ms[ +68ms] +/- 120ms
^+ 60.pool90-68-206.dynamic> 2 7 377 482 +81ms[ +83ms] +/- 153ms
^- i2t15.i2t.ehu.eus 1 8 377 30 +7726us[+7726us] +/- 551ms
Conclusion
Maintaining accurate time in Linux is essential for system stability and reliability. By mastering tools like timedatectl, configuring timezones, utilizing NTP with chronyd, and verifying configurations, administrators can ensure precise timekeeping across their Linux systems. Whether it’s scheduling tasks, logging events, or synchronizing with network time servers, accurate time management is a cornerstone of effective system administration. Apply these techniques to keep your Linux systems running smoothly and accurately synchronized with the rest of the world.