Linux Exercise: Maintain System Time

Hardware clock

  1. Retrieve the current time of the hardware clock. Is it set OK?
    • # hwclock
  1. On most UNIX systems, the hardware clock is set to UTC. However, on dual boot Windows/Linux machines the hardware clock is usually set to the local timezone. Look at the contents of /etc/adjtime to see what applies to your system.
    • # cat /etc/adjtime

CPU clock

  1. Retrieve the current time of the CPU clock. Is it set OK?
    • # date
      The CPU clock is always kept in UTC on a UNIX system, but the date command converts it to the local timezone based on the system timezone and the contents of the $TZ shell variable. The date command also shows the timezone, for instance CET (Central European Time) or CEST (Central European Summer Time).
  1. Set the $TZ variable to a different timezone and run the date command again.
    • # export TZ=America/New_York
    • # date
    • # export TZ=UTC
    • # date

System Time Zone

The System Time Zone is normally set to the physical location of the system, and is used for things such as system logging. If a user does not set a TZ variable, this timezone is also used for the users timezone.

  1. Perform an ls -l on the /etc/localtime file. What type of file is this, and what does it point to?
    • # ls -l /etc/localtime
      Depending on how your system was set up, /etc/localtime is typically a symbolic link to a file somewhere in /usr/share/zoneinfo, which describes the characteristics of that timezone. Alternatively, some Linux systems make a straightforward copy of the /etc/share/zoneinfo/... file to /etc/localtime.
      In any case, the file in /usr/share/zoneinfo contains all the information about the timezone. Not only does this contain the offset to UTC, but also when a timezone goes to Summer Time and back. And the file even contains historic information: The rules on when to go to Summer Time and back have changed quite a few times in the past.
  1. Delete the $TZ variable and run the date command. What timezone is used?
    • # unset TZ
    • # date
  1. Modify the system time zone so that it uses a different timezone.
    • # timedatectl set-timezone America/New_York
  1. Look at the file /etc/localtime again. What changed? Also run the date command again.
    • # ls -l /etc/localtime
    • # date

Network Time Protocol

Important: Red Hat 7, CentOS 7 and newer use "chrony", a new, alternative implementation of the NTP protocol. The LPI-102 exam assumes knowledge of the traditional ntpd server.

  1. If you are using Red Hat/CentOS 7 or newer, deactivate and disable chrony. Then install ntpd but don't activate it just yet.
    The traditional ntpd daemon is not included in Red Hat/CentOS 8 anymore, and has been replaced with Chrony. However the RPMs from Red Hat/CentOS 7 are still installable without issues on Red Hat/CentOS 8. You can download the ntp and ntpdate RPMs here and here.
    If you are using Red Hat/CentOS 9 or newer, then the Red Hat/CentOS 7 packages cannot be installed anymore: A dependency conflict prevents the required version of libcrypto to be installed. So you cannot do this part of the exercise on a Red Hat/CentOS 9 system.
    • # systemctl stop chronyd
    • # systemctl disable chronyd
    • On Red Hat/Centos 7:
      # yum -y install ntp
    • On Red Hat/Centos 8:
      # curl -O https://www.demo.wlid.nl/sources/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
      # curl -O https://www.demo.wlid.nl/sources/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
      # yum -y localinstall ntp*
  1. Set the system time deliberately to a wrong value, for instance an hour earlier or later.
    • # date 0415134917.35
      Note that the time specification is mmddHHMMyyyy.SS. The command above would set the time to 2017-04-15 13:49:35 in the local timezone. The conversion to UTC happens automatically.
  1. Look at the date and time.
    • # date
  1. Use the ntpdate command to synchronize the time with an NTP server on the internet, such as pool.ntp.org. Look at the date and time again.
    • # ntpdate pool.ntp.org
    • # date
  1. Check the configuration of the NTP server. See if there are at least a few "server" lines in the configuration file. Also check the "drift" file.
    • # cat /etc/ntp.conf
      ...
      driftfile /var/lib/ntp/drift
      ...
      server 0.centos.pool.ntp.org iburst
      server 1.centos.pool.ntp.org iburst
      ...
    • # cat /var/lib/ntp/drift
      On a freshly installed system it is possible that this file does not exist yet, or contains 0.000. The drift value (the difference between the pace of the cpu clock and the real-world time) will be calculated and updated by the NTP daemon automatically.
  1. Start the NTP server and make sure it is restarted after a reboot.
    • # systemctl start ntpd
    • # systemctl enable ntpd
  1. Use the ntpq tool to retrieve information from the NTP server. Use "rl" for the current status, and "peers" and "associations" to see which servers are connected to your system.
    • # ntpq
    • ntpq> rl
    • ntpq> peers
    • ntpq> associations
End of exercise