Published on

Datetime and Chronyd

Authors
  • Name
    Jackson Chen

Red Hat Linux

Configure the Date and Time

Modem operating systems hsa two types of clocks

  1. A real-time clock (RTC), commonly referred to as hardware clock (typically an integrated circuit on the sytem board). It runs when the computer is shut down
  2. A system clock, or software clock it is maintained by the kernel.

The sytem time is always kept in Coordinated Universal Time (UTC). Local time is the actual time in your current time zone, taking into account daylight saving time (DST). The real-time clock is recommended to use UTC.

Utilities

RHEL 7 and later provides three command line tools that can be used t oconfigure and display information about system date and time.

1. timedatectl
    It is part of systemd
2. date
    Traditional command
3. hwclock
    Utility to access hardware clock

Using timedatectl command

# To show date and time values
timedatectl
    This will show the software clock and hardware clock, such as
    Local time:
    Universal Time:
    RTC Time
    Time Zone:
    NTP Enabled: Yes | No

Note:
    Ensure system is confiugred to read time from UTC, not RTC (Real Time clock)
timedatectl set-local-rtc 0
    To disable sytem time read from UTC

Hardware Clock

# To display current hardware clock date and time
hwclock
    Thu 10 Aug 2020 15:11:10 AEST  -0.208080 seconds

# To set hardware clock
hwclock --set --date ""dd mmm yyyy HH:MM"
    dd      # two digit number
    mmm     # three letter abbreviation
    yyyy    # four digit number
    HH      # two digit number
    MM      # two digit number

hwclock --set --date "10 Sep 2020 13:30:30"
hwclock --set --date "12/08/2022 09:20:20"
    Note:
    a. Month is using 3 char
    b. dd-mm-yyyy hh:mm:ss  format

Important:
Need to ensure hardware clock is set to the correct time

# Hardware clock
hwclock -h  # get help

hwclock -r  # read hardware clock and print result
hwclock --show  # show current hardware clock
hwclock --hctosys   # set system clock to hardware clock
hwclock --adjust
hwclock --version

hwclock --compare   # compare hardware clock and system clock

/sbin/hwclock –show --localtime
/sbin/hwclock –-show --utc      # Prefer set hwclock to UTC


date    # show date and time

# Set hardware clock to match system time
# keep the time in UTC allows Linux to automatically adjust daylight savings 
hwclock --systohc   --utc   

# Look at clock settings
# It will also show time zone used
# a. Verify and update timezone if required
# b. UTC=true   # set UTC to true
cat /etc/sysconfig/clock    

Verify chronyc synchronization

To check if chrony is synchronized, make use of the tracking, sources, and sourcestats commands.

# Verify time source
chronyc sourcestats

# Check chronyc synchronization
chronyc tracking

# Better method watch real time update
systemctl restart chronyd ; watch chronyc tracking

Verify chronyc sources

# To display chronyc time sources
chronyc sources -v

It will show output (example)
M S    Name/IP adress      Stratum   Poll     Reach     LastRx     Last sample
------------------------------------------------------------------------------------------
^ *    <NTP-server>        2         10       300       510        -95us[ -99us] +/-  1700us

Where
M - Source Mode
    ^   server
    =   peer
    #   local clock
* - Source State
    *   current synced
    +   combined
    -   not combined
    ?   unreachable

Update sytem time

To update the system time if required when system is not able to synchonize time with external NTP server

# to update system time
date +%T -s "13:20:50"
    +%T     To view current time, and nothing else

# alternative command
timedatectl set-time HH:MM:SS
    timedatectl set-time 18:30:15

# Set both date and time
timedatectl set-time '2020-10-20 18:30:15'

# Change the current date
timedatectl set-time YYYY-MM-DD

# Set automatic time synchronization using NTP server
timedatectl set-ntp true

# To disable automatic time synchronization using NTP server
timedatectl set-ntp false

# Changing time zone
timedatectl list-timezones  # list time zones
timedatectl set-timezone <time-zone>

# Restart timedated
systemctl restart systemd-timedated.service

To show time with acuracy

# Show time up to milliseconds
date +"%Y%m%d.%H%M%S%2N

Update and verify /etc/chrony.conf

# Verify /etc/chrony.conf file
less /etc/chrony.conf

# Update NTP server
server <NTP-server1-ip> iburst
server <NTP-server2-ip> iburst

# specify directory for log file
logdir /var/log/chrony

Verify logs

There are logs that can be verified, depending on the configuration

tail -fq /var/log/messages
tail -fq /var/log/syslog

Where
    -f  follows as it grows
    -q  quite, never output header

Check port connectivity or connection

# Example 
tcpdum -ni ensf0 port 9002

Window time synchronization

Verify time synchronization status

When configure Active Directory domain controller (PDC) to use Cisco switch as the time source

# Need to use 0x8 for peer list
w32tm /config /syncfromflags:manual /manualpeerlist:"<cisco-device>,0x8"

w32tm /config /reliable:yes     # Set the NTP source as trusted source
# To monitor system time synchronization
w32tm /monitor /domain:<domain-FQDN>

# To show synchronization
w32tm /monitor

# Show synchronization status
w32tm /query /status /verbose
Resynchronization now
w32tm /resync /nowaite /rediscover

# Force time synchronization with domain controller
net time /set /y

Verify system time synchronization configuration

w32tm /query /configuration

# To find out current time on the domain controller
net time

# To find out domain time
net time /domain

Compare time with time source

w32tm /stripchart /computer:<time-source|system>

Restart time service

net stop w32time && net start w32time

Domain member server

# Run the commands if required to force update
w32tm /config /syncfromflags:DOMHIER /update
w32tm /resync /nowait
net stop w32time && net start w32time
w32tm /query /status

How to calculate process speed or time taken for the process

# Find out file copy time taken
time  cp  <path/file1>  <path/file2>