Changing default date and timezones on a Linux server

Assume that your current timezone is UTC. You would like to change your server’s timezone to Pacific Time. How do I change the timezone on my Linux distribution? On some distributions (for example, CentOS), the timezone is controlled by /etc/localtime file. The /etc/localtime file is a link to or copy of a file containing information about your time zone.

How to get the current date and time in the terminal?

You need to use the following syntax to print current date and time on screen:

1
date

The date syntax has many options; you can use formatting options with spaces and commas to make the date look nice like “Friday, February 14, 2014, 00:00:00 AM” get by using the date command:

1
date +"%A, %B %d, %Y, %T %p"

If the syntax above shows the wrong time or date, normally is because the system is set to the wrong timezone.

Changing the timezone, date, and time

TimeZone information files are usually in /usr/share/zoneinfo but this depends on your distribution. there are now about a dozen distributions out there (Debian, Dedora, Ubuntu, Mageia, Min Linux, Fuduntu, Red Har, Backtrack, Arch). So if your localtime file points to a zone info file that is not your time zone you can change it by browsing the directories in /usr/share/zoneinfo to find your country, then find your city or a city in the same time zone and link localtime to it. For example, all US timezones are located under under the /usr/share/zoneinfo/US directory as shown below.

1
2
ls /usr/share/zoneinfo/America/
Adak Coral_Harbour Hermosillo Moncton SantaremAnchorage Cordoba Indiana Monterrey SantiagoAnguilla Costa_Rica Indianapolis Montevideo Santo_DomingoAntigua Creston Inuvik Montreal Sao_PauloAraguaina Cuiaba Iqaluit Montserrat ScoresbysundArgentina Curacao Jamaica Nassau ShiprockAruba Danmarkshavn Jujuy New_York SitkaAsuncion Dawson Juneau Nipigon St_BarthelemyAtikokan Dawson_Creek Kentucky Nome St_JohnsAtka Denver Knox_IN Noronha St_KittsBahia Detroit Kralendijk North_Dakota St_LuciaBahia_Banderas Dominica La_Paz Ojinaga St_ThomasBarbados Edmonton Lima Panama St_VincentBelem Eirunepe Los_Angeles Pangnirtung Swift_CurrentBelize El_Salvador Louisville Paramaribo TegucigalpaBlanc-Sablon Ensenada Lower_Princes Phoenix ThuleBoa_Vista Fortaleza Maceio Port-au-Prince Thunder_BayBogota Fort_Wayne Managua Porto_Acre TijuanaBoise Glace_Bay Manaus Port_of_Spain TorontoBuenos_Aires Godthab Marigot Porto_Velho TortolaCambridge_Bay Goose_Bay Martinique Puerto_Rico VancouverCampo_Grande Grand_Turk Matamoros Rainy_River VirginCancun Grenada Mazatlan Rankin_Inlet WhitehorseCaracas Guadeloupe Mendoza Recife WinnipegCatamarca Guatemala Menominee Regina YakutatCayenne Guayaquil Merida Resolute YellowknifeCayman Guyana Metlakatla Rio_BrancoChicago Halifax Mexico_City RosarioChihuahua Havana Miquelon Santa_Isabel

For other country timezones, browse the /usr/share/zoneinfo directory.

Change TimeZone Using /etc/localtime File

First step: delete the current localtime file /etc/localtime.

1
rm /etc/localtime

Link the Los_Angeles file from the above the U.S. directory to the /etc/localtime directory as shown below.

1
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

Now the timezone on your Linux system is changed to the U.S. Los Angeles time. You can use the “date” command to verify.

Change TimeZone Using /etc/timezone File for some distribution like Ubuntu

To show the current timezone in the file /etc/timezone, use the command:

1
cat /etc/timezone

To change this to the U.S. Los Angeles time, we need to modify the /etc/timezone file. First step: edit the /etc/timezone file via vim or nano. Vi editor:

1
vi /etc/timezone

Nano editor:

1
nano /etc/timezone

Second step: modify the file as shown below:

1
America/Los_Angeles

set the timezone from the terminal using the TZ variable.

1
export TZ=America/Los_Angeles
Add multiple printers on Tomato firmware router with hotplug

Comments