Linux how to set date and time from the command prompt
With systemd based Linux system you need to use the timedatectl command to set or view the current date and time.
Using timedatectl command display the current date and time
Type the following timedatectl command:
timedatectl
Sample outputs:
Local time: Fri 2023-01-27 06:51:37 UTC
Universal time: Fri 2023-01-27 06:51:37 UTC
RTC time: Fri 2023-01-27 06:51:38
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
How do I change the current date using the timedatectl command?
To change the current date, type the following command as the root user:
timedatectl set-time 'YYYY-MM-DD'
Where,
- YYYY: A four-digit year.
- MM : A two-digit month.
- DD: A two-digit day of the month.
For example, set the current date to 2023-01-01 (1, Jan 2023):
timedatectl set-time '2023-01-01'
timedatectl
Sample outputs:
Local time: Sun 2023-01-01 00:00:06 UTC
Universal time: Sun 2023-01-01 00:00:06 UTC
RTC time: Sun 2023-01-01 00:00:07
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
If you see the message “Failed to set time: Automatic time synchronization is enabled”, then you need to type the following command to switch off as the root user.
timedatectl set-ntp 0
And one command to switch on again.
timedatectl set-ntp 1
To change both the date and time, use the following syntax:
timedatectl set-time YYYY-MM-DD HH:MM:SS
Where:
1. HH : An hour.
2. MM : A minute.
3. SS : A second, all typed in two-digit form.
4. YYYY: A four-digit year.
5. MM : A two-digit month.
6. DD: A two-digit day of the month.
For example, set the date ’1st Jan 2023′ and time to ‘8:10:40 am’, enter:
timedatectl set-time '2023-01-01 08:10:40'
date
Sample outputs:
Sun Jan 1 08:10:40 UTC 2023
Conclusion
Systemd based Linux users can use timedatectl to control the system time and date.