10/01/2009

How to set the Linux date and time from the command prompt


Note: This is a repost of an old website page. It has just been moved it here as a blog.

The Linux machine maintains the date and time, firstly, like all other personal computers on the hardware, this is sometimes referred to as the Basic Input and Output System (BIOS) or Complementary Metal Oxide Semiconductor (CMOS). The second date and time reference is maintained by the operating system, and is updated from the hardware clock during boot-up. During the Linux installation, the hardware clock is configured to be in Coordinated Universal Time (UTC) or Greenwich Meridian Time (GMT) The latter is often referred to as the system clock and the former is usually known as the hardware clock.

During boot-up the system clock is initialised with the date and time on the hardware clock. The advantage of maintaining time in UTC is that the Linux system will automatically account for daylight savings based on your timezone. The timezone information for the Linux box is configured via the /etc/localtime file.

The man page for the hwclock command provides more detailed information on the time keeping features of Linux. The file /etc/localtime is a symbolic link to the timezone data found under the /usr/share/zoneinfo directory.

  1. To set the timezone, create the appropriate symbolic link
    • /bin/ln -sf ../usr/share/zoneinfo/Pacific/Fiji /etc/localtime
  2. To indicate to the system that UTC time is to be used, the configuration file, /etc/sysconfig/clock needs to be edited. The file should be edited to indicate UTC=true. To use GMT ensure that the file has UTC=false set. 
    • /bin/vi /etc/sysconfig/clock
  3. To set the system clock, the date command may be used. To set the date and time enter the following command. If the UTC time is used then the -u, --utc or --universal switch must be used to set the time as UTC. 
    • /bin/date MMDDhhmm.ss
  4. Once the system clock has been set, it can be used to re-initialise the hardware clock. Again if the UTC format is used then the -u or --utc switch must be used.
    • /sbin/hwclock --systohc

Refrences:

  1. Linux man page for date
  2. Linux man page for hwclock

No comments:

Post a Comment