DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Timezone Alignment

In the daily standup, it was noted that the timezone settings across the Nautilus Application Servers in the Stratos Datacenter are inconsistent with the local datacenter's timezone, currently set to America/Blanc-Sablon.

Synchronize the timezone settings to match the local datacenter's timezone (America/Blanc-Sablon)


Table of Contents

  1. Prerequisites
  2. Manual Configuration Steps
  3. Automated One-Line Commands
  4. Complete Script Implementation
  5. Verification Procedures
  6. Alternative Methods
  7. Troubleshooting Guide
  8. Timezone Information

Prerequisites

  • SSH access to all application servers
  • Sudo privileges on each server
  • Server credentials:
    • stapp01: User tony / Password Ir0nM@n
    • stapp02: User steve / Password Am3ric@
    • stapp03: User banner / Password BigGr33n

Manual Configuration Steps

Step 1: Connect to Each Application Server

# App Server 1
ssh tony@stapp01

# App Server 2
ssh steve@stapp02

# App Server 3
ssh banner@stapp03
Enter fullscreen mode Exit fullscreen mode

Step 2: Check Current Timezone

# Using timedatectl
timedatectl

# Using date command
date

# Check timezone configuration file
cat /etc/timezone 2>/dev/null || cat /etc/sysconfig/clock 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Timezone to America/Blanc-Sablon

Method 1: Using timedatectl (Recommended)

sudo timedatectl set-timezone America/Blanc-Sablon
Enter fullscreen mode Exit fullscreen mode

Method 2: Using Symbolic Link

sudo ln -sf /usr/share/zoneinfo/America/Blanc-Sablon /etc/localtime
Enter fullscreen mode Exit fullscreen mode

Method 3: Using tzdata (Debian/Ubuntu)

echo "America/Blanc-Sablon" | sudo tee /etc/timezone
sudo dpkg-reconfigure --frontend noninteractive tzdata
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the Change

# Check updated timezone
timedatectl

# Verify with date command
date

# Check symbolic link
ls -la /etc/localtime

# Verify timezone file
cat /etc/timezone 2>/dev/null || cat /etc/sysconfig/clock 2>/dev/null

# Detailed status
timedatectl status
Enter fullscreen mode Exit fullscreen mode

Step 5: Exit the Server

exit
Enter fullscreen mode Exit fullscreen mode

Automated One-Line Commands

Complete Commands for All Servers

# stapp01
echo 'Ir0nM@n' | ssh tony@stapp01 "sudo -S bash -c 'timedatectl set-timezone America/Blanc-Sablon && timedatectl | grep \"Time zone\"'"

# stapp02
echo 'Am3ric@' | ssh steve@stapp02 "sudo -S bash -c 'timedatectl set-timezone America/Blanc-Sablon && timedatectl | grep \"Time zone\"'"

# stapp03
echo 'BigGr33n' | ssh banner@stapp03 "sudo -S bash -c 'timedatectl set-timezone America/Blanc-Sablon && timedatectl | grep \"Time zone\"'"
Enter fullscreen mode Exit fullscreen mode

Why This Syntax Works

The command uses sudo -S bash -c 'cmd1 && cmd2' to pass the password once and execute multiple commands within a single sudo session. This avoids the issue where only the first sudo command receives the password.

Using Heredoc (More Detailed Output)

# stapp01
ssh tony@stapp01 << 'EOF'
echo 'Ir0nM@n' | sudo -S bash -c '
echo "Current timezone:"
timedatectl | grep "Time zone"
echo ""
echo "Setting timezone to America/Blanc-Sablon..."
timedatectl set-timezone America/Blanc-Sablon
echo ""
echo "New timezone:"
timedatectl | grep "Time zone"
echo ""
date
'
EOF

# stapp02
ssh steve@stapp02 << 'EOF'
echo 'Am3ric@' | sudo -S bash -c '
echo "Current timezone:"
timedatectl | grep "Time zone"
echo ""
echo "Setting timezone to America/Blanc-Sablon..."
timedatectl set-timezone America/Blanc-Sablon
echo ""
echo "New timezone:"
timedatectl | grep "Time zone"
echo ""
date
'
EOF

# stapp03
ssh banner@stapp03 << 'EOF'
echo 'BigGr33n' | sudo -S bash -c '
echo "Current timezone:"
timedatectl | grep "Time zone"
echo ""
echo "Setting timezone to America/Blanc-Sablon..."
timedatectl set-timezone America/Blanc-Sablon
echo ""
echo "New timezone:"
timedatectl | grep "Time zone"
echo ""
date
'
EOF
Enter fullscreen mode Exit fullscreen mode

Complete Script Implementation

Create a script file named set_timezone.sh:

#!/bin/bash

# Define servers with their credentials
declare -A SERVERS=(
    ["stapp01"]="tony:Ir0nM@n"
    ["stapp02"]="steve:Am3ric@"
    ["stapp03"]="banner:BigGr33n"
)

TIMEZONE="America/Blanc-Sablon"

echo "========================================="
echo "Synchronizing Timezone to $TIMEZONE"
echo "========================================="

for server in "${!SERVERS[@]}"; do
    IFS=':' read -r user pass <<< "${SERVERS[$server]}"
    echo ""
    echo "-----------------------------------------"
    echo "Processing $server (User: $user)"
    echo "-----------------------------------------"

    sshpass -p "$pass" ssh -o StrictHostKeyChecking=no "$user@$server" "sudo -S bash -c '
        echo \"Current timezone:\"
        timedatectl | grep \"Time zone\"

        echo \"Setting timezone to $TIMEZONE...\"
        timedatectl set-timezone $TIMEZONE

        echo \"New timezone:\"
        timedatectl | grep \"Time zone\"

        echo \"Current date and time:\"
        date
    '"

    if [ $? -eq 0 ]; then
        echo "SUCCESS: $server completed successfully"
    else
        echo "ERROR: Failed to configure $server"
    fi
done

echo ""
echo "========================================="
echo "All App servers timezone synchronized"
echo "========================================="
echo ""
echo "Verification Commands:"
echo "  ssh tony@stapp01 'timedatectl | grep Time zone'"
echo "  ssh steve@stapp02 'timedatectl | grep Time zone'"
echo "  ssh banner@stapp03 'timedatectl | grep Time zone'"
Enter fullscreen mode Exit fullscreen mode

Make the script executable and run:

chmod +x set_timezone.sh
./set_timezone.sh
Enter fullscreen mode Exit fullscreen mode

Alternative Script Without sshpass

If sshpass is not available, use this version:

#!/bin/bash

TIMEZONE="America/Blanc-Sablon"

echo "========================================="
echo "Synchronizing Timezone to $TIMEZONE"
echo "========================================="

# stapp01
echo ""
echo "=== stapp01 ==="
ssh tony@stapp01 << 'EOF'
echo 'Ir0nM@n' | sudo -S bash -c '
echo "Setting timezone...";
timedatectl set-timezone America/Blanc-Sablon;
echo "Verification:";
timedatectl | grep "Time zone"
'
EOF

# stapp02
echo ""
echo "=== stapp02 ==="
ssh steve@stapp02 << 'EOF'
echo 'Am3ric@' | sudo -S bash -c '
echo "Setting timezone...";
timedatectl set-timezone America/Blanc-Sablon;
echo "Verification:";
timedatectl | grep "Time zone"
'
EOF

# stapp03
echo ""
echo "=== stapp03 ==="
ssh banner@stapp03 << 'EOF'
echo 'BigGr33n' | sudo -S bash -c '
echo "Setting timezone...";
timedatectl set-timezone America/Blanc-Sablon;
echo "Verification:";
timedatectl | grep "Time zone"
'
EOF

echo ""
echo "========================================="
echo "All App servers timezone synchronized"
echo "========================================="
Enter fullscreen mode Exit fullscreen mode

Verification Procedures

Verify Timezone on Each Server

# stapp01
ssh tony@stapp01 "echo 'Ir0nM@n' | sudo -S timedatectl | grep 'Time zone'"

# stapp02
ssh steve@stapp02 "echo 'Am3ric@' | sudo -S timedatectl | grep 'Time zone'"

# stapp03
ssh banner@stapp03 "echo 'BigGr33n' | sudo -S timedatectl | grep 'Time zone'"
Enter fullscreen mode Exit fullscreen mode

Verify with Date Command

# stapp01
ssh tony@stapp01 "date"

# stapp02
ssh steve@stapp02 "date"

# stapp03
ssh banner@stapp03 "date"
Enter fullscreen mode Exit fullscreen mode

Check Symbolic Links

# stapp01
ssh tony@stapp01 "ls -la /etc/localtime"

# stapp02
ssh steve@stapp02 "ls -la /etc/localtime"

# stapp03
ssh banner@stapp03 "ls -la /etc/localtime"
Enter fullscreen mode Exit fullscreen mode

Combined Verification Command

echo "=== stapp01 ===" && ssh tony@stapp01 "echo 'Ir0nM@n' | sudo -S timedatectl | grep Time" && \
echo "=== stapp02 ===" && ssh steve@stapp02 "echo 'Am3ric@' | sudo -S timedatectl | grep Time" && \
echo "=== stapp03 ===" && ssh banner@stapp03 "echo 'BigGr33n' | sudo -S timedatectl | grep Time"
Enter fullscreen mode Exit fullscreen mode

Expected Output

=== stapp01 ===
                Time zone: America/Blanc-Sablon (AST, -0400)
=== stapp02 ===
                Time zone: America/Blanc-Sablon (AST, -0400)
=== stapp03 ===
                Time zone: America/Blanc-Sablon (AST, -0400)
Enter fullscreen mode Exit fullscreen mode

Alternative Methods

Method 1: Using ln Command (if timedatectl unavailable)

# Remove existing link
sudo rm -f /etc/localtime

# Create new link
sudo ln -sf /usr/share/zoneinfo/America/Blanc-Sablon /etc/localtime
Enter fullscreen mode Exit fullscreen mode

Method 2: Using /etc/timezone (Debian/Ubuntu)

# Set timezone
echo "America/Blanc-Sablon" | sudo tee /etc/timezone

# Update system
sudo dpkg-reconfigure --frontend noninteractive tzdata
Enter fullscreen mode Exit fullscreen mode

Method 3: Using /etc/sysconfig/clock (RHEL/CentOS)

# Set timezone in config
echo 'ZONE="America/Blanc-Sablon"' | sudo tee /etc/sysconfig/clock

# Create symbolic link
sudo ln -sf /usr/share/zoneinfo/America/Blanc-Sablon /etc/localtime
Enter fullscreen mode Exit fullscreen mode

Method 4: Interactive Method (tzselect)

# This is interactive, not suitable for automation
tzselect
# Follow prompts to select America -> Blanc-Sablon
Enter fullscreen mode Exit fullscreen mode

Method 5: With NTP Sync

# If NTP is enabled, timezone change may trigger time sync
sudo timedatectl set-timezone America/Blanc-Sablon
sudo timedatectl set-ntp true
sudo timedatectl set-timezone America/Blanc-Sablon
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Guide

Issue 1: "Failed to set timezone: Invalid timezone"

Solution: Verify the timezone exists in the system:

timedatectl list-timezones | grep -i blanc-sablon
# Should show: America/Blanc-Sablon
Enter fullscreen mode Exit fullscreen mode

Issue 2: "Permission denied"

Solution: Use sudo or become root:

sudo timedatectl set-timezone America/Blanc-Sablon
Enter fullscreen mode Exit fullscreen mode

Issue 3: "System has not been booted with systemd"

Solution: Use the ln command method instead:

sudo ln -sf /usr/share/zoneinfo/America/Blanc-Sablon /etc/localtime
Enter fullscreen mode Exit fullscreen mode

Issue 4: Password not accepted in one-liner

Solution: Ensure the correct syntax with single sudo session:

# Incorrect - only first sudo gets password
echo 'pass' | sudo -S cmd1 && sudo -S cmd2

# Correct - single sudo session
echo 'pass' | sudo -S bash -c 'cmd1 && cmd2'
Enter fullscreen mode Exit fullscreen mode

Issue 5: Time not updating after timezone change

Solution: Restart systemd-timesyncd or force time sync:

# Restart time synchronization service
sudo systemctl restart systemd-timesyncd

# Or force time sync
sudo timedatectl set-ntp false
sudo timedatectl set-ntp true
Enter fullscreen mode Exit fullscreen mode

Issue 6: Multiple verification methods

# Check from multiple sources
timedatectl
date
cat /etc/timezone 2>/dev/null
cat /etc/sysconfig/clock 2>/dev/null
ls -la /etc/localtime
Enter fullscreen mode Exit fullscreen mode

Issue 7: sshpass not installed

Solution: Install sshpass or use heredoc method:

# Install sshpass
sudo yum install -y sshpass  # RHEL/CentOS
sudo apt-get install -y sshpass  # Ubuntu/Debian
Enter fullscreen mode Exit fullscreen mode

Timezone Information

America/Blanc-Sablon Details

  • Full Name: America/Blanc-Sablon
  • Standard Time: AST (Atlantic Standard Time)
  • UTC Offset: UTC-4
  • Daylight Saving Time: No DST observed
  • Location: Blanc-Sablon, Quebec, Canada
  • Major Cities: Blanc-Sablon, Quebec

Timezone Comparison

Timezone UTC Offset DST
America/Blanc-Sablon UTC-4 No
America/New_York UTC-4/UTC-5 Yes
America/Chicago UTC-5/UTC-6 Yes
America/Denver UTC-6/UTC-7 Yes
America/Los_Angeles UTC-7/UTC-8 Yes

Summary

The timezone settings have been successfully synchronized on all Nautilus Application Servers:

  • stapp01: Timezone set to America/Blanc-Sablon
  • stapp02: Timezone set to America/Blanc-Sablon
  • stapp03: Timezone set to America/Blanc-Sablon

All servers now have consistent timezone settings matching the local datacenter's timezone (America/Blanc-Sablon). Verification has been completed using timedatectl and date commands, and the timezone symbolic links have been updated accordingly.


Quick Reference Card

Manual Setup

# Connect and set timezone
ssh tony@stapp01
sudo timedatectl set-timezone America/Blanc-Sablon
timedatectl
exit
Enter fullscreen mode Exit fullscreen mode

One-Line Commands

# stapp01
echo 'Ir0nM@n' | ssh tony@stapp01 "sudo -S bash -c 'timedatectl set-timezone America/Blanc-Sablon && timedatectl | grep \"Time zone\"'"

# stapp02
echo 'Am3ric@' | ssh steve@stapp02 "sudo -S bash -c 'timedatectl set-timezone America/Blanc-Sablon && timedatectl | grep \"Time zone\"'"

# stapp03
echo 'BigGr33n' | ssh banner@stapp03 "sudo -S bash -c 'timedatectl set-timezone America/Blanc-Sablon && timedatectl | grep \"Time zone\"'"
Enter fullscreen mode Exit fullscreen mode

Verification

# Quick verification
ssh tony@stapp01 "echo 'Ir0nM@n' | sudo -S timedatectl | grep Time"
ssh steve@stapp02 "echo 'Am3ric@' | sudo -S timedatectl | grep Time"
ssh banner@stapp03 "echo 'BigGr33n' | sudo -S timedatectl | grep Time"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)