Setting the System Time and Time Zone on Linux

-Debian 7

 

Linux provides commands that let you set the current system date and time and the system time zone.

Setting the Current System Date and Time

 

To display the current system time, enter the date command:

debian:~# date 

Fri Jul 30 02:28:22 PDT 1999

 
 
To set the current system time, use the following form of the date command:
date 
MMDDhhmm[
CC]
YY[.
ss]
The parts of the command argument have the following meanings:
MM
A two-digit month, 01-12.
DD
A two-digit day of month, 01-31.
hh
A two-digit hour, 00-24.
mm
A two-digit minute, 00-59.
CC
An optional two-digit century; for example, 19 or 20.
YY
A two-digit year; for example, 99 or 00.
ss
An optional two-digit second, 00-59.
The command displays the time you enter and then sets the system time:

debian:~# date 073010411999 

Fri Jul 30 10:41:00 PDT 1999

 
When you power down or reboot your system, the system date and time will revert to the values held in non-volatile (CMOS) memory. To store the Linux date and time in CMOS, issue the following command:
hwclock --systohc
If you set your clock to UTC rather than local time, issue the command:
hwclock --systohc --utc

Setting the Time Zone

 

To set the time zone, use the tzconfig command. The command initiates a dialog that displays the current time zone and asks if you want to change it. If you reply yes, the command prompts you to choose from a list of geographical areas and then cities. Based on your choices, the command sets and displays the current time zone. A typical dialog resembles the following:
 

debian:~# dpkg-reconfigure tzdata

Your current time zone is set to US/Pacific
Do you want to change that? [n]: 

y

Please enter the number of the geographic area in which you live:

      1) Africa                     7) Australia
      2) America                    8) Europe
      3) US time zones              9) Indian Ocean
      4) Canada time zones          10) Pacific Ocean
      5) Asia                       11) Use System V style time zones
      6) Atlantic Ocean             12) None of the above

Then you will be shown a list of cities which represent the time zone
in which they are located. You should choose a city in your time zone.
 
=========================================================================== 
  

-Centos 6

 

#vim /etc/sysconfig/clock (check current time zone)

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

# sudo cp -p /usr/share/zoneinfo/Asia/Phnom_Penh /etc/localtime (for cambodia time)

# sudo cp -p /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime (for Hong Kong time)

Once ntp is installed synchronize your computer clock with:

# ntpdate 0.centos.pool.ntp.org


You are not limited to the above server. There are numerous NTP servers around 
the world. You can find a complete list at ntp.org.

Also keep in mind that ntp only affects the system time. The hardware clock on 
your server will not reflect that. So you want to set it as well so that 

the correct time is maintained after reboot: 

 #hwclock --systohc (set hard clock) 

#hwclock --show (show hard clock)