Configuration environment
This article was written using the Slackware distribution version 15.0. Many modern Linux distributions employ the timedatectl command for date, time, and timezone settings; however, this command will not be addressed in this document. Slackware uses more traditional commands, which should work correctly in most current Linux distributions.
Introduction
The Linux timezone is defined through the /etc/localtime file, but it can be configured both globally and for a specific user using the TZ environment variable. For individual users, it can be set in the ~/.bashrc file, while for the entire system, it can be configured in /etc/profile or, in some distributions, in /etc/environment.
The date command is used to manually display or configure the date and time on Linux, while the ntpdate command uses the NTP protocol to perform a one-time time synchronization with other computers on the network through port 123/UDP. The daemon ntpd is responsible for continuously synchronizing the system clock with NTP servers.
UTC vs Localtime
UTC is the global standard time, does not account for time zones or daylight saving time, and the same UTC time is used anywhere in the world. Localtime is the specific timezone of the location where the system is being configured.
Timezone
The Linux timezone is defined using the database provided and maintained by IANA. In Slackware, the package containing this list is glibc-zoneinfo, and the zone information is organized in the /usr/share/zoneinfo/ directory by regions (such as "America", "Europe", "Asia").
$ bash
ls /usr/share/zoneinfo/
Within the regional folders are zone files named after cities (such as "Sao_Paulo", "Buenos_Aires", "Los_Angeles"). To view a specific timezone, use the command:
$ bash
zdump /usr/share/zoneinfo/America/Sao_Paulo
To change the timezone:
# bash
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
Optionally, the timeconfig command can be used in Slackware to create the above configuration. Choose the "NO" option and then find your location in the provided list. Remember to run the command as root user.
Test with the date command to see if the new timezone has been applied. If the change doesn't work, it may be because the timezone is being defined by the TZ environment variable. This variable has priority, allowing different timezones to be set per user. To manipulate the variable at runtime, use:
# bash
# Check the value of TZ echo $TZ America/Sao_Paulo # Remove the value of TZ unset TZ # Change the value of TZ export TZ=America/Los_Angeles
To make the change permanent, the TZ variable should be configured in the ~/.bashrc file. Add the following line:
~/.bashrc
export TZ=America/Sao_Paulo
This configuration is specific to the user. To reload the .bashrc file with the changes made:
$ bash
. ~/.bashrc
# or
source ~/.bashrc
To use the variable system-wide, it can be configured in /etc/profile using the same line from the .bashrc file. Alternatively, it can be set in /etc/environment, but in this file, the export
command should not be included.
/etc/environment
TZ=America/Sao_Paulo
Both /etc/profile and /etc/environment can be reloaded using the same method as the .bashrc file, or upon the next user login.
Adjusting the clock manually
Using the date command to query and change the date, time, and timezone. Remember to use the root user to change the time.
# bash
# Querying date, time, and timezone date Tue Oct 1 09:16:27 -03 2024 # Setting - year/month/day hour:minute:seconds date --set "2024/10/01 09:20:00" Tue Oct 1 09:20:00 -03 2024
In the date query, the -03 information refers to the time relative to UTC, meaning three hours behind.
Synchronizing the clock with a remote ntp server
To update the time via a server on the internet, you need the ntp package, which can be installed on Slackware using the slackpkg command.
# bash
# Synchronizing the time ntpdate pool.ntp.org 5 May 00:41:05 ntpdate[3132]: step time server 201.49.148.135 offset 6.872209 sec
In most cases, it is recommended to use pool.ntp.org to find an NTP server (or 0.pool.ntp.org, 1.pool.ntp.org, etc., if you need multiple servers). The system will automatically try to select the servers closest to you.
When running the ntpdate command, the time will be updated at the moment of execution. To keep the clock synchronized, use the ntpd service. First, edit the configuration file /etc/ntp.conf and uncomment the lines as shown below:
/etc/ntp.conf
# NTP servers, list one or more for synchronization
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
server 2.pool.ntp.org iburst
server 3.pool.ntp.org iburst
Then, grant execution permission to the script /etc/rc.d/rc.ntpd and start the service with the commands below:
# Shell
# Grant execution permission chmod +x /etc/rc.d/rc.ntpd # Start the service /etc/rc.d/rc.ntpd start
To test, manually change the date using the date command as demonstrated earlier. Note that the time will not be corrected immediately; there is a default check interval ranging from 64 seconds (minimum) to 1024 seconds (maximum).
These values can be adjusted by configuring the maxpoll and minpoll parameters. For more information, check the manual: man ntp.conf.
After changing the date, wait at least 17 minutes (1024 seconds) for the time to be automatically corrected. Alternatively, you can manually restart the service to force immediate correction using the command below:
# Shell
# Restarting the service
/etc/rc.d/rc.ntpd restart
Hardware clock
The hardware clock is an electronic device that measures the passage of time, also known as the RTC. It is the time displayed in the BIOS or UEFI of computers.
To avoid inconsistencies in dual-boot systems with different Linux installations on the same computer, it is necessary to properly configure the hardware clock, defining whether it uses localtime or UTC-based time.
This procedure should be performed only once to ensure that the timezone is correctly configured. Below is how to query the hardware clock and set the timezone:
# Shell
# Retrieving the hardware clock time
hwclock --show
# Shell
# Set the timezone as local hwclock --systohc --localtime # or hwclock -w -l
# Shell
# Set the time based on UTC hwclock --systohc --utc # or hwclock -w -u
Sources
The main source for this article was the manual (man page) available on Slackware Linux.