Management of system data and time is very important in the world of Linux which provides a set of powerful commands to sync clock with the time server or adjustment of the data and time manually. In this detailed guide, we will discuss various methods and ways to work with Linux data and time systems.
Date and Time Management in Linux
Linux depends on a system clock which keeps track of data and time, it can be set or adjusted by using various commands and tools. The Data and Time should be kept accurate which is crucial for tasks like log file timestamps, scheduling tasks, and synchronized communication between systems.
Common Use Cases for Changing Date and Time
Following are a few of the scenarios in which we might need to control or adjust the Linux system’s data and time:
- Correcting an incorrect system clock.
- Synchronizing the system’s time with a time server (NTP).
- Setting a future or past date and time for testing purposes.
- Adjustment of time zones for travelling or working with remote systems placed at different time zones.
date command is used to change the date and time on a Linux system. It can be used to display and set both the date and time. Let’s have a look at some commonly used options:
1. Displaying the Current Date and Time
To print the current date and time we can simply type the data command at the terminal:
Thu Aug 09 15:30:00 UTC 2021
2. Change the Date and Time
We can use the
date command to set a new date and time in the following format:
sudo date -s "YYYY-MM-DD HH:MM:SS"
YYYY-MM-DD with the required date and
HH:MM:SS time in 24-hour format. For example, to set the date to August 10, 2022, at 14:30:00, you would enter:
sudo date -s "2022-08-10 14:30:00"
Make sure to use
sudo to change the system time as this operation requires administrative privileges.
3. How to Change the Date format on Linux?
You can change the date format in Linux using the
date command with the
+% format specifier. For example, to display the date in the format “YYYY-MM-DD”, use:
This will show the date in the desired format like:
Synchronizing Time with NTP
NTP (Network Time Protocol) is a widely used protocol used to synchronize the system time with remote time servers. In Linux, the
timedatectl command is used for managing time settings and NTP configuration.
1. Checking NTP Status
To check the NTP synchronization status, use:
It will display information about whether NTP synchronization is active or not.
Local time: Thu 2021-08-19 15:30:00 UTC Universal time: Thu 2021-08-19 15:30:00 UTC RTC time: Thu 2021-08-19 15:30:00 Time zone: UTC (UTC, +0000) System clock synchronized: yes NTP service: active RTC in local TZ: no
Where each property provides following information:
Local time: Current local time on the system.
Universal time: Current time in Coordinated Universal Time (UTC).
RTC time: Time stored in the hardware clock (Real-Time Clock).
Time zone: System’s current time zone, in this case, UTC (Universal Coordinated Time).
System clock synchronized: If the system clock is synchronized with an NTP server, where “yes” means synchronized.
NTP service: If NTP (Network Time Protocol) service is active.
RTC in local TZ: If the hardware clock is set to the local time zone, here “no” means it’s set to UTC.
2. How to Enabe NTP Synchronization?
To enable NTP synchronization, we can use which will enable automatic time synchronization with NTP servers.
sudo timedatectl set-ntp true
3. Manually Setting NTP Servers
We can manually set NTP servers by editing the
/etc/systemd/timesyncd.conf file as shown below:
[Time] NTP=server1.example.com server2.example.com
You can replace
server2.example.com with the addresses of preferred NTP servers.
How to Change Time Zone on Linux?
When travelling or working with remote systems in different regions, we need to change the time zones. Linux provides the
timedatectl command to manage time zones:
1. How to List Available Time Zones
To list available time zones we can use:
The output will look like this:
Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers ... Pacific/Wallis UTC
2. How to set a Time Zone in Linux
To set a new time zone we use following command:
sudo timedatectl set-timezone <your-timezone>
<your-timezone> with the desired time zone, for example,
How to Set Hardware Clock to the Current System Time in Linux?
For setting the hardware clock which is also known as the BIOS clock to the current system time in Linux, we can use the
hwclock command as shown below:
sudo hwclock --systohc
It will synchronize the hardware clock with the system time.
How to Set the Hardware Clock to UTC on Linux?
To set the hardware clock to UTC (Coordinated Universal Time) on Linux, use the
sudo timedatectl set-local-rtc 0
How to Synchronize System Clock with NTP Servers on Linux?
We can synchronize the system clock with NTP servers on Linux by using the
timedatectl command. It will configure your system to automatically synchronize its time with NTP servers.
sudo timedatectl set-ntp true
How do Persist Date and Time Changes Across Reboots on Linux?
To persist the data and time across reboots in Linux, we need to set the hardware clock to the current system time by using the
hwclock command with the
sudo hwclock --systohc
This ensures that the hardware clock is updated with the correct time during shutdown or reboot, and the changes are retained.