When I was tweaking one of my linux server, I found out that the time of my server isn’t match with my local time. It’s hard to read logs with different time zone from your server, so I decided to change the timezone of my server to the my current timezone. I believe it was set with EST time. It would be easier for me to manage my server if it has the same time and date. I will show you how to change date and time on centos 6 operating system in this article. This article will properly work with other Linux distributions like Ubuntu, Linux Mint, Fedora or Redhat since the way Linux date, time, and timezone work all the same in Linux server.
There are a few different way to change date, time, and timezone on centos 6 via the command line terminal. Linux system normally keep their timezone in /etc/localtime. To check or show the current date, and time of your Linux system, you can use date command. Date command can print, or set date and time of your linux system.
Display Date and Time in Linux
and its output
Sat Mar 15 03:14:29 PDT 2014
Change Timezone in Linux
Fist, we are going to get a list of timezone that came with Linux.
# cd /usr/share/zoneinfo/ # ls
Africa Canada GB Indian Mexico ROK iso3166.tab America Chile GB-Eire Iran NZ Singapore posix Antarctica Cuba GMT Israel NZ-CHAT Turkey posixrules Arctic EET GMT+0 Jamaica Navajo UCT right Asia EST GMT-0 Japan PRC US zone.tab Atlantic EST5EDT GMT0 Kwajalein PST8PDT UTC Australia Egypt Greenwich Libya Pacific Universal Brazil Eire HST MET Poland W-SU CET Etc Hongkong MST Portugal WET CST6CDT Europe Iceland MST7MDT ROC Zulu
There are countries, or continent with different timezone, for example when you list all file and directories in zoneinfo directory as I did above, there are timezone for countries with only one timezone, and directories for countries and continent with several timezones. For example my timezone is America Pacific, I will need to go to America directory to get my timezone.
# cd /usr/share/zoneinfo/America/ # ls
Adak Coral_Harbour Hermosillo Moncton Santarem Anchorage Cordoba Indiana Monterrey Santiago Anguilla Costa_Rica Indianapolis Montevideo Santo_Domingo Antigua Creston Inuvik Montreal Sao_Paulo Araguaina Cuiaba Iqaluit Montserrat Scoresbysund Argentina Curacao Jamaica Nassau Shiprock Aruba Danmarkshavn Jujuy New_York Sitka Asuncion Dawson Juneau Nipigon St_Barthelemy Atikokan Dawson_Creek Kentucky Nome St_Johns Atka Denver Knox_IN Noronha St_Kitts Bahia Detroit Kralendijk North_Dakota St_Lucia Bahia_Banderas Dominica La_Paz Ojinaga St_Thomas Barbados Edmonton Lima Panama St_Vincent Belem Eirunepe Los_Angeles Pangnirtung Swift_Current Belize El_Salvador Louisville Paramaribo Tegucigalpa Blanc-Sablon Ensenada Lower_Princes Phoenix Thule Boa_Vista Fort_Wayne Maceio Port-au-Prince Thunder_Bay Bogota Fortaleza Managua Port_of_Spain Tijuana Boise Glace_Bay Manaus Porto_Acre Toronto Buenos_Aires Godthab Marigot Porto_Velho Tortola Cambridge_Bay Goose_Bay Martinique Puerto_Rico Vancouver Campo_Grande Grand_Turk Matamoros Rainy_River Virgin Cancun Grenada Mazatlan Rankin_Inlet Whitehorse Caracas Guadeloupe Mendoza Recife Winnipeg Catamarca Guatemala Menominee Regina Yakutat Cayenne Guayaquil Merida Resolute Yellowknife Cayman Guyana Metlakatla Rio_Branco Chicago Halifax Mexico_City Rosario Chihuahua Havana Miquelon Santa_Isabel
I will use Toronto for Pacific time, so I will need to copy/set Toronto file as my localtime on my server (this will work with any linux system I believe).
# cp /usr/share/zoneinfo/America/Toronto /etc/localtime
Change Date and Time in Linux
Now your Linux system should have the timezone that you want. If the date or time is not correct, you can set date and time with date command.
# date date MMDDhhmmYYYY
- MM: two digit month number
- DD: two digit date
- hh: two digit hour (24 hour system)
- mm: two digit minute
- YYYY: four digit of year
You can also sync your system time and date with ntp (Network Time Protocol) over a network or internet. Normally ntp should be installed to your Linux system as default, in case your system does not have ntp, you can easily install ntp package with a simple command.
To install ntp on Ubuntu, Linux or debian based distribitions
# apt-get update && apt-get install ntp
To install ntp on Fedora, Centos or redhat based distribitions
# yum install ntp
After you set your timezone correcly, you should be able to synce your date and time with ntp command
# ntpdate pool.ntp.org
Note: if you have a VPS (virtual private server) and not a real dedicated server, you will properl won’t be able to adjust time on your VPS. When ou are on VPS, you do not have the right to modify or update time and date, only the VPS owner or provider have the rights to adjust time and date on that VPS node.
You will properly get one of this error if you are on a VPS.
- Can’t adjust the time of day: Operation not permitted
- rdate: could not set system time: Operation not permitted
- ntpdate: Can’t adjust the time of day: Operation not permitted