Setup/ Change Timezone on Linux.
Timezone determines the local time of your location. If you want to change the time zone according to your own preference, Linux provides you with ways to do so; one way through the command line. :~)
Having a correct time zone configured on your Linux data center servers could mean the difference between software running properly or not and as an IT admin, you know the importance of time. Believe it or not, having the correct time on a server is crucial for certain applications and services. Without having time configured properly, you'll receive errors that give you absolutely no indication as to what is causing the problem.
Let’s jump into it!
Checking the Current Timezone
In most other Linux distributions, we can use the date
or the timedatectl
command to display and set the current system’s time and timezone.
$ date
OUTPUT
Sun 28 Mar 2021 07:17:04 AM UTC
or using the timedatectl command
$ timedatectl
OUTPUT
timedatectl
Local time: Sun 2021-03-28 07:19:51 UTC
Universal time: Sun 2021-03-28 07:19:51 UTC
RTC time: Sun 2021-03-28 07:19:52
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
As you can see in both outputs above, the timezone is set to UTC.
Set Timezone by changing the symbolic link
The configuration file for timezone is usually located at /etc/localtime which is often a symlink to the file localtime or to the correct time zone file in the system.
$ ls -l /etc/localtime
OUTPUT
lrwxrwxrwx 1 root root 27 Feb 3 17:53 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
or
$ readlink /etc/localtime
OUPUT
../usr/share/zoneinfo/Etc/UTC
The time zone directory is locate at /usr/share/zoneinfo where you can find a list of time zone regions.
$ ls /usr/share/zoneinfo
OUPUT
Africa Canada EST GMT+0 Iran Libya NZ-CHAT right Universal
America CET EST5EDT GMT-0 iso3166.tab localtime Pacific ROC US
Antarctica Chile Etc GMT0 Israel MET Poland ROK UTC
Arctic CST6CDT Europe Greenwich Jamaica Mexico Portugal Singapore WET
Asia Cuba Factory Hongkong Japan MST posix SystemV W-SU
Atlantic EET GB HST Kwajalein MST7MDT posixrules Turkey zone1970.tab
Australia Egypt GB-Eire Iceland leapseconds Navajo PRC tzdata.zi zone.tab
Brazil Eire GMT Indian leap-seconds.list NZ PST8PDT UCT Zulu
Now, the solution of changing a timezone becomes a simple exercise of changing the symbolic link from the above directory to the /etc/localtime file. You might need to use the -f
flag to force the new link and run the following command as sudo user:
$ sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
Run the date
command to verify the change:
$ date
OUTPUT
Sun 28 Mar 2021 05:25:17 PM JST
As you can see in output above, the timezone is set to JST (Japan Standard Time).
Set Timezone by Using the timedatectl Command
Before changing the timezone, you’ll need to find out the long name for the timezone you want to use. The timezone naming convention usually uses a "Region/City" format.
To list all available time zones, you can either list the files in the /usr/share/zoneinfo
directory or use the timedatectl
command.
$ timedatectl list-timezones
OUTPUT
...
America/Los_Angeles
America/Maceio
America/Managua
America/Manaus
America/Martinique
America/Matamoros
America/Mazatlan
America/Menominee
America/Merida
America/Metlakatla
America/Mexico_City
....
Once you identify which time zone is accurate to your location, run the following command as sudo user:
$ sudo timedatectl set-timezone your_time_zone
For example, to change the system’s timezone to America/Los_Angeles
:
$ sudo timedatectl set-timezone America/Los_Angeles
Run the timedatectl
command to verify the change:
$ timedatectl
OUTPUT
timedatectl
Local time: Sun 2021-03-28 01:49:00 PDT
Universal time: Sun 2021-03-28 08:49:00 UTC
RTC time: Sun 2021-03-28 08:49:01
Time zone: America/Los_Angeles (PDT, -0700)
System clock synchronized: yes
NTP service: act
As you can see in output above, the timezone is set to PDT (Pacific Daylight Time).
That’s it!! Have yourself a great day.