how to set or change the date and time in linux

The system time and date in Linux is important to be set correctly as it is used by many other applications and services that run on your machine. In most modern distros, it is possible to set the time and date from the graphical interface. In this post we will only discuss how to set time from the command line.

Just for brevity, in most desktop environments such as Gnome or KDE you should be able to access and set time by right clicking on the time/date applet in the status bar. You should also be able to access it from the system settings interface.

The Linux date command is a very versatile command that allows you to not only display date but also set and update both the time and the date on Linux systems.

linux date command

Let’s see how you can use the date command to display the date and time that the system is currently set to. Using date without any options will print out the current date and time in the default format.

bash$ date
Tue Oct 9 00:09:25 CDT 2012

You can control the format of the output to display the clock in almost any format you desire. There are numerous options available such as (and many more) what is described below:

  • %H : hour
  • %M : minutes
  • %S : seconds
  • %D: date equivalent to %m/%d/%y
  • %Z: timezone
  • %j: day of the year

bash$ date -u +"Time is %H:%M:%S on %D in %Z (%j day of the year)"
Time is 05:26:45 on 10/09/12 in UTC (283 day of the year)

Using the -u option (–utc or –universal) will print out the time and date in the Coordinated Universal Time (UTC).

Setting the Date

The date command can be used to modify and set the system date on the machine. The –set option (-s) allows you to do that. To set the date to Jan 12th 2012, you can do the following

bash$ date -s "12 Jan 2012"

The date command is reasonably good with different formats that it can accept as an argument when setting the date. For example, you can use this format: “2012-01-12” or “01/12/2012” or “01/12/12” etc. If you happen to have a format that is not supported, you can specify the exact format you want to use as well…

bash$ date +%Y%m%d -s "20120112"

Setting the Time

The date command can again be used to set the system time independent of the date. To set the time to 3 hours 47 minutes and 51 seconds in the evening (PM), you can use the following syntax.

bash$ date -s 15:47:51

The colon (:) acts as the default delimiter for the time. You can also set both the time and the date in a single command such as

bash$ date -s "1 Jan 2012 15:47:51"

The above command will set the current time to 15:47:51 and the date to 1st Jan 2012.

Command to set date and time in Linux

Setting the Hardware Clock

In addition to the system time and date, there is also a hardware time that is set on the machine hardware. This is the time which is maintained by your hardware with the help of a built in lithium (usually) battery regardless of the OS and other factors.

It is not uncommon to see some difference between the hardware and system clocks. Setting the system time does not change the hardware clock which needs to be set independently using the linux command hwclock.

To display the hardware clock,

bash$ hwclock --show
Tue Oct 9 01:16:34 2012 -0.653095 seconds

If there is a difference between the system time and the hardware clock, then you can easily set the hardware clock to the system clock using the –systohc option or vice versa using the –hctosys option.

bash$ hwclock --systohc

will set the hardware clock to whatever the system clock is currently set to.

bash$ hwclock --hctosys

will set the system clock to match the hardware clock.

You can also set the hardware clock to a particular date and time. It takes very similar arguments as the date command.

bash$ hwclock --set --date="1 Jan 2012 15:47:51"

While all the above methods allows you to set and modify the system date and time, if you are interested in keeping the system clock accurate and correct any errors automatically, then you should use a network/internet time server. This is done using NTP or Network Time Protocol. The NTP client can be run as a Linux daemon which will connect to a network server to update and adjust the time periodically.