This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has
sudoaccess and that you precede each of the privileged commands with
Sometimes selecting the right timezone becomes very important as some task are highly dependent on time usually such tasks which include logging. using timestamps. Since these timestamps are based on system time, it becomes important to have the correct time zone configuration.
The system’s timezone is set during the installation, but it can be easily changed at a later time . The following tutorial shows the procedure for setting or changing the timezone in a Debian 9 environment.
Check Current Timezone
Type in the following command which allows to display as well as change the current time and timezone
[email protected]:~# timedatectl Local time: Mon 2019-07-01 20:34:15 UTC Universal time: Mon 2019-07-01 20:34:15 UTC RTC time: Mon 2019-07-01 20:34:16 Time zone: Etc/UTC (PKT, +0000) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no
As shown on the output below, the system’s timezone is set to “UTC”:
In Debian systems, the timezone is changed by simlinking the timezone directory /etc/timezone to a timezone identifier, which are located in /usr/share/zoneinfo directory.
Change Current Timezone
The timezone in debian systems follows the following format: Region/City
To list all the time zones that are stored in the timezones directory open terminal and typein the following commands
[email protected]:~# timedatectl list-timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Bamako Africa/Bangui Africa/Banjul Africa/Bissau Africa/Blantyre Africa/Brazzaville Africa/Bujumbura Africa/Cairo Africa/Casablanca Africa/Ceuta Africa/Conakry Africa/Dakar Africa/Dar_es_Salaam Africa/Djibouti Africa/Douala ...
Find your desired timezone from the list and then type in the following command in terminal
timezone set-timezone My_Region/My_City
For Example, I want to set the timezone to Asia/Karachi, I would run the following command
timezone set-timezone Asia/Karachi
Verify the change by typing in timedatectl again
[email protected]:~# timedatectl Local time: Mon 2019-07-01 21:24:20 PKT Universal time: Mon 2019-07-01 16:24:20 UTC RTC time: Mon 2019-07-01 16:24:21 Time zone: Asia/Karachi (PKT, +0500) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no
Older Versions Of Debian
Some older versions of debian do not have timedatectl command. In this case we need to symlink the systems directory to the timezone directory. This can be achieved by following commands
Step One: Deleting the current timezone file
rm -f /etc/localtime
Step Two: Create a symlink as shown
ln -s /usr/share/zoneinfo/My_Region/My_city /etc/localtime
For Example: In my case it is Asia/Karachi, so the symlink would be
ln -s /usr/share/zoneinfo/Asia/Karachi /etc/localtime
Step Three: Verify the listing the directory contents of /etc/locatime file using the following command
[email protected]:~# ls -l /etc/localtime lrwxrwxrwx 1 root root 34 Jun 26 01:45 /etc/localtime -> ../usr/share/zoneinfo/Asia/Karachi
Alternatively you can also verify using the date command
[email protected]:~# date Mon Jul 1 21:26:15 PKT 2019