System Administrator | All about Date & Time in linux

Modern operating systems distinguish between the following two types of clocks:
  • real-time clock (RTC), commonly referred to as a hardware clock, (typically an integrated circuit on the system board) that is completely independent of the current state of the operating system and runs even when the computer is shut down.
  • system clock, also known as a software clock, that is maintained by the kernel and its initial value is based on the real-time clock. Once the system is booted and the system clock is initialized, the system clock is completely independent of the real-time clock.
The system time is always kept in Coordinated Universal Time (UTC) and converted in applications to local time as needed. Local time is the actual time in your current time zone, taking into account daylight saving time (DST). The real-time clock can use either UTC or local time. UTC is recommended.
Red Hat Enterprise Linux 7 offers three command line tools that can be used to configure and display information about the system date and time: the timedatectl utility, which is new in Red Hat Enterprise Linux 7 and is part of systemd; the traditional date command; and the hwclock utility for accessing the hardware clock.

Using the timedatectl Command

The timedatectl utility is distributed as part of the systemd system and service manager and allows you to review and change the configuration of the system clock. You can use this tool to change the current date and time, set the time zone, or enable automatic synchronization of the system clock with a remote server.

 Displaying the Current Date and Time

To display the current date and time along with detailed information about the configuration of the system and hardware clock, run the timedatectl command with no additional command line options:

timedatectl

Displaying the Current Date and Time

 root@client:~# timedatectl 
 Local time: Sat 2018-03-03 20:23:00 IST
 Universal time: Sat 2018-03-03 14:53:00 UTC
 RTC time: Sat 2018-03-03 14:53:01
 Time zone: Asia/Kolkata (IST, +0530)
 Network time on: no
NTP synchronized: yes
 RTC in local TZ: no
root@client:~#

Changing the Current Time

timedatectl set-time HH:MM:SS
This command updates both the system time and the hardware clock. The result it is similar to using both the date --set and hwclock --systohc commands.

 

By default, the system is configured to use UTC. To configure your system to maintain the clock in the local time, run the timedatectl command with the set-local-rtc option as root:

timedatectl set-local-rtc boolean

To configure your system to maintain the clock in the local time, replace boolean with yes(or, alternatively, ytruet, or 1). To configure the system to use UTC, replace booleanwith no (or, alternatively, nfalsef, or 0). The default option is no.

Changing the Current Date

timedatectl set-time YYYY-MM-DD

Changing the Time Zone

If you have different requirements and need to change the time zone, you can use the timedatectlcommand to do so.

First, list the available time zones:

timedatectl list-timezones

Now set the time zone with timedatectl set-timezone, making sure to replace the highlighted portion below with the time zone you found in the list. You’ll need to use sudo with timedatectl to make this change:

sudo timedatectl set-timezone America/New_York

Controlling timesyncd with timedatectl

Until recently, most network time synchronization was handled by the Network Time Protocol daemon or ntpd. This server connects to a pool of other NTP servers that provide it with constant and accurate time updates.

Ubuntu’s default install now uses timesyncd instead of ntpd. timesyncd connects to the same time servers and works in roughly the same way, but is more lightweight and more integrated with systemd and the low level workings of Ubuntu.

We can query the status of timesyncd by running timedatectl with no arguments. You don’t need to use sudo in this case:

timedatectl
root@client:~# timedatectl 
 Local time: Sat 2018-03-03 20:29:58 IST
 Universal time: Sat 2018-03-03 14:59:58 UTC
 RTC time: Sat 2018-03-03 14:59:58
 Time zone: Asia/Kolkata (IST, +0530)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
root@client:~#

If timesyncd isn’t enabled, turn it on with timedatectl:

sudo timedatectl set-ntp on

Run timedatectl again to confirm the network time status. It may take a minute for the actual sync to happen, but eventually both Network time on: and NTP synchronized: should read yes.

Switching to ntpd

Though timesyncd is fine for most purposes, some applications that are very sensitive to even the slightest perturbations in time may be better served by ntpd, as it uses more sophisticated techniques to constantly and gradually keep the system time on track.

NTP uses hierarchies of servers with top-level servers synchronizing time with atomic clocks. This hierarchy levels are known as stratum, and the level can range between 1 and 15, both inclusive. The highest stratum level is 1 and is determined by the accuracy of the clock the server synchronizes with. If a server synchronizes with other NTP server with stratum level 3, then the stratum level for this server is automatically set to 4.

Make sure that you have opened UDP port 123 on the firewall.

Before installing ntpd, we should turn off timesyncd:

sudo timedatectl set-ntp no

Verify that timesyncd is off:

timedatectl

Look for Network time on: no in the output. This means timesyncd has been stopped. We can now install the ntp package with apt-get:

sudo apt-get install ntp

ntpd will be started automatically after install. You can query ntpd for status information to verify that everything is working:

sudo ntpq -p

root@client:~# ntpq -p
remote refid st t when poll reach delay offset jitter
============================================================================
0.ubuntu.pool.n .POOL. 16 p – 64 0 0.000 0.000 0.015
1.ubuntu.pool.n .POOL. 16 p – 64 0 0.000 0.000 0.015
2.ubuntu.pool.n .POOL. 16 p – 64 0 0.000 0.000 0.015
3.ubuntu.pool.n .POOL. 16 p – 64 0 0.000 0.000 0.015
ntp.ubuntu.com .POOL. 16 p – 64 0 0.000 0.000 0.015
+139.59.43.68 193.6.176.19 3 u 48 64 367 93.960 9.193 25.879
-123.108.200.124 194.80.204.184 2 u 15 64 377 96.411 -7.417 23.723
-bjaaland.ci.uv. 140.7.62.122 2 u 37 64 277 316.461 -50.024 16.194
+resolver1.skyfi 216.218.254.202 2 u 5 64 357 320.673 23.942 7.233
+2001:67c:1560:8 17.253.34.125 2 u 30 64 373 427.095 20.604 61.909
+se.dyn.im 156.133.237.214 2 u 18 64 377 205.221 5.204 72.973
*192.92.6.30 .PPS. 1 u 20 64 377 335.909 27.213 21.737
root@client:~#

===========================================================================

USING THE DATE COMMAND

By default, the date command displays the local time. To display the time in UTC, run the command with the --utc or -u command line option:
date --utc
You can also customize the format of the displayed information by providing the +"format"option on the command line:
date +"format"
Control Sequence Description
%H The hour in the HH format (for example, 17).
%M The minute in the MM format (for example, 30).
%S The second in the SS format (for example, 24).
%d The day of the month in the DD format (for example, 16).
%m The month in the MM format (for example, 09).
%Y The year in the YYYY format (for example, 2016).
%Z The time zone abbreviation (for example, CEST).
%F The full date in the YYYY-MM-DD format (for example, 2016-09-16). This option is equal to %Y-%m-%d.
%T The full time in the HH:MM:SS format (for example, 17:30:24). This option is equal to %H:%M:%S
 root@client:~# date +”%Y-%m-%d %H:%M”
2018-03-03 20:49
root@client:~#

Changing the Current Time

date --set HH:MM:SS

Changing the Current Date

date --set YYYY-MM-DD

 Changing the Current Date and time

date --set "2017-06-02 23:26:00"

============================================================================

USING THE HWCLOCK COMMAND

hwclock is a utility for accessing the hardware clock, also referred to as the Real Time Clock (RTC). The hardware clock is independent of the operating system you use and works even when the machine is shut down. This utility is used for displaying the time from the hardware clock. hwclock also contains facilities for compensating for systematic drift in the hardware clock.
The hardware clock stores the values of: year, month, day, hour, minute, and second. It is not able to store the time standard, local time or Coordinated Universal Time (UTC), nor set the Daylight Saving Time (DST).

 

Displaying the Current Date and Time

hwclock

Setting the Date and Time

Besides displaying the date and time, you can manually set the hardware clock to a specific time.

hwclock --set --date "dd mmm yyyy HH:MM"

Synchronizing the Date and Time

You can synchronize the hardware clock and the current system time in both directions.
  • Either you can set the hardware clock to the current system time by using this command:
hwclock --systohc
  • Note that if you use NTP, the hardware clock is automatically synchronized to the system clock every 11 minutes, and this command is useful only at boot time to get a reasonable initial system time.

    Or, you can set the system time from the hardware clock by using the following command:

hwclock --hctosys

Synchronizing the Hardware Clock with System Time

root@client:~# hwclock –debug
hwclock from util-linux 2.27.1
Using the /dev interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick…
…got clock tick
Time read from Hardware Clock: 2018/03/03 15:24:58
Hw clock time : 2018/03/03 15:24:58 = 1520090698 seconds since 1969
Time since last adjustment is 1520090698 seconds
Calculated Hardware Clock drift is 0.000000 seconds
Sat 03 Mar 2018 08:54:57 PM IST .446626 seconds

After sync the ntp time

root@client:~# hwclock –systohc –localtime
root@client:~# hwclock –debug
hwclock from util-linux 2.27.1
Using the /dev interface to the clock.
Last drift adjustment done at 1520091079 seconds after 1969
Last calibration done at 1520091079 seconds after 1969
Hardware clock is on local time
Assuming hardware clock is kept in local time.
Waiting for clock tick…
…got clock tick
Time read from Hardware Clock: 2018/03/03 21:02:17
Hw clock time : 2018/03/03 21:02:17 = 1520091137 seconds since 1969
Time since last adjustment is 58 seconds
Calculated Hardware Clock drift is 0.000000 seconds
Sat 03 Mar 2018 09:02:16 PM IST .179826 seconds
root@client:~#

============================================================================

CONFIGURING NTP USING THE CHRONY SUITE

 In Linux systems, the NTP protocol is implemented by a daemon running in user space.The user space daemon updates the system clock running in the kernel. The system clock can keep time by using various clock sources. Usually, the Time Stamp Counter (TSC) is used. The TSC is a CPU register which counts the number of cycles since it was last reset. It is very fast, has a high resolution, and there are no interruptions.
There is a choice between the daemons ntpd and chronyd, available from the repositories in the ntp and chrony packages respectively.

Differences Between ntpd and chronyd

Things chronyd can do better than ntpd:
  • chronyd can work well in an environment where access to the time reference is intermittent, whereas ntpd needs regular polling of time reference to work well.
  • chronyd can perform well even when the network is congested for longer periods of time.
  • chronyd can usually synchronize the clock faster and with better accuracy.
  • chronyd quickly adapts to sudden changes in the rate of the clock, for example, due to changes in the temperature of the crystal oscillator, whereas ntpd may need a long time to settle down again.
  • In the default configuration, chronyd never steps the time after the clock has been synchronized at system start, in order not to upset other running programs. ntpd can be configured to never step the time too, but it has to use a different means of adjusting the clock, which has some disadvantages including negative effect on accuracy of the clock.
  • chronyd can adjust the rate of the clock on a Linux system in a larger range, which allows it to operate even on machines with a broken or unstable clock. For example, on some virtual machines.
  • chronyd is smaller, it uses less memory and it wakes up the CPU only when necessary, which is better for power saving.

Things chronyd can do that ntpd cannot do:

  • chronyd provides support for isolated networks where the only method of time correction is manual entry. For example, by the administrator looking at a clock. chronyd can examine the errors corrected at different updates to estimate the rate at which the computer gains or loses time, and use this estimate to adjust the computer clock subsequently.
  • chronyd provides support to work out the rate of gain or loss of the real-time clock, for example the clock that maintains the time when the computer is turned off. It can use this data when the system boots to set the system time using an adapted value of time taken from the real-time clock. These real-time clock facilities are currently only available on Linux systems.
  • chronyd supports hardware timestamping on Linux, which allows extremely accurate synchronization on local networks.
Things ntpd can do that chronyd cannot do:
  • ntpd supports all operating modes from NTP version 4 (RFC 5905), including broadcast, multicast and manycast clients and servers. Note that the broadcast and multicast modes are, even with authentication, inherently less accurate and less secure than the ordinary server and client mode, and should generally be avoided.
  • ntpd supports the Autokey protocol (RFC 5906) to authenticate servers with public-key cryptography. Note that the protocol has proven to be insecure and will be probably replaced with an implementation of the Network Time Security (NTS) specification.
  • ntpd includes drivers for many reference clocks, whereas chronyd relies on other programs, for example gpsd, to access the data from the reference clocks using shared memory (SHM) or Unix domain socket (SOCK).

Choosing Between NTP Daemons

Chrony should be preferred for all systems except for the systems that are managed or monitored by tools that do not support chrony, or the systems that have a hardware reference clock which cannot be used with chrony.

Note

Systems which are required to perform authentication of packets with the Autokey protocol, can only be used with ntpd, because chronyd does not support this protocol. The Autokey protocol has serious security issues, and thus using this protocol should be avoided. Instead of Autokey, use authentication with symmetric keys, which is supported by both chronyd and ntpdChrony supports stronger hash functions like SHA256 and SHA512, while ntpd can use only MD5 and SHA1.

 Installing chrony:-

apt-get install chrony

OR

yum install chrony

The default location for the chrony daemon is /usr/sbin/chronyd. The command line utility will be installed to /usr/bin/chronyc

NOTE:- If you have install ntp it will autoremoved.

Checking if chrony is Synchronized

root@client:~# chronyc tracking
Reference ID : 125.132.236.55 (mail.lafkor.de)
Stratum : 3
Ref time (UTC) : Sat Mar 3 16:02:15 2018
System time : 0.000455908 seconds slow of NTP time
Last offset : +0.008529777 seconds
RMS offset : 0.020625478 seconds
Frequency : 9.370 ppm fast
Residual freq : -26.239 ppm
Skew : 1000000.000 ppm
Root delay : 0.217213 seconds
Root dispersion : 2.865103 seconds
Update interval : 65.5 seconds
Leap status : Normal
root@client:~#

Reference ID

This is the reference ID and name (or IP address) if available, of the server to which the computer is currently synchronized. Reference ID is a hexadecimal number to avoid confusion with IPv4 addresses.

Stratum

The stratum indicates how many hops away from a computer with an attached reference clock we are. Such a computer is a stratum-1 computer, so the computer in the example is two hops away (that is to say, a.b.c is a stratum-2 and is synchronized from a stratum-1).

Ref time

This is the time (UTC) at which the last measurement from the reference source was processed.

System time

In normal operation, chronyd never steps the system clock, because any jump in the timescale can have adverse consequences for certain application programs. Instead, any error in the system clock is corrected by slightly speeding up or slowing down the system clock until the error has been removed, and then returning to the system clock’s normal speed. A consequence of this is that there will be a period when the system clock (as read by other programs using the gettimeofday() system call, or by the date command in the shell) will be different from chronyd‘s estimate of the current true time (which it reports to NTP clients when it is operating in server mode). The value reported on this line is the difference due to this effect.

Last offset

This is the estimated local offset on the last clock update.

RMS offset

This is a long-term average of the offset value.

Frequency

The “frequency” is the rate by which the system’s clock would be wrong if chronyd was not correcting it. It is expressed in ppm (parts per million). For example, a value of 1 ppm would mean that when the system’s clock thinks it has advanced 1 second, it has actually advanced by 1.000001 seconds relative to true time.

 

Checking chrony Sources

The sources command displays information about the current time sources that chronyd is accessing. The optional argument -v can be specified, meaning verbose. In this case, extra caption lines are shown as a reminder of the meanings of the columns.

root@client:~# chronyc sources
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
====================================================================
^* mail.lafkor.de 2 6 377 35 -43ms[ -40ms] +/- 175ms
^+ altus.ip-connect.net.ua 2 6 377 36 +77ms[ +79ms] +/- 230ms
^+ muenchen.kumi.systems 3 6 377 35 +10ms[ +10ms] +/- 242ms
^+ hotel.zq1.de 3 6 377 36 -1219us[-1219us] +/- 146ms
root@client:~#

 Checking chrony Source Statistics

root@client:~# chronyc sourcestats
210 Number of sources = 4
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
================================================================
mail.lafkor.de 11 9 461 -18.985 211.649 -13ms 22ms
altus.ip-connect.net.ua 11 5 461 +14.355 157.158 +10ms 17ms
muenchen.kumi.systems 11 6 462 +4.283 68.917 +9332us 9104us
hotel.zq1.de 11 7 461 -7.069 144.878 -7928us 16ms
root@client:~#

configuration file for chrony in ubuntu:- /etc/chrony/chrony.conf
in redhat :- /etc/chrony.conf

root@client:~# chronyc
chrony version 2.1.1
Copyright (C) 1997-2003, 2007, 2009-2015 Richard P. Curnow and
others chrony comes with ABSOLUTELY NO WARRANTY. This is free 
software, and you -are welcome to redistribute it under certain 
conditions. See theGNU General Public License version 2 for details.

chronyc> help

 

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s