Getting older, not necessarily wiser!
In this post we will be discussing control of your computers timezone, time, and date via timedatectl, one of the many sub systems of systemd. We will also be looking at systemd-timedated.service, and systemd-timesync.service.
In some distributions you will find systemd installed alongside other programs that modify computer time. This is a prime example of redundancy, and why systemd needs to be modular. The end user has no choice about installing the time functions of systemd in distributions that use it.
This is not an issue on most modern hardware, as none of the alternative programs use large amounts of resources. However I found it interesting that systemd is adopted as the init system, and many of the other installed components are ignored.
Be that as it may, we will focus on systemd here, and not the alternatives in this post.
Before messing with system time, we want to determine the actual time and date status of the our system. To do that we will use timedatectl. Note that the argument โstatusโ is assumed so timedatectl and timedatectl status are equivalent.
[rt@archer ~]$ timedatectl Local time: Fri 2023-10-06 06:37:24 EDT Universal time: Fri 2023-10-06 10:37:24 UTC RTC time: Fri 2023-10-06 10:37:24 Time zone: US/Eastern (EDT, -0400) System clock synchronized: no NTP service: inactive RTC in local TZ: no
Running the command provides several pieces of information regarding computer time. It is hoped we all understand the concepts of UTC (Universal Coordinated Time) and local time.
RTC stands for Real Time Clock, which has been used on PC system boards since 1984. NTP stands for Network Time protocol, which is used for adjusting the RTC based on network time servers to ensure synchronization of time between computers.
Note that we will want to check if any other time service is running before engaging with systemd time services. Unfortunately there are a few possibilities out there, so you will need to check with your distribution documentation, and stop/uninstall any other time services as necessary.
If this is a new setup or install, the first thing we will want to do is make sure your computer is using the correct time zone. Lets use systemd to find out what the names of available time zones are, then set it. Note there are a lot of time zone names, so we want to use grep to help with our search.
[rt@archer ~]$ timedatectl list-timezones | grep US US/Alaska US/Aleutian US/Arizona US/Central US/East-Indiana US/Eastern US/Hawaii US/Indiana-Starke US/Michigan US/Mountain US/Pacific US/Samoa
[rt@archer ~]$ sudo timedatectl set-timezone America/New_York [rt@archer ~]$ sudo timedatectl set-timezone US/Eastern
Note, when listing time zones nothing was changed so sudo (administrative privilege) was not required. Also note that there are multiple names for the same time zone, like America/New_York vs US/Eastern. This is more of a personal preference than anything else.
We can also use the timedatectl to set date and time on our computer, assuming time sync services have not been activated (more on this later). Setting time with timedatectl uses one of two formats. Either HH:MM:SS (hours minutes seconds), or YYYY-MM-DD (year month day). When used together it will be YYY-MM-DD HH:MM:SS.
[rt@archer ~]$ sudo timedatectl set-time 2023-10-06 07:34:00
To enable time synchronization with a NTP server, we need to do three things; choose an NTP time server, enable/start the a synchronization service, then actually use it. So lets take it one step at a time.
Finding a NTP Server: The best place to check is the Network Time Foundation’s NTP Support Wiki. You might have to dive through a few links, but here you can find lists of time servers for any region of the world, along with technical information about time servers in general.
Using a Time Server: The systemd sub section dealing with time server communication is called systemd-timesyncd.service. If you take a look at its man page, you will be referred to the man page for timesync.conf. From here you can find some default locations for the configuration file. Since I am using Arch Linux in these examples, the file is found at /etc/systemd/timesync.conf. Lets open and edit this file, adding our time servers of choice (remember sudo). For myself I changed the following line form
#NTP=
to
NTP=0.us.pool.ntp.org 1.us.pool.ntp.org 2.us.pool.ntp.org
I added three space separated time servers and saved the file.
Enable time sync Service: We need to use our old friend systemctl to start/enable systemd-timesyncd.service.
[rt@archer systemd]$ systemctl enable systemd-timesyncd.servcie [rt@archer systemd]$ systemctl start systemd-timesyncd.servcie
Use the time sync service: Next we can use timedatectl to enable synchronization with our computer.
[rt@archer systemd]$ sudo timedatectl set-ntp true
At this point we can restart the service or reboot the computer for everything to be active.
[rt@archer systemd]$ sudo systemctl restart systemd-timedated
After all this our computer time will periodically be synced to one of our chosen NTP server.
[rt@archer systemd]$ timedatectl Local time: Fri 2023-10-06 14:04:06 EDT Universal time: Fri 2023-10-06 18:04:06 UTC RTC time: Fri 2023-10-06 18:04:06 Time zone: US/Eastern (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: no
This has been a quick introduction to using systemd sub system for setting and managing time on your computer. Yes there are other programs that can do similar jobs, but if you use systemd as your init, the time sub system is there by default.
One note, systemd uses SNTP (Simple Network Time Protocol) which is a subset of NTP (Network Time protocol). If your use/need case requires the full NTP protocol, systemd will fall short.
Next time around we will look at the systemd boot init sub system.
SystemD Leviathan Part 1: Overview
SystemD Leviathan Part 2: Service Units
SystemD Leviathan Part 3: Time Works
Add New Comment