How to Change the System Time Zone on a CentOS Cloud Server?

16-01-2024 02:03:18

For cloud server administrators, maintaining the correct system time setting is crucial. Incorrect system time can cause many server environment issues, such as data inconsistency, data synchronization failure, and the failure of scheduled tasks.

To avoid these unnecessary troubles, we need to first set the correct system time zone. Next, we use the NTP (Network Time Protocol) service for real-time synchronization to ensure the system time is always precise. Below, we use a CentOS 7 server as an example to explain how to change the time zone and use the NTP service.

Changing the Time Zone

First, we use the date command to check the current time.

date

The result returned is as follows:

Fri Feb 21 07:23:08 UTC 2020

We can change the time zone from UTC (Coordinated Universal Time) to Beijing Time using the following command.

rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Confirm the change again with the date command.

Fri Feb 21 15:24:28 CST 2020

Step Two: Installing the NTP Service

The NTP service is a daemon running on Linux operating systems, used to synchronize with remote NTP servers. Use the following command to install the NTP service.

yum install ntp

Step Three: Configuring the Firewall

Since the NTP service requires communication through port 123 to function properly, we need to open this port in the firewall.

Use iptables for this:

iptables  -A  INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT
service iptables restart

With this, the time zone change and firewall configuration on the CentOS cloud server are complete.