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:
$ date
$ 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.
$ tzselect
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.
export TZ='America/New_York'
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.
systemd
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 timedatectl
command.
Under 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 timedatectl
:
$ sudo timedatectl set-timezone 'America/New_York'
Use the TZ
value you obtained above.
If you want to set the default timezone in PHP, refer to this tutorial.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
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.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean