X

How to Check and Change the Time Zone on Linux

Spread the love

Time zones are very important for the Linux system because they can affect how the system operates, as well as affecting data displayed on your website for example. In this blog post, we will explain how to check and change the time zone on your Linux server. Setting the time and date is also important as it involves setting the correct time zone. These settings can be easily checked and changed according to your needs.

Changing the timezone on a Linux server is a very straightforward process. Let’s get things done!

Prerequisites

  • A server running a recent version of Linux
  • User privileges: root or non-root user with sudo privileges

Update the System

Before we make any changes to our Linux system, we should always update the packages to the latest version available. To do that, execute the following command:

sudo apt-get update -y && sudo apt-get upgrade -y

If there’s a new kernel update, you’ll need to restart to load the new kernel.

Check the Time Zone

All Linux servers have some kind of time zone that is already set. To check the time zone, we need to use the timedatecl command. The timedatectl command in Linux is used for displaying the current time on your server. It displays the system time, the hardware clock, and the time zone configured on your system. There is additional info as output from this command about the status of the systemd-timesyncd.service and clock synchronization.

Let’s execute the timedatectl command and see the exact output:

root@host:~# timedatectl
               Local time: Sun 2024-01-14 15:57:57 CST
           Universal time: Sun 2024-01-14 21:57:57 UTC
                 RTC time: Sun 2024-01-14 21:57:57
                Time zone: America/Chicago (CST, -0600)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

The first line displays the local time, the second line is the Universal time, and the third line is the hardware clock (RTC, or Real Time Clock). Then the timezone is displayed, and as you can see on our system it’s set to America/Chicago CST (Central Standard Time). After that is the info about the synchronization, the network time protocol service, and the hardware clock in the local time zone, which in this case is disabled.

We can check the time zone if we execute the date command:

root@host:~# date
Sun Jan 14 16:12:14 CST 2024

There is also another way to check the time zone, and that way is by listing the following file:

ls -l /etc/localtime

You should get the following outptut:

root@host:~# ls -al /etc/localtime
lrwxrwxrwx 1 root root 35 Jan  14 16:14 /etc/localtime -> /usr/share/zoneinfo/America/Chicago

As you can see, the timedatectl command is easier to use and the output of that command includes more details compared to the other methods.

Change the Time Zone on Linux

In the previous step, we explained different ways for checking the time zone. Now let’s cover how to change the current time zone on Linux.

Before changing the time zone, we need to list the available time zones with the following command:

timedatectl list-timezones

You will get a long list of time zones, as listed below:

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
 .
 .
America/Anchorage
America/Anguilla
America/Antigua
America/Araguaina
America/Argentina/Buenos_Aires
America/Argentina/Catamarca
America/Argentina/ComodRivadavia
America/Argentina/Cordoba
America/Argentina/Ushuaia
 .
 .
Europe/Monaco
Europe/Moscow
Europe/Nicosia
Europe/Oslo
Europe/Paris
Europe/Podgorica
Europe/Prague
Europe/Riga
Europe/Rome
Europe/Samara
Europe/San_Marino

To change the current time zone to Europe/Monaco for example, you need to execute the following command:

timedatectl set-timezone Europe/Monaco

After executing this command, you can check the time zone to see the changes:

root@host:~# timedatectl
               Local time: Sun 2024-01-15 22:28:29 CET
           Universal time: Sun 2024-01-15 21:28:29 UTC
                 RTC time: Sun 2024-01-15 21:28:29
                Time zone: Europe/Monaco (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

As you can see the time zone was successfully changed.

Another way to change the time zone is using the tzselect command:

root@host:~# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the timezone using the Posix TZ format.
#? 

Now, you can select the continent, ocean, and coordinates, or specify the time zone using the Posix TZ format. Let’s choose number 1 for the Africa continent. You will get a list of all countries in Africa.

Please select a country whose clocks agree with yours.
 1) Algeria    13) Côte d'Ivoire     25) Iceland        37) Namibia  49) Sudan
 2) Angola    14) Djibouti      26) Kenya        38) Niger   50) Tanzania
 3) Benin    15) Egypt      27) Lesotho        39) Nigeria  51) Togo
 4) Botswana    16) Equatorial Guinea     28) Liberia        40) Rwanda  52) Tunisia
 5) Burkina Faso   17) Eritrea      29) Libya        41) Sao Tome & Principe 53) Uganda
 6) Burundi    18) Eswatini (Swaziland)  30) Madagascar       42) Senegal  54) Western Sahara
 7) Cameroon    19) Ethiopia      31) Malawi        43) Sierra Leone  55) Zambia
 8) Central African Rep.  20) Gabon      32) Mali        44) Somalia  56) Zimbabwe
 9) Chad    21) Gambia      33) Mauritania       45) South Africa
10) Comoros    22) Ghana      34) Mayotte        46) South Sudan
11) Congo (Dem. Rep.)   23) Guinea      35) Morocco        47) Spain
12) Congo (Rep.)   24) Guinea-Bissau     36) Mozambique       48) St Helena
#? 

We will choose number 45 for example to change the time zone to South Africa.

The following information has been given:

 South Africa

Therefore TZ='Africa/Johannesburg' will be used.
Selected time is now: Mon Jan 15 13:00:40 SAST 2024.
Universal Time is now: Mon Jan 15 11:00:40 UTC 2024.
Is the above information OK?
1) Yes
2) No
#? 

After that, we can confirm by typing 1 and hitting the Enter button. You will get the following screen:

You can make this change permanent for yourself by appending the line
 TZ='Africa/Johannesburg'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Africa/Johannesburg 

You should definitely add that line to your .profile file so that your time zone is remembered when you next login:

echo "TZ='Africa/Johannesburg'; export TZ" >> ~/.profile

Congratulations! You successfully changed your time zone using two different ways.

Of course, you don’t have to spend your time on this and if you have a managed Linux support plan with us. We will help you with any aspect of your server time zones, including any app-specific time zone settings. You can simply ask our support team – they are available 24/7 and will be able to help you with your time zone as well as any additional requirements that you may have.

Categories: Linux Tutorials
admin:
Related Post