Setting and Configuring Timezone in Linux: A Complete Guide
If you are a system administrator or devops engineer, then you must have the skill of setting up a time zone on a Linux server. The correct display of time for users and your applications that use this time will depend on how you set up the time on the server. Databases, logging systems and the cron task scheduler also depend on the correct time setting.
In almost all cases, setting the correct time zone on servers will ensure that data is correctly synchronized between the various components of the system on other servers, which plays an important role in maintaining data consistency and increasing the security of the entire infrastructure.
In this article, we will consider in detail several ways to set the time zone on Linux. I will conduct all examples on the Ubuntu operating system, but do not worry if you have another platform installed, for example, CentOS, Debian or Fedora, because all commands will work the same on different platforms.
To avoid problems with setting the time zone, it is best to have sudo privilege or perform all actions as the root user.
Contents of the article:
- View your current time zone.
- View available time zones.
- Setting the time zone.
1. View your current time zone.
Before setting a new time zone, let's learn what options there are for viewing the current time on a Linux server.
The first way to view the time zone is by reading the file /etc/timezone. This file contains data about the time zone on the server. We look at the file with the command:
~# cat /etc/timezone
As a result, we see that the current time zone on the server is set to "America/New_York".
The second way to view is by reading the link /etc/localtime. /etc/localtime contains a link to the time zone file /usr/share/zoneinfo. To view the link, use the command:
~# ls -l /etc/localtime
And here we see that the time zone is set to "America/New_York".
The third way to view the time zone is through the timedatectl command. The command provides complete information about the date, time, and time zone in the system.
~# timedatectl
The command returns:
- Local time - Shows the local time set in the system.
- Universal time - Shows time according to universal time (UTC).
- RTC time - Shows the hardware time on BIOS/UEFI.
- Time zone - Shows the current time zone.
- NTP service - Shows whether the clock is synchronized via NTP.
We don't need the rest now. The main thing for us is that the Time zone parameter shows exactly the same time zone as all previous viewing options, which means the time zone data is up-to-date.
2. View available time zones.
Before changing the time zone, we will need to find a suitable name for the new time zone we want to apply. To view all available time zones, we can use a simple command:
~# timedatectl list-timezones
The result is a large list with all available time zones. Each time zone follows the format "Region/City".
3. Setting the time zone.
The easiest and fastest way to set the time zone on the server is to use the same timedatectl command, but only with the addition of the set-timezone parameter and then the name of the time zone we need.
Example command to set time zone to "America/Chicago".
~# timedatectl set-timezone America/Chicago
The time zone is set on a permanent basis, it will not be the case that after rebooting the server the time zone will reset to the time zone that was set initially.
As a result, today we looked at various ways to view the current time zone on a Linux server under the Ubuntu operating system and also looked at how quickly and easily we can change the time zone to the one we need for our services.