This topic describes how to change the time zone of a Linux Elastic Compute Service (ECS) instance, and how to enable and configure the Network Time Protocol (NTP) service to ensure that the local time of the instance is precisely synchronized with the NTP server. In the topic, an ECS instance that runs CentOS 6.5 is used.
Prerequisites
UDP port 123, which is used by the NTP service for communication, is open on the ECS instance that you want to manage. You can run the netstat -nupl
command to check whether UDP port 123 is open on the instance. For information about how to open UDP port 123, 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 the time zones of your instances based on your business requirements.
The NTP service ensures that the local time of ECS instances is synchronized with the standard time. In Linux, you can run the ntpdate or ntpd command to synchronize the system clock with an NTP server. This topic describes the standard and custom NTP configurations. You can choose one based on your business requirements. For more information about the NTP service, see Alibaba Cloud NTP servers.
ntpdate
performs one-time synchronization. For newly purchased instances, you can usentpdate
to synchronize time.ntpd
adjusts the system clock in small steps. For instances with running workloads, we recommend that you usentpd
to synchronize time.
Change the time zone of a Linux instance
You must open and edit the time zone configuration file as the root user. Therefore, sudo
commands are used in the following operations.
Connect to a Linux instance.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to remove the symbolic link to the current time zone configuration:
sudo rm /etc/localtime
Run the following command to open and edit the
/etc/sysconfig/clock
configuration file:sudo vi /etc/sysconfig/clock
Press the
I
key to enter Insert mode and add a time zone city. For example, enterZone=Asia/Shanghai
.Press the
Esc
key to exit Insert mode and enter:wq
to save and close the file.(Optional) Run the following command to query the list of time zones:
ls /usr/share/zoneinfo
Run the following command to change the time zone to Asia/Shanghai:
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Run the following command to update the real-time clock (RTC):
hwclock -w
Run the following command to restart the instance:
sudo reboot
Warning The restart operation stops the instance for a short period of time and may interrupt services that are running on the instance. We recommend that you restart instances during off-peak hours.Run the following command to check the current time zone:
date -R
Enable the standard NTP service
Connect to a Linux instance.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to start the NTP service:
sudo service ntpd start
Run the following command to configure the NTP service to automatically start on system startup:
chkconfig ntpd on
Run the following command to check the status of the NTP service:
ntpstat
(Optional) View more information about the NTP service.
Run the following command to view the list of NTP peers:
ntpq -p
Run the following command to view the runlevel of the NTP service:
sudo chkconfig --list ntpd
Configure the custom NTP service
Connect to a Linux instance.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to open and edit the NTP service configuration file:
sudo vi /etc/ntp.conf
Press the
I
key to enter Insert mode. Findserver <NTP server> iburst
information in the file. For NTP servers that you do not need, add a number sign (#
) at the beginning of the lines to hide the servers.Add a new line of NTP server information in the following format:
server <NTP server> iburst
.NoteReplace
<NTP server>
in the preceding line with the actual address of the NTP server that you want to add.Press the
Esc
key to exit Insert mode. Enter:wq
to save and close the file.
Run the following command to start the custom NTP service:
sudo service ntpd start
Run the following command to configure the NTP service to automatically start on system startup:
chkconfig ntpd on
Run the following command to check the status of the NTP service:
ntpstat