When automating server deployments, any interactive command like dpkg-reconfigure tzdata
breaks the workflow. The challenge is to configure the timezone permanently while handling daylight saving time automatically.
The proper way involves two critical files:
# Set timezone for America/Los_Angeles
TIMEZONE="America/Los_Angeles"
echo $TIMEZONE > /etc/timezone
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
After configuration, verify with:
# Check system time
date
# Check timezone settings
timedatectl
# Verify symlink
ls -l /etc/localtime
For deployment scripts, consider this robust version:
#!/bin/bash
set_timezone() {
local tz="$1"
[ -f "/usr/share/zoneinfo/$tz" ] || {
echo "Error: Timezone $tz doesn't exist" >&2
return 1
}
echo "$tz" > /etc/timezone
ln -sf "/usr/share/zoneinfo/$tz" /etc/localtime
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
systemctl restart cron >/dev/null 2>&1
}
For RHEL/CentOS systems:
timedatectl set-timezone America/New_York
For Alpine Linux:
setup-timezone -z America/Chicago
In Docker containers, you'll need to:
RUN ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime \
&& echo "America/Los_Angeles" > /etc/timezone
When automating server deployments, timezone configuration often becomes the last manual step. The standard dpkg-reconfigure tzdata
command requires interactive input, breaking our precious CI/CD pipelines. Here's how to solve this properly.
Linux systems store timezone information in two critical locations:
1. /etc/localtime (binary timezone data)
2. /etc/timezone (textual timezone identifier)
For Ubuntu/Debian systems, here's the complete automation approach:
#!/bin/bash
TZ="America/Los_Angeles"
# Set the timezone identifier
echo "$TZ" | sudo tee /etc/timezone
# Create the symlink to zoneinfo
sudo ln -sf /usr/share/zoneinfo/$TZ /etc/localtime
# Optional: reconfigure to ensure all services pick up changes
sudo dpkg-reconfigure -f noninteractive tzdata
For containerized environments or minimal installations, you might need additional steps:
# For Alpine Linux:
apk add --no-cache tzdata
cp /usr/share/zoneinfo/$TZ /etc/localtime
echo "$TZ" > /etc/TZ
# For CentOS/RHEL:
timedatectl set-timezone $TZ
Always validate your timezone configuration:
date
timedatectl status
ls -l /etc/localtime
cat /etc/timezone
Remember that timezone rules change periodically. Include this in your maintenance scripts:
# For Debian/Ubuntu:
sudo apt-get install -y tzdata
# For RHEL/CentOS:
sudo yum update -y tzdata
For cloud deployments, add this to your cloud-init configuration:
#cloud-config
timezone: America/Los_Angeles