This blog post will teach you how to set date and time from the command prompt in Linux OS using the date
and timedatectl
commands.
How can I set the system date and time from the command prompt? I don’t have GUI installed OS and I login over ssh session. How can I set date and time under Linux operating systems?
You can use date command to display the current date and time or set the system date and time over ssh session. You can also use the date command from X terminal as root user. You must login as root user to use the date command. Date command is useful if the Linux server or desktop os time and date is wrong, and you need to set the date and time from the shell prompt.
Example: –
Linux Display Current Date and Time:
type date command:
$ date
output: –
Sat Oct 29 00:03:53 IST 2022
Linux Display the Hardware Clock (RTC)
Type the following hwclock command to read the Hardware Clock and display the time on screen:
# hwclock -r
OR
# hwclock --show
OR show it in Coordinated Universal time (UTC):
# hwclock --show --utc
Sample outputs:
Sat 29 Oct 2022 12:28:11 AM IST -0.192539 seconds
Linux Set Date Command Example
Use the following syntax to set new data and time:# date --set="STRING"
For example, set new date to 28 Oct 2022 23:31:00, type the following command as root user:#
date -s “28 OCT 2022 23:31:00”
OR# date --set="28 OCT 2022 23:31:00"
You can also simplify format using following syntax:# date +%Y%m%d -s "20221028"
Linux Set Time Examples
To set time use the following syntax:# date +%T -s "23:31:00"
Where,
- 23: Hour (hh)
- 31: Minute (mm)
- 00: Second (ss)
Use %p locale’s equivalent of either AM or PM, enter:# date +"%T%p" -s "6:10:30AM"
# date +"%T%p" -s "12:10:30PM"
How do I set the Hardware Clock to the current System Time?
Use the following syntax:# hwclock --systohc
OR# hwclock -w
A note about systemd based Linux system
With systemd based Linux system you need to use the timedatectl command to set or view the current date and time. Most modern distro such as RHEL/CentOS v.7.x+, Fedora Linux, Debian, Ubuntu, Arch Linux and other systemd based system need to the timedatectl utility. Please note that the above command should work on modern system too.
Using timedatectl command display the current date and time
Type the following timedatectl command:# timedatectl
How do I change the current date using the timedatectl command?
To change the current date, type the following command as root user:# timedatectl set-time YYYY-MM-DD
OR$ sudo timedatectl set-time YYYY-MM-DD
For example set the current date to 2022-10-28 (28th, Oct, 2022):# timedatectl set-time '2022-10-28'
# timedatectl
Sample outputs:
Local time: Fri 2022-10-28 00:00:15 IST Universal time: Thu 2022-10-27 18:30:15 UTC RTC time: Thu 2022-10-27 18:30:15 Time zone: Asia/Kolkata (IST, +0530) NTP enabled: n/a NTP synchronized: no RTC in local TZ: no DST active: n/a
To change both the date and time, use the following syntax:# timedatectl set-time YYYY-MM-DD HH:MM:SS
Where,
- HH : An hour.
- MM : A minute.
- SS : A second, all typed in two-digit form.
- YYYY: A four-digit year.
- MM : A two-digit month.
- DD: A two-digit day of the month.
For example, set the date ’28th Oct 2022′ and time to ‘23:31:00 pm’, enter:# timedatectl set-time '2022-10-28 23:31:00'
# date
How do I set the current time only?
The syntax is:# timedatectl set-time HH:MM:SS
# timedatectl set-time '10:42:43'
# date
Sample outputs:
Mon Nov 28 23:31:00 EST 2022
How do I set the time zone using timedatectl command?
To see list all available time zones, enter:$ timedatectl list-timezones
$ timedatectl list-timezones | more
$ timedatectl list-timezones | grep -i asia
$ timedatectl list-timezones | grep America/New
To set the time zone to ‘Asia/Kolkata’, enter:# timedatectl set-timezone 'Asia/Kolkata'
Verify it:# timedatectl
Local time: Mon 2022-10-28 08:17:04 IST Universal time: Mon 2022-10-28 02:47:04 UTC RTC time: Mon 2022-10-28 13:16:09 Time zone: Asia/Kolkata (IST, +0530) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a
How do I synchronizing the system clock with a remote server using NTP?
Simply type the following command:# timedatectl set-ntp yes
Verify it:$ timedatectl
Sample outputs:
Local time: Mon 2022-10-28 08:18:49 IST Universal time: Mon 2022-10-28 02:48:49 UTC RTC time: Mon 2022-10-28 02:48:50 Time zone: Asia/Kolkata (IST, +0530) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a
Conclusion
Linux users can use date command to print or set the system date and time. Systemd based Linux users can use timedatectl to control the system time and date. You can also set new timezone using Linux command line. Read the following manual pages using the man command or help command:$ man 8 hwclock
$ man 1 date
$ man 8 timedatectl