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 | ls /usr/share/zoneinfo/America/ |
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 |
Comments