Skip to Content

3 ways to change time in Linux

In the Linux operating system, accurately managing time is crucial for various tasks, such as scheduling events, maintaining system logs, and ensuring synchronized operations across multiple systems.

Fortunately, Linux provides several methods to change and adjust the system time. Whether you need to set the time manually, synchronize it with a time server, or tweak specific time-related settings, this guide will walk you through three commonly used approaches for changing the time in Linux.

Procedure to change time in Linux

  1. Open the terminal.
  2. Type date and press Enter to see the current date and time.
  3. Type sudo date -s “YYYY-MM-DD HH:MM:SS” and press Enter to change the date and time. Replace YYYY-MM-DD HH:MM:SS with the actual date and time you want to set.
  4. The output will show the new date and time that has been set.
  5. Type hwclock -w  to set the hardware clock to the current system time.
  6. You can also use the timedatectl command to change the date and time. Type sudo timedatectl set-time “YYYY-MM-DD HH:MM:SS” and press Enter.

 

This article is part of the following series.

 

Change system time with date command in Linux

The system time in Linux is used to track events and schedule tasks. It is essential for various system services, such as network time synchronization, logging, and file modification time stamps.

The best way to change time in Linux is using date command.To set time, you can use the “-s” option with the date command. For example, to set the date to January 22nd, 2030 and the time to 23:59:59, you would type the following command:

date -s "22 Jan 2030 23:59:59"

The date command can be used to do a few different things, such as displaying the current date and time, setting the date and time.

The date command has a few different format options that you can use. The most common format is “MMDDhhmmYYYY”. For example, to set the date to February 2nd, 2020 at 6pm, you would type the following command:

date -s "Feb 02 18:00:00 2020"

The second most common format is “YYYYMMDD”. For example, to set the date to February 29th, 2020, you would type the following command:

date -s "20200229"

You can also use the ISO 8601 standard date format. For example, to set the date to February 29th, 2020, you would type the following command:

date -s "2020-02-29"

Change time with timedatectl command in Linux

The timedatectl command is another way to control and configure the system time and date settings in Linux.

To change the time using the timedatectl command in Linux, you can follow the steps below:

Open the terminal on your Linux system.

Use the following command to view the current system time and date:

timedatectl

To change the system time, use the set-time option followed by the desired time value. The time should be specified in the format “HH:MM:SS” (hours:minutes:seconds). For example, to set the time to 09:30:00, you would run:

timedatectl set-time 09:30:00

If you want to change the date, use the set-time option followed by the desired date value. The date should be specified in the format “YYYY-MM-DD” (year-month-day). For example, to set the date to January 1, 2023, you would run:

 timedatectl set-time 2023-01-01

If you want to change both the time and date together, use the set-time option followed by the desired time and date values in the format “YYYY-MM-DD HH:MM:SS”. For example, to set the time and date to January 1, 2023, at 09:30:00, you would run:

timedatectl set-time "2023-01-01 09:30:00"

After making the changes, verify the new time and date by running the timedatectl command again:

timedatectl

Note: Changing the system time may require administrative privileges, so you might need to use the sudo command or be logged in as a superuser.

Change hardware time with BIOS in Linux

The time in the BIOS (Basic Input/Output System) is the system time that is maintained by the computer’s hardware clock, also known as the Real Time Clock (RTC).

This clock is independent of the operating system and is powered by a small battery, which ensures that it keeps running even when the computer is turned off or unplugged.

The BIOS time is used by the computer’s firmware to keep track of time, and it is also used by the operating system to synchronize its own system time with the hardware clock during bootup.

To set your computer’s BIOS to the correct time, you will need to enter the BIOS setup utility.  The method to do this may vary depending on your computer model, but typically, you can press a specific key during the boot process to enter the BIOS. Check your computer’s documentation or try pressing keys such as F2, F10, or Del to enter the BIOS.

Once in the BIOS setup utility, look for the “Date” and “Time” settings. Use the arrow keys on your keyboard to navigate the BIOS menu and find the appropriate option. Set the date and time to the correct values.

Then save your changes and exit.  You can do this by pressing the F10 key or selecting an option to “Save and Exit” the BIOS. Make sure to save your changes before exiting the BIOS, or else your changes won’t be applied.

Change hardware time with hwclock command in Linux

The hwclock command in Linux is used to read and set the hardware clock (also known as the real-time clock or RTC) of the computer. The hardware clock is a built-in clock in the computer’s motherboard that keeps track of time even when the system is powered off.

The hwclock command is typically used for two main purposes:

  • Reading the hardware clock: By running hwclock without any options or arguments, it will display the current date and time stored in the hardware clock. This can be useful for checking the time when the system was last shut down or to compare it with the system’s current time.
  • Setting the hardware clock: The hwclock command can also be used to set the hardware clock to a specific date and time.

 

Here are some common options and arguments used with the hwclock command:

  • -r or –show: Displays the current date and time from the hardware clock.
  • -w or –systohc: Sets the hardware clock to the current system time.

It’s important to ensure that the hardware clock is set correctly and synchronized with the system time to avoid time discrepancies and potential issues with applications that rely on accurate timekeeping.

System Time in Linux: Initialization and Synchronization

When a Linux system is booted, the system time is initially set from the hardware clock (also known as the Real Time Clock or RTC) that is maintained by the computer’s BIOS or UEFI firmware. The hardware clock is an independent clock that runs on a small battery, and it keeps the time even when the computer is turned off.

During boot-up, the system reads the time from the hardware clock and sets the system time accordingly. The system time is then used by the Linux kernel and various system services to track time and schedule tasks.

After the system time is set from the hardware clock during boot-up, it may be periodically synchronized with a network time server or other time source using the NTP (Network Time Protocol) service. This helps ensure that the system time remains accurate and synchronized with other systems on the network.

How to change the timezone in Linux?

To change the timezone in Linux, you can use the “timedatectl” command. For example, to set your timezone to America/New_York, you would type the following command: sudo timedatectl set-timezone America/New_York

How to Sync time with  ntp server in Linux

The ntp service is a daemon that runs on Linux systems and synchronizes the system time with an NTP server. This ensures that the system time is accurate even when the system is turned off.

To use the ntp service, you will need to install the ntp package. Once installed, you can start the service by running “sudo service ntp start”. To configure the ntp service, you can edit the “/etc/ntp.conf” file.

To check if your computer’s NTP service is active, you can use the “timedatectl” command. For example, to check if the NTP service is active, you would type the following command:

sudo timedatectl status | grep -i ntp

If the NTP service is active, you will see a line that says “NTP synchronized: yes”. If the NTP service is not active, you will see a line that says “NTP synchronized: no”.

To manually sync your computer’s time with an NTP server, you can use the “ntpdate” command. For example, to sync your computer’s time with the NTP server pool.ntp.org, you would type the following command:

sudo ntpdate pool.ntp.org

Emily Gk

Monday 6th of November 2023

Thanks for the helpful info. Any idea why the time on my server is lagging all the time even I changed it?