Linux Set Date and Time From a Shell Command Prompt

Q. The time on my server set back 1 hour and all my cron jobs are running 1 hour off. How can I set the server date and time from the shell command prompt? I can only login over ssh session using Putty. Please advice?

A. You will use date command to display the current date and time or set the system date / time over ssh session.

This is useful if the Linux server date and/or time is wrong, you need to set it to new values from the shell command. You must log in as root user to input date command.

Linux Date Help command

You must know specific date command to perform date/time adjustment on your server. Use this command to display a list of date/time commands:

$ date –h

OR

$ date –help

Linux Set Date

Use the following syntax to set new data and time:
date set=”DATE STRING”

For example, to set new date to March 22 2010 15:00:00, type the following command:

$ date +%Y%m%d -s “20100322”

Linux Set Time

Use the following syntax to set time for your server:

$ date +%T -s “05:15:15”

Where,

  • 05: Hour (hh)
  • 15: Minute (mm)
  • 15: Second (ss)

Use %p locale’s equivalent of either AM or PM, enter the following syntax w/o space in the time string:

$ date +%T%p -s “8:20:50AM”
$ date +%T%p -s “01:40:30PM”