Linux Setting Date, Time and Timezone

How to set date, time and timezone on Linux/Unix box will be introduced in this post.

Unix time, or POSIX time which is a system for describing points in time is the number of seconds elapsed since midnight UTC on the morning of January 1, 1970, not counting leap seconds.

The number of seconds elapsed can be got by this command on Linux/Unix systems:

$ date +%s

Set Linux date

Linux date can be set using following syntax:

# date +%Y%m%d -s "yyyymmdd"

yyyy is year, mm is month and dd is day.

For example, we can set the date to June 22, 2010 by:

# date +%Y%m%d -s "20100622"

Set Linux time

Linux time can be set using following syntax:

# date +%T -s "hh:mm:ss"

hh is hour, mm is minite and ss is second.

For example, we can set the time to 11:28 by:

# date +%T -s "11:28:00"

Set Linux date and time

The date and time can be set by date command at the same time by:

# date mmddhhmmyyyy.ss

The first mm means month while the second mm means minite.

For example, we can set the date and time to 11:28 on June 22, 2010 by:

# date 062211282010.00

Another way to set new date and time is using the following syntax:

# date --set="STRING"

The method to set the date and time above is:

# date -s "22 JUN 2010 11:28:00" 

or

# date --set="22 JUN 2010 11:28:00" 

Set Linux timezone

The configuration file for timezone is usally /etc/localtime which is often a symlink to the file localtime or to the correct time zone file in the system. The time zone directory is /usr/share/zoneinfo where you can find a list of time zone regions. In some distro such as Fedora/RHEL/Cent OS, the zone files use /usr/share/zoneinfo/REGION/CITY like format.

The method for setting Linux timezone:

Backup old timezone info if needed

# mv /etc/localtime /etc/localtime.bak

Find out the appropriate timezone from /etc/localtime and create a symbolic link to it

For example we want to set the time zone to Hong Kong time:

# ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

Verify the timezone is changed

$ date

You may get a output like this:

$ date
Tue Jun 22 12:33:10 HKT 2010

Some related utilities

Here is some related Linux time/date related utilities.

Update the current system time by rdate

# rdate -s time.nist.gov

For a list of available time servers, please check NIST Internet Time Servers.

Set the hardware clock

# /sbin/hwclock --systohc

Fedora/RHEL/CentOS have a date/time setting tool

You can use the GUI tool on Red Hat’s distros:

# system-config-date

Or try:

# setup 

and then select the timezone entry.

Eric Zhiqiang Ma

Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *