On Linux, it is essential to have some basics when it comes to changing the timezone.
The timezone is a parameter set at the installation stage and that determines the current date for your host.
Setting a correct timezone is crucial : your web and application servers may rely on this date for deserializing for example.
As a consequence, you want to set the timezone to an information that reflects the timezone you are currently located in.
On Debian 10, there are three ways to change the timezone :
- By using the timedatectl command with the set-timezone option
- By creating a symbolic link from your /etc/localtime to the correct timezone.
- By using the TZ environment variable
In order to change your timezone on Debian 10, you will need to have sudo privileges on your host.
Change the timezone using timedatectl
The quickest way to modify the timezone is to use the timedatectl command.
Showing your current timezone
To see the current timezone used on your host, run the following command
Note that it is equivalent to run the command with the status option.
$ timedatectl status
As you can see, you are presented with the local time (the time in your timezone), the universal time, the RTC time but most importantly the time zone.
Currently, my timezone is set to America/New York.
In order to see all the timezones available on your distribution, run the following command
$ timedatectl list-timezones
If you already have an idea of the timezone you want to select, you can narrow down your results to pinpoint the exact matching timezone.
$ timedatectl list-timezones | grep Paris
Setting a new timezone
In order to set the current timezone, use the set-timezone option with the timezone as an argument.
$ timedatectl set-timezone Europe/Paris
No need to restart any services, your timezone will be changed immediately.
To verify it, you can the timedatectl command again, or a simple date command.
$ timedatectl $ date
The second method to change to timezone is to create a symbolic link from your /etc/localtime to predefined timezones available on your host.
This is an old method, and you should be fine by simply running timedatectl. But in case you need to create a symbolic link, here is how you would do it.
By default, timezones are stored in the /usr/share/zoneinfo directory of your host.
Here is for example the content of the Europe folder
You will be able to change your timezone by pointing your localdate file to one of those files.
To do it, run the following command
$ sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
To verify that your changes were correctly applied, make sure to run the date command.
Change the timezone using the TZ environment variable
First of all, in order to determine the timezone we are interested in, we are going to use the tzselect command.
The tzselect command is an interactive tool that guides the user towards the timezone syntax to use on its .profile file.
It means that the tzselect won’t modify the timezone, but it will provide you with the commands you have to write to modify it.
Select the timezone you are interested in by typing the number corresponding to the entry.
From there, you will be presented with a list of countries. Choose the one you are interested in.
If you agree with the information shown in the terminal, simply hit “1” and press Enter.
As specified, append the following line to your .profile file.
echo "TZ='Europe/Paris'; export TZ" >> /home/<user>/.profile
Verify that your changes were correctly appended to your .profile file.
$ tail -n 1 /home/<user>/.profile TZ='Europe/Paris'; export TZ
Log out from your account and log in again for your changes to be taken into account.
On the next login, verify that the TZ environment variable is correctly set
$ echo $TZ Europe/Paris $ date Wed 18 Sep 2019 11:25:36 PM CEST
Awesome, you have successfully changed the timezone on your Debian 10 Buster instance.
Congratulations, you have learnt how you can easily change your timezone on Debian 10 Buster.
If you are interested in more tutorials related to system administration and server setup, make sure to read all the articles we have written on the subject.