How to Check and Change the System Time Using the Linux date Command

In a graphical operating system, you simply look at the clock in the bottom corner of your screen to check the time. On a headless Linux server, verifying the system time is a critical diagnostic step. If your server’s internal clock drifts by even a few minutes, SSL certificates will fail to validate, database replications will break, and automated cron jobs will execute at the wrong time. To check, format, and manually adjust the system clock from the terminal, you use the date command.

How to Check the Current System Time

Checking the time is the simplest command you can run in Linux.

  1. Open your terminal.
  2. Type the command: date
  3. Press Enter.

The system will output the full timestamp, including the day of the week, month, day, time (in military format), timezone, and year (e.g., Thu Oct 26 14:30:00 UTC 2023). It is critical to note the timezone listed (e.g., UTC, EST, PST), as many servers default to UTC (Coordinated Universal Time) regardless of their physical location.

Formatting the Output

If you are writing a bash script to automatically backup files and you want to append the current date to the filename, the default output of the date command is too long and contains spaces (which breaks filenames). You can use formatting flags to force the command to output only the specific data you need.

To format the output, you must append a plus sign (+) followed by specific variables:

  • Just the Date (YYYY-MM-DD): date +"%Y-%m-%d" (Outputs: 2023-10-26)
  • Just the Time (HH:MM:SS): date +"%H:%M:%S" (Outputs: 14:30:00)
  • Combined for a Filename: date +"%Y%m%d_%H%M%S" (Outputs: 20231026_143000)

You can then use this in a script variable like so: backup_file="database_$(date +"%Y%m%d").sql"

How to Manually Change the System Time

Most modern Linux servers use NTP (Network Time Protocol) or systemd-timesyncd to automatically sync their clocks with atomic clocks on the internet. However, if your server is completely isolated from the internet (air-gapped), you may need to set the time manually.

To change the time, you must use sudo privileges and pass the -s (set) flag, followed by a string containing the new date and time.

  1. Type the command: sudo date -s "2023-10-26 15:45:00"
  2. Press Enter.

The system will instantly update its internal clock and echo the new time back to you for confirmation. Note: If NTP is running in the background, it will eventually overwrite your manual change. You must disable NTP if you intend for the manual time to be permanent.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.