Terminal Tuts

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

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 to be 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.

Cele mai bune jocuri de linie de comandă pentru Linux
Linia de comandă nu este doar cel mai mare aliat al tău când folosești Linux - poate fi și sursa de divertisment, deoarece poți să o folosești pentru ...
Best Gamepad Mapping Apps for Linux
If you like to play games on Linux with a gamepad instead of a typical keyboard and mouse input system, there are some useful apps for you. Many PC ga...
Instrumente utile pentru jucătorii Linux
Dacă vă place să jucați jocuri pe Linux, este posibil să fi folosit aplicații și utilitare precum Wine, Lutris și OBS Studio pentru a îmbunătăți exper...