Skip to content

Localtime file on Linux (defines the timezone)

The localtime file on Linux is used to define the time zone zone and thereby change the system clock to local time.

On Red Hat-based systems (Red Hat, CentOS, Fedora, etc.), to change a time zone, you must delete the existing symbolic /etc/localtime link, and create a new link with the desired time zone file:

# rm -f /etc/localtime # ln -s /usr/share/ZoneInfo/America/Sao_Paulo /etc/localtime

Or more simply, use the “-sf” option of the ln command, which deletes the previous symbolic link when creating the new one:

# ln -sf /usr/shave/zoneinfo/America/Sao_Paulo /etc/localtime

In this example, the São Paulo time zone will be used by the system.

Once the zone is changed, the watch automatically adjusts to the new time.

/etc/timezone

In Debian-based distributions (Debian, Linux Mint, Ubuntu, etc.), the time zone configuration is controlled through the /etc/timezone file, which contains the Information about which Timezone is. See the example:

$ cat /etc/timezone America/Los_Angeles

The text to be placed in the /etc/timezone file must match the name of the zone contained in the /usr/share/zoneinfo/ directory:

# echo “America/Sao_Paulo” > /etc/timezone

After changing the content of the /etc/timezone file, it is necessary to reconfigure the tzdata package:

# dpkg-reconfigure -f noninteractive tzdata

TZ variable

The TZ variable can also be used to define the logged-in user’s time zone and works for any Linux.

If this variable is not configured, information from /etc/localtime or /etc/timezone will be used.

This variable is useful for configuring Time Zone information per user. The system can have a time zone, and each connected user can have their own zone when logged in. This can be useful in a server environment with users spread over several zone zones.

See the example:

$ TZ='America/Miami'; export TZ
$ date Sat, Oct 19, 2019 2:37:07 PM America
$ TZ='America/Sao_Paulo'; export TZ
$ date Sat, Oct 19, 2019 11:38:01 AM -03

Like any environment variable, to work for programs running in the shell, it must be exported with the “export” command.

In addition, for the timezone configuration using the TZ variable to be permanent, it must be configured in a user initialization file, preferably in the ~/.profile file.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share