Set time with date linux

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:

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:

Читайте также:  What is virtual file system in linux

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:

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:

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.

Читайте также:  Web сервер на linux ubuntu

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.

Читайте также:  Установка boost на linux

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:

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

How to change ubuntu’s server date and time via command line?

The Ubuntu server’s current date and time is different from the time zone date and time. I have tried using:

sudo date "30 Sep 2015 4:43:42" 

to change it but it did not change the date and time, just printed on terminal the date and time I changed, but when I executed:

The date and time is still the old one. What is the correct way to change date and time of Ubuntu Server?

7 Answers 7

You can set the system date with this command:

sudo date --set="2015-09-30 10:05:59.990" 

Then when using date , it should be showed correctly.

Now you should also the set hardware clock in the BIOS of the system, that the setting persists over a reboot (dureing the startup the system time is set to the value of the hardware clock). Do that with hwclock :

This gets the system clocks (sys) value and sets the hardware clock (hc). Check it with the hwclock command. Both hwclock and date should now show the same date and time.

To set your timezone, you can use this command:

sudo dpkg-reconfigure tzdata 

BTW: If you use a this machine as a server, I strongly recommend using an NTP-Client to sync the time over network. So you can guarantee that all your servers have the exactly same time set. This will sync the time while the machine runs. If you have applications which are dependent of synced time over server, I recommend the NTP-Daemon. The longer it runs in the background, the more precise is the time.

Источник

Оцените статью
Adblock
detector