How to Change System Time and Timezone on CentOS
When a server's time drifts, the first victims are the logs: during debugging, timestamps that don't match real events are as bad as contaminated evidence. Next come scheduled tasks — cron fires on time, so a few minutes of drift can make you miss a backup window. Then there's HTTPS certificate validation: if "current time" disagrees with the certificate's validity window, clients reject the cert outright. So syncing the clock looks trivial, but it's one of those basic operations you have to get right.
1. One-Command Time Update (Recommended)
The quickest path is a single command that installs the tool, syncs the time, and writes it to the hardware clock:
yum install ntpdate -y && ntpdate cn.pool.ntp.org && clock -w
Breaking it down:
yum install ntpdate -y: installs the ntpdate time-sync tool;ntpdate cn.pool.ntp.org: syncs the system time from the Chinese NTP server pool. It adjusts rather than jumps, so your logs don't develop a sudden gap;clock -w: writes the system time to the hardware clock (CMOS). This step is often forgotten — and if you forget it, the system reads the old time back from the hardware clock after reboot, so the sync was wasted.
Understanding the reverse operation, hwclock -s, helps clarify the mechanics: at boot the system reads the time from the hardware clock by default, so whether the hardware clock is accurate decides whether the system time is accurate after a restart.
2. Change the Timezone
Write the Shanghai timezone into the current timezone config:
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
If prompted to overwrite, enter y and press Enter. What this does: /etc/localtime is either a symlink or a copy of a timezone file, so replacing it tells the system "from now on, compute local time as Shanghai time".
Then verify with date:
date
The timezone should now show CST (China Standard Time), for example:
Tue Apr 4 10:30:00 CST 2023
If it still shows UTC or some other abbreviation, the /etc/localtime replacement didn't take — check file permissions and the path.
3. CentOS 7+: Prefer timedatectl
Starting with CentOS 7, timedatectl is built in and makes time and timezone management much more intuitive, avoiding hand-editing mistakes:
# View the current time and timezone
timedatectl
# Set the timezone to Shanghai
timedatectl set-timezone Asia/Shanghai
# Enable automatic NTP time sync
timedatectl set-ntp true
timedatectl set-ntp true turns on background auto-sync via systemd-timesyncd or chronyd, after which you rarely need to sync manually. On CentOS 8/9, ntpdate is gone from the default repositories, so timedatectl or chrony is the hassle-free choice.
4. Chrony: The Modern Way to Stay Accurate Long-Term
If the server needs sustained high accuracy (billing, log audits), install chrony:
yum install chrony -y
systemctl enable --now chronyd
# Check sync status
chronyc sources -v
Chrony beats ntpdate because it doesn't just "re-sync every so often" — it continuously tracks clock drift, keeps long-term stability alongside the hardware clock, tolerates network jitter better, and converges quickly after being offline for a while.
Common Questions
- Timezone changed but log timestamps didn't: restart services such as cron and nginx so they re-read the local timezone;
ntpdatesays deprecated / command not found: it was removed from the CentOS 8/9 default repos — usetimedatectl set-ntp trueor chrony instead;- Overseas server, Chinese time: replace
cn.pool.ntp.orgwithpool.ntp.orgor an NTP pool closer to the data center to reduce latency-induced error.
System time management is part of basic server operations. For more server configuration and operations tips, see the cloud servers category, such as server operations tips and Linux server basics.
Reference: NTP documentation https://www.ntp.org/documentation/, chrony documentation https://chrony-project.org/documentation.html
Original post: https://www.cnblogs.com/cqzhuomi/articles/17283254.html (cnblogs.com CQZHUOMI, repost)