Your server logs show timestamps that don't match your local time. Cron jobs run at unexpected hours. Application timestamps are off by hours. These are all symptoms of incorrect timezone configuration.

Understanding Linux Time

Linux uses two types of clocks: - Hardware clock (RTC): Battery-backed clock on the motherboard - System clock: Kernel's software clock, typically synchronized with NTP

Timezone settings affect how times are displayed and interpreted, while the underlying UTC time remains the same.

Typical Symptoms

  • date shows wrong time zone
  • Logs have incorrect timestamps
  • Cron jobs run at unexpected times
  • Application dates are off by hours
  • Database timestamps don't match expected values

Diagnosing Timezone Issues

Check Current Settings

```bash # View current date, time, and timezone date

# View detailed time settings timedatectl

# Check timezone timedatectl show --property=Timezone cat /etc/timezone # Debian/Ubuntu ls -la /etc/localtime # All distros

# Check hardware clock hwclock --show

# View available timezones timedatectl list-timezones | grep -i america timedatectl list-timezones | grep -i europe timedatectl list-timezones | grep -i asia

# Check timezone database ls -la /usr/share/zoneinfo/ ```

Check for Timezone Mismatches

```bash # Compare system time with hardware clock date && hwclock --show

# Check if NTP is synchronized timedatectl status

# Check timezone offset date +%z # Numeric timezone (+0000) date +%Z # Timezone abbreviation (UTC, EST, etc.)

# Check if /etc/localtime is a symlink or file ls -la /etc/localtime # Should be symlink to /usr/share/zoneinfo/Region/City ```

Solutions

Solution 1: Set Timezone with timedatectl

The modern and recommended method:

```bash # List available timezones timedatectl list-timezones

# Search for your timezone timedatectl list-timezones | grep -i new_york timedatectl list-timezones | grep -i london timedatectl list-timezones | grep -i tokyo

# Set timezone sudo timedatectl set-timezone America/New_York sudo timedatectl set-timezone Europe/London sudo timedatectl set-timezone Asia/Tokyo sudo timedatectl set-timezone UTC

# Verify change timedatectl date ```

Solution 2: Set Timezone with Symlink (Traditional)

```bash # Backup current localtime sudo cp /etc/localtime /etc/localtime.bak

# Create symlink to desired timezone sudo ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime

# For Debian/Ubuntu, also update /etc/timezone echo "America/New_York" | sudo tee /etc/timezone

# Verify ls -la /etc/localtime date ```

Solution 3: Set Timezone with dpkg-reconfigure (Debian/Ubuntu)

```bash # Interactive timezone configuration sudo dpkg-reconfigure tzdata

# Select your region and city from the menu # Changes are applied immediately ```

Solution 4: Fix Hardware Clock Sync

```bash # Check if hardware clock is set to UTC or local time timedatectl | grep "RTC in local TZ"

# Set hardware clock to UTC (recommended) sudo timedatectl set-local-rtc 0

# Sync system time to hardware clock sudo hwclock --systohc

# Sync hardware clock to system time sudo hwclock --hctosys

# If dual-booting with Windows, you may need local time # (Windows expects hardware clock to be local time) sudo timedatectl set-local-rtc 1 sudo hwclock --systohc ```

Solution 5: Configure NTP Time Synchronization

```bash # Check NTP status timedatectl status

# Enable NTP synchronization sudo timedatectl set-ntp true

# Or enable timesyncd service sudo systemctl enable systemd-timesyncd sudo systemctl start systemd-timesyncd

# Check timesyncd status systemctl status systemd-timesyncd journalctl -u systemd-timesyncd -n 20

# For systems using chrony sudo systemctl enable chronyd sudo systemctl start chronyd chronyc tracking

# For systems using ntpd sudo systemctl enable ntpd sudo systemctl start ntpd ntpq -p ```

Solution 6: Configure NTP Servers

```bash # For systemd-timesyncd sudo nano /etc/systemd/timesyncd.conf

[Time] NTP=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org FallbackNTP=ntp.ubuntu.com

# Restart timesyncd sudo systemctl restart systemd-timesyncd

# For chrony sudo nano /etc/chrony.conf # Add or modify servers: server 0.pool.ntp.org iburst server 1.pool.ntp.org iburst server 2.pool.ntp.org iburst server 3.pool.ntp.org iburst

sudo systemctl restart chronyd

# For ntpd sudo nano /etc/ntp.conf # Add or modify servers: pool 0.pool.ntp.org iburst pool 1.pool.ntp.org iburst

sudo systemctl restart ntpd ```

Solution 7: Fix Application-Specific Timezone Issues

Applications may have their own timezone settings independent of the system.

For PHP: ```bash # Edit php.ini sudo nano /etc/php/8.1/cli/php.ini sudo nano /etc/php/8.1/fpm/php.ini

# Find and set: date.timezone = America/New_York

# Or in PHP code: <?php date_default_timezone_set('America/New_York'); ```

For Java: ```bash # Pass timezone as JVM argument java -Duser.timezone=America/New_York -jar app.jar

# Or set environment variable export JAVA_OPTS="-Duser.timezone=America/New_York" ```

For PostgreSQL: ```sql -- Show current timezone SHOW timezone;

-- Set timezone for session SET timezone = 'America/New_York';

-- Set timezone in postgresql.conf -- timezone = 'America/New_York'

-- Or in ALTER DATABASE ALTER DATABASE mydb SET timezone = 'America/New_York'; ```

For MySQL: ```sql -- Show current timezone SELECT @@global.time_zone, @@session.time_zone;

-- Set global timezone SET GLOBAL time_zone = 'America/New_York';

-- Set session timezone SET time_zone = 'America/New_York';

-- Set in my.cnf [mysqld] default-time-zone='America/New_York' ```

For Node.js: ```bash # Set TZ environment variable export TZ=America/New_York node app.js

# Or in code process.env.TZ = 'America/New_York'; ```

For Python: ```python import os import time

# Set timezone via environment os.environ['TZ'] = 'America/New_York' time.tzset()

# Or use pytz for timezone-aware datetimes from datetime import datetime import pytz tz = pytz.timezone('America/New_York') now = datetime.now(tz) ```

Solution 8: Fix Cron Job Timezone

```bash # Cron uses system timezone by default # Check cron timezone systemctl status cron

# For user crontab, you can set CRON_TZ crontab -e

# Add at top: CRON_TZ=America/New_York

# Your jobs will run in this timezone 0 2 * * * /path/to/script.sh # Runs at 2 AM New York time

# For system crontab (/etc/crontab), check CRON_TZ cat /etc/crontab ```

Solution 9: Fix Container Timezone

For Docker containers:

dockerfile
# In Dockerfile
ENV TZ=America/New_York
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

```bash # Or via docker run docker run -e TZ=America/New_York -v /etc/localtime:/etc/localtime:ro myimage

# Or in docker-compose.yml environment: - TZ=America/New_York volumes: - /etc/localtime:/etc/localtime:ro ```

For Kubernetes:

yaml
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: app
    image: myimage
    env:
    - name: TZ
      value: "America/New_York"
    volumeMounts:
    - name: timezone
      mountPath: /etc/localtime
      readOnly: true
  volumes:
  - name: timezone
    hostPath:
      path: /usr/share/zoneinfo/America/New_York

Daylight Saving Time Considerations

```bash # Check DST transitions for your timezone zdump -v America/New_York | grep 2024

# Always use full timezone names (America/New_York) not abbreviations (EST) # This ensures proper DST handling

# Check current UTC offset date +%z # -0500 for EST, -0400 for EDT

# Verify DST handling TZ=America/New_York date TZ=UTC date ```

Verification

After making changes:

```bash # Verify timezone is correct timedatectl date cat /etc/timezone ls -la /etc/localtime

# Verify NTP sync is working timedatectl status timedatectl timesync-status

# Test with a known timezone TZ=UTC date TZ=America/New_York date

# Verify hardware clock hwclock --show

# Check application timezone if applicable php -i | grep date.timezone mysql -e "SELECT @@time_zone;" ```

Common Timezone List

```bash # United States America/New_York # Eastern Time America/Chicago # Central Time America/Denver # Mountain Time America/Los_Angeles # Pacific Time America/Alaska # Alaska Time Pacific/Honolulu # Hawaii Time

# Europe Europe/London # GMT/BST Europe/Paris # CET/CEST Europe/Berlin # CET/CEST Europe/Moscow # MSK

# Asia Asia/Tokyo # JST Asia/Shanghai # CST Asia/Singapore # SGT Asia/Kolkata # IST Asia/Dubai # GST

# Australia Australia/Sydney # AEST/AEDT Australia/Perth # AWST

# UTC UTC # Coordinated Universal Time ```

Quick Reference

```bash # Show current timezone timedatectl

# List all timezones timedatectl list-timezones

# Set timezone sudo timedatectl set-timezone America/New_York

# Enable NTP sudo timedatectl set-ntp true

# Sync hardware clock sudo hwclock --systohc

# Check timezone in scripts date +%Z echo $TZ cat /etc/timezone ```