Best 3 Steps To Update Timezone DB in Linux (tz or zoneinfo)

Posted on

Best 3 Steps To Update Timezone DB in Linux (tz or zoneinfo)

Best 3 Steps To Update Timezone DB in Linux (tz or zoneinfo)

In this tutorial, we aim to guide you through the process of how to Update Timezone DB in Linux (Zoneinfo). The Time Zone Database, commonly referred to as tz or zoneinfo, plays a crucial role in ensuring your system displays the correct time. Occasionally, the timezone database in Linux can be incorrect, outdated, or require manual updates after changes in timezone rules. This necessitates updating the timezone DB or the tzdata.

You can update zoneinfo files with your Linux package manager. To do this, you can install and update the tzdata package on your Linux server and fix up the zoneinfo files.

To Update Timezone DB in Linux, you must have access to your server as a root or non-root user with sudo privileges. You can visit the Orcacore website and check for the Linux initial setup guides. In this guide, you will learn to:

Step 1 – Install and Update tzdata on Centos / AlmaLinux / RHEL

For RHEL-based distributions like CentOS, AlmaLinux, and RHEL, you can leverage YUM or DNF, the default package managers, to install the tzdata package. First, update your system:

# sudo yum update #centos7,RHEL7
# sudo dnf update #centos 8, AlmaLinux 8,9, RHEL 8,9

Then, install the tzdata package to Update Timezone DB in Linux:

# sudo yum install tzdata #centos7,RHEL7
# sudo dnf install tzdata #centos 8, AlmaLinux 8,9, RHEL 8,9

If the tzdata package is already installed, update it:

# sudo yum update tzdata #centos7,RHEL7
# sudo dnf update tzdata #centos 8, AlmaLinux 8,9, RHEL 8,9

Step 2 – Install and Update tzdata on Ubuntu / Debian

If you’re using Debian-based distributions like Ubuntu, you can use the apt package manager to install and update tzdata.

First, update the package list:

sudo apt update

Then, install the tzdata package:

sudo apt install tzdata

If the tzdata package is already installed, update it:

sudo apt update tzdata

Note: If you were unable to install or update the tzdata package, were unable to find a tzdata package, or don’t have a functional package manager, you can still update your zoneinfo files manually. Proceed to the next step to do it.

Step 3 – Update zoneinfo Files Manually in Linux

Visit the Time Zone Database page and download the latest tzdata package using wget:

sudo wget https://data.iana.org/time-zones/releases/tzdata2023c.tar.gz

Then, extract the downloaded file:

sudo tar -xvzf tzdata2023c.tar.gz

The zic command is a Linux program that reads the text containing the time zone from a file and creates the correct time conversion based on the specified command and timezone.

Compile a file based on your timezone. For instance, if your timezone is EDT, your file is named northamerica. Compile it by appending its name to zic:

sudo zic -d zoneinfo northamerica

Switch to your zoneinfo directory:

cd zoneinfo

Next, copy the files into the path with your time zone:

sudo cp -r * /usr/share/zoneinfo/

Now you can test that the proper dates and times are found in your zoneinfo files on your Linux server. For example:

zdump -v America/Chicago | grep 2009
America/Chicago  Sun Mar  8 07:59:59 2009 UTC = Sat Mar  7 01:59:59 2009 CST isdst=0 gmtoff=-21600
America/Chicago  Sun Mar  8 08:00:00 2009 UTC = Sun Mar  8 03:00:00 2009 CDT isdst=1 gmtoff=-18000
America/Chicago  Sun Nov  1 06:59:59 2009 UTC = Sun Nov  1 01:59:59 2009 CDT isdst=1 gmtoff=-18000
America/Chicago  Sun Nov  1 07:00:00 2009 UTC = Sun Nov  1 01:00:00 2009 CST isdst=0 gmtoff=-21600

This will find zoneinfo files for the 2009 DST changes.

Then, Simply run date from the command line and verify that the correct date and time are reported.

Conclusion

At this point, you have learned to Update Timezone DB in Linux. You can install or update tzdata or manually update zoneinfo files on your Linux distros.

Hope you enjoy it. You may be interested in these articles:

Fix PuTTY Server Refused Our Key Error

Create Tar.Gz File in Linux

Clear DNS Cache in Linux

Alternative Solutions to Update Timezone DB in Linux

While the above methods are effective, alternative approaches can also be used to Update Timezone DB in Linux. Here are two different methods, each with its explanation and code examples:

1. Using timedatectl (systemd-timedated)

timedatectl is a command-line utility that allows you to query and change the system clock and settings related to time. It’s part of the systemd-timedated service and is available on most modern Linux distributions that use systemd.

Explanation:

This method offers a more user-friendly way to manage timezones, particularly on systems where systemd is the init system. It provides an abstraction layer over the underlying zoneinfo files, making it easier to interact with timezone settings.

Steps:

  1. List Available Timezones: Use timedatectl list-timezones to view a comprehensive list of available timezones. You can pipe the output to grep to search for a specific region or city.

    timedatectl list-timezones | grep America
  2. Set the Timezone: Use timedatectl set-timezone to set the desired timezone.

    sudo timedatectl set-timezone America/Los_Angeles
  3. Verify the Change: Use timedatectl status to confirm that the timezone has been updated correctly.

    timedatectl status

    The output should display the new timezone.

Code Example:

# List timezones containing "Europe"
timedatectl list-timezones | grep Europe

# Set the timezone to Europe/London
sudo timedatectl set-timezone Europe/London

# Verify the timezone setting
timedatectl status

Benefits:

  • User-friendly interface.
  • Integrated with systemd.
  • Easier to query and change timezone settings.

Drawbacks:

  • Requires systemd.
  • May not be available on older Linux distributions.

2. Using a Configuration Management Tool (Ansible, Chef, Puppet)

Explanation:

For managing multiple Linux servers, using a configuration management tool is highly recommended. Tools like Ansible, Chef, and Puppet allow you to automate the process of updating the timezone database across your entire infrastructure.

Steps (Example with Ansible):

  1. Create an Ansible Playbook: Create a YAML file (e.g., timezone_update.yml) that defines the tasks to be performed.

    ---
    - hosts: all
      become: true
      tasks:
        - name: Update apt cache (Debian/Ubuntu)
          apt:
            update_cache: yes
          when: ansible_os_family == "Debian"
    
        - name: Install/Update tzdata
          package:
            name: tzdata
            state: latest
    
        - name: Set timezone (using timedatectl)
          command: timedatectl set-timezone {{ timezone }}
          register: timezone_result
          ignore_errors: true
    
        - name: Set timezone (dpkg-reconfigure tzdata)
          command: dpkg-reconfigure tzdata
          when: timezone_result is failed and ansible_os_family == "Debian"
    
        - name: Display current timezone
          command: timedatectl status
          register: timezone_status
        - debug:
            var: timezone_status.stdout_lines
  2. Create a Variable File (Optional): Create a YAML file (e.g., vars.yml) to define variables like the desired timezone.

    timezone: America/New_York
  3. Run the Ansible Playbook: Execute the playbook using the ansible-playbook command.

    ansible-playbook -i inventory.ini timezone_update.yml -e "@vars.yml"

    Replace inventory.ini with your Ansible inventory file.

Code Example (Ansible Playbook):

(See the YAML code blocks above for the full playbook)

Benefits:

  • Automated and repeatable.
  • Scalable to manage multiple servers.
  • Centralized configuration management.
  • Ensures consistency across your infrastructure.

Drawbacks:

  • Requires familiarity with configuration management tools.
  • Initial setup can be complex.

These alternative methods provide different approaches to Update Timezone DB in Linux, catering to various scenarios and preferences. Choosing the right method depends on your specific needs and environment.