Linux

How to set the current time in Linux

This tutorial will discuss various methods of setting the current date and time from your Linux terminal session.
Captain Salem 4 min read
How to set the current time in Linux

As a Linux user, you will come across instances where you need to configure the current date and time of a Linux system without access to a Graphical interface. Whether you are configuring a remote machine via SSH or just too lazy to exit the terminal, learning how to set the date and tiem from the terminal can be useful.

Method 1 - Using the timedatectl command

The timedatectl command enables you to query and change the system clock and settings. It also allows you to configure time synchronization services.

We can use this command to set our current date and time.  Start by checking whether your system is configured to your desired timezone.

timedatectl

Output:

               Local time: Sun 2022-11-27 20:04:06 PST
           Universal time: Mon 2022-11-28 04:04:06 UTC
                 RTC time: Mon 2022-11-28 04:04:06
                Time zone: US/Pacific (PST, -0800)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no

In the example above, the current timezone is set to US/Pacific.

If your timezone is incorrect, you can change it by using the set-timezone command.

Start by listing all the available timezones in your machine.

timedatectl list-timezones | grep <target_timezone>

For example, to fetch the Los Angeles Timezone:

timedatectl list-timezones | grep Angeles

Result:

America/Los_Angeles

Finally, run the set-timezones command to update your system's timezone:

sudo timedatectl set-timezone <target_timezone>

Example:

sudo timedatectl set-timezone America/Los_Angeles

You can then verify the changes have been applied with the command:

timedatectl

Expected output:

Local time: Sun 2022-11-27 17:09:25 PST
           Universal time: Mon 2022-11-28 01:09:25 UTC
                 RTC time: Mon 2022-11-28 01:09:25
                Time zone: America/Los_Angeles (PST, -0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Once the correct timezone is configured, you need to enable the NTP service to synchronize your time. Run the command below to enable date and time synchronization:

sudo timedatectl set-ntp true

To check your current date and time, run:

date
Sun 27 Nov 2022 05:13:41 PM PST

Method 2 - Set Current Date and Time Using NTPD

The ntpd program is an operating system daemon which sets and maintains the system time of day in synchronism with Internet standard time servers

The nap protocol provides accurate date and time information from the internet as defined by your timezone. To set date and time using ntp, install the ntp daemon with the command:

sudo apt-get install ntp      [Debian, Ubuntu]
sudo yum install ntp					[CentOS, RHEL]
sudo pacman -S ntp						[Arch, Manjaro]

Once installed, run the command to check the status of the daemon:

sudo service ntp status

Output:

● ntp.service - Network Time Service
   Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-11-27 17:16:43 PST; 11s ago
     Docs: man:ntpd(8)
 Main PID: 1253 (ntpd)
    Tasks: 2 (limit: 1060)
   Memory: 1.4M
   CGroup: /system.slice/ntp.service
           └─1253 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 115:121

Nov 27 17:16:44 debian ntpd[1253]: Soliciting pool server 162.159.200.123
Nov 27 17:16:45 debian ntpd[1253]: Soliciting pool server 41.175.51.165
Nov 27 17:16:45 debian ntpd[1253]: Soliciting pool server 162.159.200.1
Nov 27 17:16:46 debian ntpd[1253]: Soliciting pool server 160.119.216.197
Nov 27 17:16:46 debian ntpd[1253]: Soliciting pool server 160.119.216.202
Nov 27 17:16:46 debian ntpd[1253]: Soliciting pool server 160.119.216.206
Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 2606:4700:f1::1
Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 64:ff9b::a29f:c801
Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 64:ff9b::a29f:c87b
Nov 27 17:16:47 debian ntpd[1253]: Soliciting pool server 64:ff9b::29af:33a5

Finally, activate time and date synchronization with the comand:

timedatectl set-ntp true

Verify:

timedatectl

Output:

Local time: Sun 2022-11-27 17:21:37 PST
           Universal time: Mon 2022-11-28 01:21:37 UTC
                 RTC time: Mon 2022-11-28 01:21:38
                Time zone: America/Los_Angeles (PST, -0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Method 3 - Setting System Date and Time using the date command

To set the date and time for your system using the date command, disable ntp with the command:

sudo timedatectl set-ntp false

Run the comand:

sudo date -s <current_date>

Specify the date in the format: YYYY-MM-DD HH:MM:SS

Example:

$ sudo date -s '2022-12-27 17:25:58'
Tue 27 Dec 2022 05:25:58 PM PST

Sync Hardware Clock with System Date

To synchronize your hardware clock with your current date and time, run the command:

sudo hwclock --systohc

Summary

In this tutorial, you learned various methods of setting up your current date and time from the comfort of your Linux terminal.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.