Changing Date and Time on Command line Terminal

This tutorial explains how to change or sync the date and time on Ubuntu Linux using a command-line terminal. Learn how to check and set the correct date and time on Ubuntu.

An Ubuntu system maintains three types of time: hardware time, system time, and network time.

Hardware time: - This time is provided by the hardware clock. Computer motherboards come with a pre-installed and configured hardware clock.

System time: - This time is provided by the system clock. The system clock is installed by the operating system during the installation of the operating system.

Network time: - This time is provided by an NTP server. An NTP server provides an accurate and reliable time using an atomic clock or an NTP server connected with an atomic clock.

Key points
  • The hardware clock and system clock are installed on the local system. Because of this, the date and time of the hardware clock and system clock can be set or changed.
  • Network time is used to sync the system clock and hardware clock. Since the network time is provided by the external NTP server, you cannot change it.
  • Hardware clock and system clock can be set manually or can be synced from an NTP server. You can use only one option at a time.
  • If you use the sync option, you can't set the date and time manually. If you set the date and time manually, you can't sync the date and time with the NTP server.

On Ubuntu, to manage time and set clocks, by default two commands are available: date and timedatectl.

The date command is a shell's built-in internal command. It only provides essential functions such as viewing and setting the date and time.

The timedatectl is an external command. It not only provides all functionally of the date command but also offers a lot of advance features and functions such as syncing time between clocks and changing time-zones.

I have already explained how to use the date command to manage the hardware clock and system clock in the previous part of this tutorial. The previous part of this tutorial is available on the following webpage.

Setting date, time, and time-zone using the date command

In this part, we will understand how to use the timedatectl command to view and change the date and time, sync time between clocks and change the time-zone.

Viewing current date and time

To view the current date and time, run the timedatectl command without any argument and option.

$timedatectl

The following image shows the output of the timedatectl command.

timedatectl command

The output of this command contains the following information.

Local time: Displays the system time in the following format.

Day of the week, Date (year-month-day), time (hours:minutes:seconds), and time-zone

Universal time: Displays system time in the UTC time-zone.

RTC time: Displays hardware time.

Time zone: Displays the name of the time-zone used by the system clock.

System clock synchronized: Displays whether the system clock is synchronized with the remote NTP server.

systemd-timesyncd.service active: Displays whether the time synchronization service between the system clock and NTP server is enabled or not.

RTC in local TZ: Displays whether the hardware clock is using the UTC time-zone or not.

Changing date

To change the date, use the following command.

$timedatectl set-time yyyy-mm-dd

For example, the following command sets the date to 7-April-2020.

$timedatectl set-time 2020-04-07

The following image shows the above example.

changing date

Setting or changing date and time

To change or set both date and time, use the following command.

$timedatectl set-time 'yyyy-mm-dd hh:mm:ss'

For example, the following command sets the date to 7 April 2020 and time to 11:20:15.

$timedatectl set-time '2020-04-07 11:20:15'

The following image shows the output of the above command.

setting date and time

Using network time

A public NTP server provides an accurate and reliable date and time. Instead of manually setting the date and time, you can instruct Ubuntu to set the system clock and hardware clock from a public NTP server automatically.

For this, you have to turn on the 'systemd.timesyncd.service' service.

Once this service is enabled, Ubuntu connects to the nearby public NTP server and retrieves the current date and time and sets the system clock and hardware clock according to the received date and time. This process is repeated periodically.

To turn on this service, use the following command.

$sudo timedatectl set-ntp on

The following image shows a sample output of this command.

syncing system time with ntp server

To turn off this service again, use the following command.

$sudo timedatectl set-ntp off

The following image shows a sample output of the above command.

timedatectl ntp-off

Changing time-zone

To change the time-zone, the exact name of the new time-zone is required. To view a list of all time-zones, use the following command.

$timedatectl list-timezones

You can use the grep command to filter and view specific time-zones. For example, the following command filters and displays all time-zones of Asia that start with the letter K.

$timedatectl list-timezones | grep -o "Asia/k.*"

The following image shows the output of the above command.

listing timezone

Once you the exact name of the new time-zone, you can use the following command.

$sudo timedatectl set-timezone "name of the new time-zone"

The following image shows an example of the above command.

setting new timezone

The above command changes the time-zone of the system clock only. It does not change the time-zone of the hardware clock. Due to many reasons, it is recommended to keep the hardware clock in the UTC time-zone.

But, due to any specific reason, if you want to change the time-zone of the hardware clock, you can do this by the following command.

$sudo timedatectl set-local-rtc 1

This command sets the time-zone of the hardware clock to the time-zone of the system clock.

The following image shows the output of this command.

changing time zone of hardware clock

To set the time zone of hardware clock back to UTC, use the following command.

$sudo timedatectl set-local-rtc 0

The following image shows the output of this command.

sudo timedatectl set-local-rtc 0

That's all for this part. In the next part of this tutorial, we will understand how to configure an NTP server in the local network. If you like this tutorial, please don't forget to share it with friends through your favorite social network.

ComputerNetworkingNotes Linux Tutorials Changing Date and Time on Command line Terminal