Kiến Thức Linux

How to Set Up Time Synchronization on Debian

Set up time synchronization on Debian

Introduction

Time synchronization is essential for ensuring the reliability, security, and functionality of Debian and other Unix-like systems. The Network Time Protocol (NTP) is an essential element for time sync that synchronizes the clocks of client computers with a time server.

This article explains how to set up time synchronization on Debian using NTP and an alternative.

Set up time synchronization on DebianSet up time synchronization on Debian

Prerequisites

  • Debian system (this tutorial uses Debian 11).
  • Non-root sudo access.

Setting up Time Sync on Debian

Correct time setting is essential for the proper functioning of modern software. Poor time synchronization across systems leads to various issues, from simple errors to severe data corruption. The following sections explain how to set up time synchronization on Debian.

Step 1: Check Time on Debian

Check the time on the server with the date command:

date
date terminal outputdate terminal output

The output shows the current time as well as the current date. The current time in the output is usually in Coordinated Universal Time (UTC). UTC is the time at zero degrees longitude and is accepted as a universal timezone.

Step 2: Set up Timezone

The date command prints the UTC zone by default. However, users sometimes need to change the timezone on Debian, which is done with the timedatectl command. Take the following steps to change the timezone on Debian:

1. List the available timezones on Debian:

timedatectl list-timezones
Listing timezones in Debian.Listing timezones in Debian.

The command prints a list of all the available timezones. 

2. Navigate the output with the Spacebar and b key. 

3. Choose the timezone. For example, US/Eastern.

Choosing a US/Eastern timezone.Choosing a US/Eastern timezone.

4. Press q to exit the list.

5. Change the timezone with the following command:

sudo timedatectl set-timezone <timezone>

For example, to change the timezone to US/Eastern run:

sudo timedatectl set-timezone US/Eastern

The command shows no output

6. Verify the change with date:

date
Changing the timezone.Changing the timezone.

The output shows the EDT time, the abbreviation for Eastern Daylight Time in US and Canada. 

Step 3: Check The Status of ntpd

Debian runs the standard Network Time Protocol daemon (ntpd) to sync the system time with external time servers. While NTP is the protocol for synchronizing time, ntpd is the program which implements the NTP protocol.

To confirm ntpd is running, execute the systemctl command:

sudo systemctl status ntp
Output for sudo systemctl status ntpOutput for sudo systemctl status ntp

The output shows the ntpd service is active. Therefore, ntpd is running and in sync with a time server.

However, the ntpq command provides additional info about the ntpd status:

ntpq -p
Output for ntpg -pOutput for ntpg -p

The ntpq command is an ntpd query tool. The -p argument specifies info about the NTP servers to which ntpd currently connects to.

Switching from ntpd to timesyncd

Timesyncd is a lightweight ntpd alternative, which is simpler to configure, more efficient, and more secure. Furthermore, timesyncd also integrates better with systemd. This feature makes it easy to manage using the systemd commands.

However, timesyncd cannot be used as a time server, and it is less sophisticated in keeping the system time in sync. These features make the program a less suitable choice for systems in need of accuracy and reliability. Complex real-time distributed systems generally work better with ntpd.

Still, users who choose timesyncd need to remove ntpd first. Follow the steps:

1. Uninstall ntpd:

sudo apt purge ntp
Output for sudo apt purgeOutput for sudo apt purge

2. If the timesyncd is not on the system, run the following to install it:

sudo apt install systemd-timesyncd
Terminal output for sudo apt install systemd timesyncdTerminal output for sudo apt install systemd timesyncd

2. Start the timesyncd service:

sudo systemctl start systemd-timesyncd

The command has no output.

3. Check the status:

sudo systemctl status systemd-timesyncd
Output for sudo systemctl status systemd-timesyncdOutput for sudo systemctl status systemd-timesyncd

The output confirms timesyncd is running. To display the current time and date, run:

timedatectl
Output for timedatectlOutput for timedatectl

The output prints info such as:

  • Local time.
  • Universal time.
  • System real-time clock (RTC) time.
  • Timezone.
  • Confirmation the system clock is synchronized.

Note: Using ntpd instead of timesyncd does not change the process of checking the date and time, or timezones, or changing timezones. The commands are the same, regardless of which NTP daemon is used.

Conclusion

After reading this text, you now know how to set up time synchronization on Debian with ntpd and timesyncd.

Next, learn how to get the current date and time in Python.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !