Skip to content

Instantly share code, notes, and snippets.

@Prajwal-Koirala
Created July 22, 2024 16:16
Show Gist options
  • Save Prajwal-Koirala/acd2a0ee18b2d58c54ab9b38bed9c66e to your computer and use it in GitHub Desktop.
Save Prajwal-Koirala/acd2a0ee18b2d58c54ab9b38bed9c66e to your computer and use it in GitHub Desktop.

How to force Linux to sync time on boot using NTP (Network Time Protocol). Here's a comprehensive guide:

Synchronizing Time on Boot using NTP in Linux

Overview

In Linux, it's essential to keep accurate time for various system operations and applications. Network Time Protocol (NTP) is the standard protocol used to synchronize the time on Linux systems with time servers over the Internet or local network. This guide will show you how to configure your Linux system to sync time on every boot using NTP.

Prerequisites

  • Linux distribution (e.g., Ubuntu, Debian, CentOS, Fedora)
  • Root or sudo access to the system
  • Internet connectivity (for syncing with NTP servers)

Steps to Configure NTP Time Sync on Boot

Step 1: Install NTP Client

First, ensure that the NTP client software is installed on your system. Most Linux distributions come with ntp or chrony packages pre-installed. You can install them if not already present.

For Ubuntu/Debian:

sudo apt update
sudo apt install ntp

For CentOS/RHEL:

sudo yum install ntp

For Fedora:

sudo dnf install ntp

Step 2: Configure NTP Client

Once installed, you need to configure the NTP client to synchronize time on boot. The configuration file for NTP varies slightly between distributions.

For NTP (older method, common on older systems):

Edit the NTP configuration file /etc/ntp.conf:

sudo nano /etc/ntp.conf

Locate the server lines and add NTP servers that you want to sync with. For example:

server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org

Save and close the file (Ctrl + X, Y, Enter in nano).

For chrony (modern method, common on newer systems):

Edit the chrony configuration file /etc/chrony/chrony.conf:

sudo nano /etc/chrony/chrony.conf

Add NTP servers under the server directive:

server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
server 2.pool.ntp.org iburst

Save and close the file (Ctrl + X, Y, Enter in nano).

Step 3: Enable and Start NTP Service

After configuring NTP, you need to enable and start the NTP service to synchronize time on boot:

For NTP:

sudo systemctl enable ntp
sudo systemctl start ntp

For chrony:

sudo systemctl enable chronyd
sudo systemctl start chronyd

Step 4: Verify NTP Operation

To ensure that NTP is working correctly, check the synchronization status and system time:

For NTP:

ntpq -p
date

For chrony:

chronyc tracking
date

The output of these commands should indicate the synchronized status (* before the hostname in ntpq -p or * in chronyc tracking) and the correct time.

Step 5: Reboot and Test

Finally, reboot your system to verify that time synchronization occurs automatically on boot:

sudo reboot

After rebooting, check the system time again to ensure it's synchronized correctly:

date

Additional Notes

  • Firewall: If you have a firewall enabled, ensure that NTP traffic (UDP port 123) is allowed for outgoing connections.
  • Troubleshooting: If time synchronization fails, check logs (/var/log/messages, /var/log/ntp, or /var/log/chrony/chronyd.log) for any errors.
  • Security: Ensure your system time is accurate as many security protocols and applications depend on it.

Conclusion

By following these steps, you can configure your Linux system to synchronize time with NTP servers on every boot, ensuring accurate timekeeping for your applications and system operations.


Feel free to modify the NTP servers (0.pool.ntp.org, 1.pool.ntp.org, etc.) to those closer to your geographical location for better synchronization performance. Adjust the steps according to your specific Linux distribution's nuances if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment