Set Up Timezone and NTP on Linux VPS
Incorrect time settings on a VPS cause cron jobs to run at the wrong time, timestamps in logs become confusing, and SSL certificates fail validation. Properly configuring your timezone and synchronizing time via NTP is the first critical step after receiving a new VPS.
View and Change Timezone
Ubuntu/Debian uses timedatectl to manage system time:
# View current timezone
timedatectl
# List all available timezones
timedatectl list-timezones | grep Asia
# Set timezone to Bangkok (UTC+7)
sudo timedatectl set-timezone Asia/Bangkok
# Verify the change
timedatectl status
Asia/Bangkok makes reading logs much more convenient.
Install and Configure NTP
Ubuntu 20.04+ includes systemd-timesyncd built-in, which is lighter than ntpd and ideal for typical VPS deployments:
# Enable time synchronization
sudo timedatectl set-ntp true
# Check synchronization status
systemctl status systemd-timesyncd
# View which NTP server is being used
cat /etc/systemd/timesyncd.conf
To use Thai NTP servers, edit the configuration file:
sudo nano /etc/systemd/timesyncd.conf
[Time]
NTP=0.th.pool.ntp.org 1.th.pool.ntp.org 2.th.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org
# Restart to apply new configuration
sudo systemctl restart systemd-timesyncd
# Check synchronization status
timedatectl show-timesync --all
Configure Timezone for PHP and MySQL
If running a PHP web application, timezone must be set in two additional locations:
# Set timezone in PHP
sudo nano /etc/php/8.3/fpm/php.ini
# Change the line to:
date.timezone = Asia/Bangkok
# Set in MySQL (add to /etc/mysql/mysql.conf.d/mysqld.cnf)
[mysqld]
default-time-zone = '+07:00'
# Reload services
sudo systemctl reload php8.3-fpm
sudo systemctl restart mysql
Verify Timezone and NTP are Working Correctly
# Display current time with timezone
date
timedatectl
# Check that NTP synchronized = yes
timedatectl | grep "NTP synchronized"
# View offset from NTP server
chronyc tracking 2>/dev/null || timedatectl show-timesync --all
# Test PHP timezone
php -r "echo date('Y-m-d H:i:s T') . PHP_EOL;"
sudo systemctl restart cron to ensure cron jobs pick up the new timezone.
Common Issues & Troubleshooting
Error: "Failed to set time zone: Failed to manipulate /etc/localtime"
/etc/localtime is a regular file instead of a symlink. Delete it and let timedatectl recreate it:
sudo rm /etc/localtime
sudo timedatectl set-timezone Asia/Bangkok
ls -la /etc/localtime # Should be a symlink to /usr/share/zoneinfo/
NTP synchronized: no even though set-ntp true is enabled
Your firewall may be blocking UDP port 123 used by NTP, or your VPS provider might block outbound NTP traffic:
# Check firewall rules
sudo ufw status | grep 123
# Test connectivity to NTP server
ntpdate -q 0.pool.ntp.org
# If provider blocks NTP, install chrony for NTP over TCP
sudo apt install chrony -y
sudo systemctl enable --now chrony
PHP still shows incorrect time even after setting date.timezone
PHP may have multiple php.ini files depending on SAPI (CLI, FPM, Apache). Check all of them:
# Find which php.ini PHP actually uses
php --ini | grep "Loaded Configuration"
php-fpm8.3 -i 2>/dev/null | grep "date.timezone"
# Apply changes to all php.ini files
sudo sed -i 's|;date.timezone =|date.timezone = Asia/Bangkok|' /etc/php/*/cli/php.ini
sudo sed -i 's|;date.timezone =|date.timezone = Asia/Bangkok|' /etc/php/*/fpm/php.ini
Log timestamps are incorrect despite correct system time
rsyslog or journald may be logging in UTC regardless of system timezone:
# Configure rsyslog to use local timezone
sudo nano /etc/rsyslog.conf
# Add: $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# For journald, view logs in local timezone
journalctl --since today -u nginx --output=short-iso
Frequently Asked Questions
Should I set my VPS to UTC or Asia/Bangkok?
For production servers with multiple services and team members, UTC is the industry standard—it avoids confusion with Daylight Saving Time. However, for a personal VPS used by a single person in Thailand, Asia/Bangkok makes logs easier to read at a glance.
What's the difference between timedatectl and ntpdate?
ntpdate is a legacy tool that syncs time once and exits. timedatectl set-ntp true enables systemd-timesyncd, which continuously syncs time in the background. ntpdate is deprecated in modern Ubuntu and should be replaced with timesyncd or chrony.
Do I need to reboot after changing timezone?
No. timedatectl changes timezone immediately. However, processes already running may still use the old timezone. Restart important services such as MySQL, PHP-FPM, and cron to pick up the new timezone.