Last updated on October 12, 2020 by Dan Nanni
If the default timezone of your Linux system is not correct, you need to correct it to get accurate local time. Note that configuring NTP is not a solution for an incorrect timezone as NTP itself does not handle timezones. NTP handles time data in UTC, and actual local time is calculated by adding the local time zone offset. Thus you need to change the timezone separately yourself.
You can easily find out the current timezone of your Linux system with:
$ ls -l /etc/localtime
The first step to change the timezone of your Linux system is to obtain a timezone value (
TZ value) of your region.
For that, you can use
tzselect command, which allows you to view and choose installed timezone files.
Go through timezone selection menu to identify your zone.
tzselect will display your timezone in
Posix TZ format (e.g.,
America/New_York). This is the
TZ value of your region. Make a note of this value, and proceed next.
If you want to change timezone for a specific user only, you can define
TZ environment variable using the
TZ value you obtained earlier. Add the following in
~/.bashrc as follows.
Then re-login or execute
~/.bashrc to activate the change.
$ source ~/.bashrc
To change the timezone for all users system-wide on your Linux, use the following commands.
$ sudo rm -f /etc/localtime $ sudo ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
Note that the path to the timezone file (
/usr/share/zoneinfo/America/New_York) is constructed from the
TZ value you obtained earlier.
When the system-wide timezone is set up like this, you can still override it for particular users by defining
TZ environment variable in
~/.bashrc in their home directories.
If your Linux system uses
systemd (e.g., Ubuntu 15.04+, Debian 8+, Fedora 15+, CentOS/RHEL 7+), there is another way to change timezone system-wide, which is by using
systemd, there is a system service called
systemd-timedated whose responsibility is to adjust system clock and timezone. The
timedatectl command configures this system service.
To change the current timezone system-wide with
$ sudo timedatectl set-timezone 'America/New_York'
TZ value you obtained above.
If you want to set the default timezone in PHP, refer to this tutorial.
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.