4 Linux commands for managing time (and why that's important)


Jack Wallen/ZDNET

The right time helps systems communicate and ensures you can complete tasks — and with Linux, there are a few commands for managing time on your system.

Each of the following commands I’ll show you has a different purpose, but they all serve time. Although you may never use these commands, it’s good to know them, especially if you ever intend on using Linux as more than just a desktop operating system.

Also: The best Linux distros for beginners: You can do this!

Let me introduce you to the commands you need to know to work with time on Linux.

1. date

With date, you can print out or set the system time on your Linux machine. If you run the date command without any arguments, it will print out the date in the format:

Mon Jul 15 08:53:41 AM EDT 2024

You can also set the time and date with this command. The syntax of such a command would be something like this:

sudo date -s "15 July 2024 08:53:00"

You can also just set the date like this:

date +%Y%m%d -s "20240715"

Or set the time with:

2. timedatectl

The timedatectl command ensures your Linux machine is set to the right time zone. You can also use the command to set the time. If your Linux desktop or server doesn’t have the right time (by a few hours), the timezone could be improperly set. 

Also: The first 5 Linux commands every new user should learn

Thanks to timedatectl, setting the timezone isn’t all that hard. The trick is to know your timezone. You might be in America/New_York, America/Toronto, or America/Kentucky/Louisville. To find out what timezones are available, issue the command:

timedatectl list-timezones

Once you know your timezone, issue a command like this:

sudo timedatectl set-timezone America/Kentucky/Louisville

You can also set the date and time with this command like so:

timedatectl set-time 'YYYY-MM-DD HH:MM:SS'

For example:

timedatectl set-time '2024-07-15 08:53:00'

3. hwclock

Next, we have a command that can manipulate the hardware clock. The hardware clock is known as the Real-Time Clock (RTC), a battery-powered clock on your computer’s motherboard that keeps time, even when the system is powered off. Your computer’s BIOS manages the RTC.

Also: There’s a new coolest Linux distribution ready to wow you 

Should you need to change the hardware clock, Linux has a command for that purpose. 

You can view the hardware clock with the command:

If you find the hardware clock is wrong, you can set it with a command like this:

sudo hwclock --set --date="YYYY-MM-DD HH:MM:SS"

For example:

sudo hwclock --set --date="2024-07-15 09:15:00"

4. ntp

Now let’s return to timedatectl because the command includes a feature that works with Network Time Protocol (ntp) and keeps your computer in sync with a more reliable networked server. This feature does two things: it ensures your computer has the right time, and keeps you from dealing with time manually.

You can enable NTP on your system with the command:

sudo timedatectl set-ntp yes

Other tools allow you to make use of NTP. For example, you can use timesyncd, which most likely isn’t enabled by default. To enable timesyncd, issue the command:

systemctl enable --now systemd-timesyncd

Now, your system will always have the correct time. 

Also: How to enable Linux on your Chromebook

To learn more about these commands, make sure to read the man pages, with the following commands:

  • man date
  • man timedatectl
  • man hwclock

You won’t find a man page for systemd-timesyncd, but you can learn more about the command from the official Arch Linux Wiki page.





Source link