How to Set Timezone and Sync Server Time with NTP in Linux

Using the correct time zone is essential for many systems related tasks and processes.

4
How to Sync Server Time with NTP in Linux Using Systemd

In this article we will learn what the NTP is, how to sync your server time and date using systemd-timesyncd network time service, and how to set the timezone in Linux.

You can easily keep your system’s date and time accurate by using NTP (Network Time Protocol). It lets you to synchronize computer clocks through network connections and keep them accurate. Basically a client requests the current time from a remote server, and uses it to set its own clock.

How to Synchronize Time with NTP Using systemd

The majority of Linux distributions have adopted systemd, and with it comes the systemd-timesyncd daemon. That means you have an NTP client already preinstalled, and there is no need to run the full-fledged ntpd daemon anymore. The built-in systemd-timesyncd can do the basic time synchronization job just fine.

To check the current status of time and time configuration via timedatectl, run the following command:

timedatectl status
               Local time: Thu 2021-05-13 15:44:11 UTC
           Universal time: Thu 2021-05-13 15:44:11 UTC
                 RTC time: Thu 2021-05-13 15:44:10
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

If you see NTP service: active in the output, then your computer clock is automatically periodically adjusted through NTP.

If you see NTP service: inactive, run the following command to enable NTP time synchronization.

timedatectl set-ntp true

That’s all you have to do. Once that’s done, everything should be in place and time should be kept correctly.

In addition, timesyncd itself is still a normal service, so you can check its status also more in detail via.

systemctl status systemd-timesyncd
systemd-timesyncd.service - Network Time Synchronization
      Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled) 
      Active: active (running) since Thu 2021-05-13 18:55:18 EEST; 3min 23s ago
      ...

If it is disabled, you can start and make systemd-timesyncd service active like this:

systemctl start systemd-timesyncd
systemctl enable systemd-timesyncd

How to Set/Change Timezone on Linux

Before changing your time zone, start by using timedatectl to find out the currently set time zone.

timedatectl
               Local time: Thu 2021-05-13 16:59:32 UTC
           Universal time: Thu 2021-05-13 16:59:32 UTC
                 RTC time: Thu 2021-05-13 16:59:31
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: inactive
          RTC in local TZ: no

Now let’s list all the available time zones, so you know the exact name of the time zone you’ll use on your system.

timedatectl list-timezones

The list of time zones is quite large. You do need to know the official time-zone name for your location.

Another way to find your timezone is to list (ls -l) the contents of the /usr/share/zoneinfo/ directory. Find your preferred timezone (/usr/share/zoneinfo/Zone/SubZone) where Zone/SubZone is your selection such as America/New_York, Europe/Paris, Asia/Bangkok, and so on. You got the idea.

Say you want to set the timezone on your Linux system to New York. The command should be like this:

timedatectl set-timezone America/New_York

This command creates a symbolic link for the time zone you choose from /usr/share/zoneinfo/ to /etc/localtime.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

In addition, you can skip the command shown above, create this symbolic link manually and achieve the same result.

ln -s /usr/share/zoneinfo/America/New_York /etc/localtime

Just in case you need, here’s the man page for the timedatectl command.

4 Comments

    • Yes, systemd-timesyncd is enough to do all the work. In fact, it implements an SNTP (Simple Network Time Protocol) client.

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge