Configure a system to use time services

RHEL 7 has 3 command-line utilities to configure the system date and time:
1. date
2. hwclock
3. timedatectl

date command

Use the date command to display or set the system date and time. Run the date command with no arguments to display the current date and time:
# date
Mon Sep 12 19:41:40 IST 2016
The date command provides a variety of output formatting options. You can also time and date in future or past. Few examples are given below.
1. Display day of the week :
# date +%A
2. Display date one year from now :
# date -d "1 year"
Mon Sep 12 19:47:49 IST 2017
3. Display 1 month past date :
# date -d "1 month ago"
Mon Aug 12 19:49:07 IST 2016
Use the following syntax to change the current date. Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month.
# date +%D -s [YYYY-MM-DD]
Use the following syntax to change the current time. Replace HH with a two-digit hour, MM with a two-digit minute, and SS with a two-digit second. Include either AM or PM. Include the –u option if your system clock is set to use UTC.
# date +%T%p -s AM|PM –u

hwclock command

Use the hwclock command to query and set the hardware clock, also known as the RTC (real-time clock). This clock runs independently of any control program running in the CPU and even when the machine is powered off. The hwclock command allows you to:
  • Display the current time
  • Set the hardware clock to a specified time
  • Set the system time from the hardware clock (hwclock –s)
  • Set the hardware clock to the current system time (hwclock –w)

timedatectl command

– The timedatectl utility is part of the systemd system and service manager.
– To display local, universal, and RTC time and time zone, NTP configuration, and DST information:
# timedatectl
      Local time: Tue 2016-09-13 20:30:26 IST
  Universal time: Tue 2016-09-13 15:00:26 UTC
        RTC time: Tue 2016-09-13 15:00:26
       Time zone: Asia/Kolkata (IST, +0530)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
– Use the following syntax to change the date and time:
# timedatectl set-time [YYYY-MM-DD]
# timedatectl set-time [HH:MM:SS]
– Use the following syntax to change the time zone:
# timedatectl set-timezone [time_zone]
– To list available time zones :
# timedatectl list-timezones
– To enable clock synchronization over NTP:
# timedatectl set-ntp yes

Using NTP

NTP provides a method of verifying and correcting your computer’s time by synchronizing it with another system.
To install NTP :
# yum install ntp
By default, there are four public server entries in the NTP configuration file, /etc/ntp.conf, which are specified by the server directive.
# grep server /etc/ntp.conf 
Instead of using a predefined public server, you can specify a local reference server in the /etc/ntpd.conf file. For example:
# vi /etc/ntpd.conf
Another directive in the configuration file is driftfile. The default setting is as follows:
driftfile /var/lib/ntp/drift
This drift file contains one value used to adjust the system clock frequency after every system or service start.

NTP daemon

The ntpd program is the user space daemon that synchronizes the system clock with remote NTP time servers or local reference clocks. The daemon reads the configuration file at system start or when the service is restarted. You also need to open UDP port 123 in the firewall for NTP packets. After editing the /etc/ntp.conf file, use the systemctl command to start the NTP daemon:
# systemctl start ntpd
Use the following command to ensure the NTP daemon starts at boot time:
# systemctl enable ntpd

Other NTP utilities

Use the ntpq command to query the NTP daemon operations and to determine performance. Use the –p option (or peers command) to display a list of peers known to the server as well as a summary of their state. For example:
# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
*      2 u  911 1024  377    1.274    0.147   0.355
+      2 u 1026 1024  377    1.161    0.073   0.852
The * indicates your system is synchronized with the server. Use the ntpstat command to show network time synchronization status.
# ntpstat
synchronised to NTP server ( at stratum 3
   time correct to within 31 ms
   polling server every 1024 s

Configuring NTP using chrony

Chrony is a suite of utilities that provides another implementation of NTP. Chrony is designed for mobile systems and virtual machines that are often powered down or disconnected from the network. Systems that are not permanently connected to a network take a relatively long time to adjust their system clocks with the NTP daemon, ntpd.
Chrony consists of chronyd, a daemon that runs in user space, and chronyc, a command- line program for making adjustments to chronyd. The chronyd daemon makes adjustments to the system clock that is running in the kernel. It uses NTP to synchronize with another system when network access is available. When network access is not available, chronyd uses the last calculated drift stored in the drift file to synchronize the system time.

Post a Comment