Configure system time using timedatectl in ubuntu 23.04

Why Timedatectl? Hardware clocks in computers and system are not perfect and they might get drift away from the real time. For example, after some days the system clock may drift one or two seconds ahead or behind. It’s important that the servers must have an accurate time as there are multiple time based jobs that needs to run on the servers. Now a days we don’t manually set time in our gadgets like phones, computers and laptops because they automatically fetch time through internet. They fetch time automatically from NTP servers also known as time servers. Most modern softwares include time synchronization by default and in Ubuntu it is managed by systemd-timesyncd utility.

There are 24 different time zones around the world with the difference of one hour. For example if its 04:00 in Germany then it will be 08:00 in Pakistan. If we have servers in different regions than the server might get synced to that time zone but it might cause issues in viewing the logs of the server. Sometimes the servers might get synced to wrong time zone then you can configure the time zone of the servers which you want.

The first thing we might do on the server is to set its time zone. In Linux time related operations can be performed via the timedatectl utility. In this article we will deep dive into the set of commands that can be used to configure system time.

To list all the time zones using timedatectl command

To see the available timezones this command can be used.

sudo timedatectl list-timezones

The output might look like this.

Timedatectl command list-timezones

To set timezone using timedatectl command

To set the timezone this command can be used

sudo timedatectl set-timezone Asia/Karachi

To verify the time we can use this.

sudo timedatectl

The output might look like this

To activate NTP service using timedatectl command:

If we come across a servers which shows that the ntp service is not active then this can be done via below command

sudo timedatectl set-ntp true

Now check with the the timedatectl command to see if the ntp is active.

If the package is not installed already then it can be installed manually using this command

sudo apt install systemd-timesyncd

To check the status of timesyncd daemon:

To see the status of time sync daemon use the following command,

sudo systemctl status systemd-timesyncd.service 

The output might look like this

To set different time server in timesyncd file:

To change the setting of timesyncd daemon we can edit the timesyncd.conf file

 sudo nano /etc/systemd/timesyncd.conf 

Here we can specify which time server or NTP server we want to use. By uncommenting the line that starts with NTP we can manually set the NTP servers through which our servers will get synced.

To check for the servers in your country you can visit ntppool.org. It is recommended to use pool.ntp.org to find an NTP server. We can add multiple servers in a single line as shown below.

NTP= 0.asia.pool.ntp.org 1.asia.pool.ntp.org 2.asia.pool.ntp.org
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

After this restart the timesyncd daemon to apply the changes

sudo systemctl restart systemd-timesyncd.service

To check if the time servers we added are used or not we use the following command

sudo timedatectl show-timesync

or we can also use below command for more details related to ntp server.

sudo timedatectl timesync-status

Thanks for reading the blog. You can also use NTPd or chronyd as NTP client. For more Linux related blogs, do visit our website simplealltech.com

To set up NTP server and client on ubuntu 22.04 visit our blog on how to set NTP server and client.

2 thoughts on “Configure system time using timedatectl in ubuntu 23.04”

  1. I will immediately take hold of your rss as I
    can not to find your e-mail subscription hyperlink or e-newsletter service.
    Do you’ve any? Please permit me realize in order that I
    may just subscribe. Thanks.

    Reply

Leave a Comment