You Date and Time in Linux using the timedatectl command, the date command, and how to modify your hardware clock using the hwclock command.
First, the timedatectl command. If you just type timedatectl, it will tell you your local time, UTC, RTC, your time zone, whether your system clock is synchronized with the hardware clock, whether Network Time Protocol (NTP) is enabled, and whether your RTC is in local configuration.
timedatectl
The command-line utility timedatectl allows you to view and change the system time, date, and timezone. It can also synchronize the system clock with a remote server via the Network Time Protocol (NTP). A part of the Systemd and service manager systems is called timedatectl.
The syntax for the timedatectl command is (Hour, Minute, and Seconds).
sudo timedatectl set-time HH:MM:SS
sudo timedatectl set-time 16:56:00
This will change your system time.
- When you set the date as indicated above, you can receive the following error:
root@ip-172-31-22-37:~# timedatectl
Local time: Mon 2024-07-29 15:00:11 UTC
Universal time: Mon 2024-07-29 15:00:11 UTC
RTC time: Mon 2024-07-29 15:00:11
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
- It states in the error that the NTP service is running. The command listed below must be used to disable it.
NTP(Network Time Protocol)
The Network Time Protocol, or NTP, is a transport layer protocol that synchronizes computer time across networks to provide a precise system time. It operates over port 123 UDP.
To disable NTP, use:
sudo timedatectl set-ntp no
root@ip-172-31-22-37:~# timedatectl
Local time: Mon 2024-07-29 22:55:48 IST
Universal time: Mon 2024-07-29 17:25:48 UTC
RTC time: Mon 2024-07-29 17:25:48
Time zone: Asia/Kolkata (IST, +0530)
System clock synchronized: yes
NTP service: inactive
RTC in local TZ: no
Enter your timedatectl will show that NTP is inactive. Now change in time.
To enable NTP, use:
sudo timedatectl set-ntp yes
- Enabling NTP allows your system to automatically fetch the time from NTP servers.
To change your timezone, you can use:
sudo timedatectl set-timezone TimeZone
- If you don’t know your time zone, you can list them with
root@ip-172-31-22-37:~# timedatectl list-timezones |grep -i "kolkata"
root@ip-172-31-22-37:~# timedatectl set-timezone "Asia/Kolkata"
To set the time zone,
date command
The Date command is used to display the system date and time. date command is also used to set the date and time of the system
Date
- To see the date in UTC, use:
date --utc
- To display the date in a custom format, use:
date "+%Y-%m-%d %H:%M"
- To change the date, use:
sudo date YYYYMMDDHHMM.SS
sudo date 202103051700.00
hwclock command.
Finally, the hwclock command. To view your hardware clock, use:
sudo hwclock
- To set your hardware clock, use:
sudo hwclock --set --date="MM/DD/YYYY HH:MM"
sudo hwclock --set --date="03/05/2021 17:00"
- To synchronize your system time with your hardware clock, use:
sudo hwclock --systohc
- To do the reverse, use:
sudo hwclock --hctosys
- If you have a dual-boot configuration, you could set your hardware clock to local time to avoid incorrect times. Use:
sudo hwclock --localtime
summary
He explains the importance of accurate timekeeping for servers to avoid issues like data corruption and debugging difficulties. The steps include checking the current time using the date
command, listing available time zones with timedatectl list-timezones
, and setting the desired time zone using sudo timedatectl set-timezone <your_time_zone>
.
He also discusses the timesyncd
service, that replaced the older ntpd
service for time synchronization. how to enable and ensure time synchronization using the timedatectl set-ntp on
command. He emphasizes that keeping the correct time on servers is crucial, especially for production environments.