How to sync date and time from the command-line in Ubuntu

Ubuntu uses NTP for synchronizing time over the internet. It is a TCP/IP protocol that fetches the current date and time from a server. The NTP servers are connected to atomic clocks via third-party servers.

Going a tad deeper, Ubuntu (16.04 onwards) uses timedatectl / timesyncd services to synchronize time. Optionally, one can use chrony to serve the Network Time Protocol.

Today, let’s learn the command lines used in the Terminal to get the date and time synced to the internet atomic clock.

If you ever learned this earlier, you might be remembering ntpdate. This is deprecated and replaced with a powerful timesyncd tool. By default settings, it regularly checks and keeps your computer’s local time in sync. It also stores time updates locally so that time is maintained even after the reboot.

Checking Current Status

Launch terminal and use the timedatectl command as follows:

timedatectl status

You should see the output similar to this one:

kiran@foss-linux:~$ timedatectl status
Local time: Mon 2018-08-13 21:54:25 EDT
Universal time: Tue 2018-08-14 01:54:25 UTC
RTC time: Tue 2018-08-14 01:54:25
Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no

Installing and using Chrony

To install Chrony, enter the following command in the Terminal:

sudo apt install chrony

If you require a one-shot sync use, use the following command in the Terminal.

chronyd -q

If you require a one-shot time check without setting the time, enter:

chronyd

Adding your own time servers

By default settings, Ubuntu fetches date and time from pool.ntp.org. You can view and edit the details. Use ‘Edit’ command in the ‘Terminal.’

Edit /etc/chrony/chrony.conf

By default, these servers are configured:

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

Go ahead and replace the server details as needed and then save the config file. Restart the chrony service for the new settings to take effect.

sudo systemctl restart chrony.service

That’s it! I hope you enjoyed learning the command line way of syncing the date and time of your Ubuntu PC.

Leave a comment

Your email address will not be published. Required fields are marked *