Blog / Linux/ Linux Server Timezone and Time Sync Guide: Setting UTC+8 and Updating Hardware Clock

Linux Server Timezone and Time Sync Guide: Setting UTC+8 and Updating Hardware Clock

Problem Background

When using a foreign Linux server to sync files with cloud storage services like Baidu Cloud BOS, you may encounter errors due to time desynchronization, such as:

BceServerError: The difference between the request time and the server's time is too large.

This occurs because many cloud services strictly validate request timestamps. To resolve this, you need to set the server's system timezone to China Standard Time (UTC+8, Asia/Shanghai), synchronize with network time, and finally write the correct time to the hardware clock (BIOS/RTC) to ensure accuracy after reboots.

Method 1: CentOS 7/8 & RHEL Systems (Recommended: timedatectl)

Modern CentOS/RHEL 7+ systems recommend using the timedatectl tool for timezone and time management.

1. Check Current Time & Timezone Status

timedatectl

This command displays system time, timezone, RTC (hardware clock) time, and NTP service status.

2. Set System Timezone to Shanghai (UTC+8)

sudo timedatectl set-timezone Asia/Shanghai

After setting, run timedatectl again to confirm the timezone is now 'Asia/Shanghai'.

3. Synchronize Network Time

Use NTP service to sync time. First ensure the system has chronyd or ntpd enabled and running:

# For systems using chrony (default on CentOS 7/8)
sudo systemctl enable --now chronyd
sudo chronyc sources

# Or, perform a manual sync with ntpdate (install if needed: yum install ntpdate)
sudo ntpdate ntp.aliyun.com

Note: If chronyd or ntpd is enabled and running, they will automatically sync time continuously; manual ntpdate is usually unnecessary.

4. Write System Time to Hardware Clock (BIOS/RTC)

This critical step ensures correct time after reboot. It's recommended to set the hardware clock to UTC, letting the OS convert based on timezone:

# Recommended: Set hardware clock to UTC
sudo timedatectl set-local-rtc 0
# Write current correct system time to hardware clock
sudo hwclock --systohc --utc

Alternatively, if you want the hardware clock to store local time directly (not recommended, may cause dual-boot issues):

sudo timedatectl set-local-rtc 1
sudo hwclock --systohc --localtime

Important: Setting the hardware clock to UTC is industry best practice, avoiding confusion from daylight saving time and timezone changes. Use local time only for specific needs (e.g., some older Windows dual-boot setups).

Method 2: Traditional Method (For CentOS 6 & Earlier)

1. Set Timezone Using tzselect or Direct Link

# Interactive timezone selection (choose Asia -> China -> Beijing Time)
tzselect
# Follow prompts to add line like TZ='Asia/Shanghai'; export TZ to /etc/profile and source it

# More direct method: Create timezone symlink
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2. Manually Sync Network Time & Write to Hardware Clock

# Install ntpdate (if not installed)
sudo yum install -y ntpdate
# Sync time using a domestic NTP server
sudo ntpdate ntp1.aliyun.com
# Write system time to hardware clock
sudo hwclock -w
# Or explicitly specify
sudo hwclock --systohc

Method 3: Ubuntu/Debian Systems

Ubuntu also uses timedatectl, steps are similar to CentOS 7:

# 1. Set timezone
sudo timedatectl set-timezone Asia/Shanghai
# 2. Enable and ensure NTP sync service is running (Ubuntu typically uses systemd-timesyncd)
sudo timedatectl set-ntp true
# 3. Write system time to hardware clock (recommend UTC mode)
sudo hwclock --systohc --utc

Verification & Troubleshooting

  • Verify System Time: date or timedatectl
  • Verify Hardware Clock Time: sudo hwclock --show
  • Check Timezone File: ls -l /etc/localtime should point to /usr/share/zoneinfo/Asia/Shanghai
  • Check Time Sync Service Status:
    • CentOS 7/8: sudo systemctl status chronyd
    • Ubuntu: sudo systemctl status systemd-timesyncd

Extended Knowledge: UTC vs Localtime

Real-Time Clock (RTC/Hardware Clock): The BIOS time, running independently of the OS. It can be set to store Coordinated Universal Time (UTC) or local time.
Best Practice: Set RTC to UTC. Upon boot, the OS reads the RTC's UTC time, then converts it to local time for display based on the system timezone (e.g., Asia/Shanghai). This avoids issues from timezone switches and daylight saving adjustments, and is the default or recommended configuration for most Linux distributions. Consider setting RTC to local time only when coexisting with another OS set to local time (e.g., some older Windows configurations).

Following these steps ensures your Linux server uses the correct UTC+8 time, stays synchronized with network time, and persists accurate time to the hardware, completely resolving cloud service API errors caused by time deviation.