This topic describes how to change the time zone of an Elastic Compute Service (ECS) Linux instance and how to enable, configure, and use chrony to ensure that the local system time of the instance is synchronized with the UTC (Coordinated Universal Time) time. In this topic, an instance that runs a CentOS 7.8 operating system is used.

Prerequisites

An inbound rule is added to a security group of the ECS instance to allow traffic on UDP port 123. For more information, see Add a security group rule.

Background information

By default, ECS instances in all Alibaba Cloud regions use UTC+8. You can configure or change time zones for your instances.

CentOS 7 instances use chrony to synchronize the local system time with the UTC time. Compared with Network Time Protocol (NTP), chrony can synchronize the system clock more quickly and accurately on CentOS 6 instances and minimize time and frequency differences. chrony consists of the following core programs:
  • chronyd is a daemon process that runs in the background. chronyd is used to adjust the system clock that runs in the kernel to synchronize with the NTP clock server. chronyd can determine accurate statistics for the difference between the UTC time and the local system time and adjust the system time accordingly.
  • chronyc provides a user interface to monitor the performance of chronyd and to change various operating parameters in chronyd. chronyc can run on a server controlled by chronyd or a server not controlled by chronyd.
For more information, see Chrony.

Change the time zone of a Linux instance

  1. Connect to the Linux instance. For more information, see Connect to a Linux instance by using a password or key.
  2. Run the following command to view the time zone list:
    ls /usr/share/zoneinfo/<Name of the time zone folder>
    For example, you can run the following command to view the Hong_Kong time zone in the list:
    ls /usr/share/zoneinfo/Asia
  3. Run the following command to change the time zone:
    ln -sf /usr/share/zoneinfo/Asia/Hong-Kong /etc/localtime
  4. Run the following command to update the real-time clock (RTC):
    hwclock -w
  5. Run the following command to view the time zone:
    timedatectl status
    A command output similar to the following one indicates that the time zone is changed to Hong_Kong:
          Local time: -- 2020-09-14 08:00:04 UTC
      Universal time:-- 2020-09-14 08:00:04 UTC
            RTC time: -- 2020-09-14 08:00:04
           Time zone: Asia/Hong-Kong (UTC, +0000)

Enable chrony

  1. Connect to the Linux instance. For more information, see Connect to a Linux instance by using a password or key.
  2. Run the following commands to start the chronyd service and configure it to run on system startup:
    systemctl start chronyd.service
    systemctl enable chronyd.service
  3. Run the following command to view the time synchronization status of the instance to check whether the service is started:
    chronyc tracking
  4. Run the following command to view the list of servers that have chrony enabled:
    chronyc -n sources -v

Configure chrony

  1. Connect to the Linux instance. For more information, see Connect to a Linux instance by using a password or key.
  2. Run the following command to open the configuration file of chrony:
    vim /etc/chrony.conf
  3. Find server <NTP server> minpoll 4 maxpoll 10 iburst and press the I key to edit the file. Add # at the beginnings of sentences that contain the information of NTP servers that you want to hide.
  4. Add a row of NTP server information in the server <Required NTP server> minpoll 4 maxpoll 10 iburst format. Then, press the Esc key and enter :wq to save the file and exit.
    For more information about NTP servers, see Alibaba Cloud NTP server.
  5. Run the following commands to start the chronyd service and configure it to run on system startup:
    systemctl start chronyd.service
    systemctl enable chronyd.service
  6. Run the following command to view the list of servers that have chrony enabled:
    chronyc -n sources -v

Manually synchronize the clock by using chrony

  1. Run the following command to access chrony:
    chronyc
  2. Run the following command to synchronize the clock in chrony:
    makestep
    Note You can run the help command to obtain instructions for common chrony commands.