Nowadays, for a Linux system to be useful in any specific location, it must adapt to the local way of doing all those things. Localization is that the ability to adapt a Linux system to a selected locale. In accomplishment of this matter, there is a way the Linux system identify how to handle the characters contained in the local language. All Linux distributions can be set to use custom localisation settings. These settings include region and language related definitions like the zone , interface language also as character encoding are often modified during the installation of the operating system or anytime after that.

In this guide, we are to gain an understanding of locale setting and environmental variables in Linux.

The Time Zones

UTC, Coordinated Universal Time is the time at the prime meridian. Due to practical reasons, time zones don’t follow the precise longitudinal distance from the point of reference (the prime meridian). Instead, time zones are artificially adapted to follow the borders of nations or other significant subdivisions.

Also, a connected machine are often accessed from different parts of the planet , so it’s an honest practice to line the hardware clock to UTC (the GMT+0 time zone) and leave the selection of the zone to every particular case. Cloud services, for instance , are commonly configured to use UTC, because it can help to mitigate occasional inconsistencies between civil time and time at clients or at other servers. In contrast, users who open a foreign session on the server might want to use their civil time zone. Thus, it’ll be up to the OS to line up the right zone consistent with each case.

The date command is used to display current time zone configuration;

$ date
Mon 24 May 2021 10:43:07 AM EAT

timedatectl command shows system date and time in details;

$ timedatectl
               Local time: Mon 2021-05-24 10:49:27 EAT
           Universal time: Mon 2021-05-24 07:49:27 UTC
                 RTC time: Mon 2021-05-24 07:49:28    
                Time zone: Africa/Nairobi (EAT, +0300)
System clock synchronized: yes                        
              NTP service: active                     
          RTC in local TZ: no

In the above output, it shows that in the Time zone entry, time zone names based on localities, like Africa/Nairobi, are also accepted. The default time zone for the system is kept in the file /etc/timezone, either by the zone’s full descriptive name or offset.

Checking the default time zone;

$ cat /etc/timezone
Africa/Nairobi

Zones can have more than one name, therefore making it difficult to remember when when setting the time zone. tzselect command offers an interactive method which will guide the user towards the correct time zone definition.

Using tzselect command to identify the time zone;

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

Option 1 is for Africa locations. The next step is to choose the country:

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

In some countries locality names are not available, option 25 is for country Kenya. The given information will then be used by tzselect to display the corresponding time zone:

The following information has been given:

	Kenya

Therefore TZ='Africa/Nairobi' will be used.
Selected time is now:	Mon 24 May 2021 11:13:28 AM EAT.
Universal Time is now:	Mon 24 May 2021 08:13:28 AM UTC.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
	TZ='Africa/Nairobi'; 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/Nairobi

The resulting time zone name, Africa/Nairobi, can also be used as the content of the /etc/timezone to inform the default time zone for the system:

$ cat /etc/timezone
Africa/Nairobi

The provide results of tzselect command above shows that the environment variable TZ defines the zone for the shell session, regardless of the system’s default zone is. Adding the line TZ='Africa/Nairobi' will export TZ to the file ~/.profile and make Africa/Nairobi the time zone for the user’s next sessions. The TZ variable also can be temporarily modified during the present session, so as to display the time during a different time zone:

$ env TZ='Africa/Nairobi' date
Mon 24 May 2021 11:28:56 AM EAT

In the example above, command env will run the given command in a new sub-shell session with the same environment variables of the current session, except for the variable TZ, modified by the argument TZ='Africa/Nairobi'.

The Daylight Savings Time

Many regions adopt daylight savings time for a part of the year and when clocks are typically adjusted by an hour it would lead a misconfigured system to report the wrong time during that season of the year.

The /etc/localtime file contains the info employed by the OS to regulate its clock accordingly. Standard Linux systems have files for all time zones within the directory /usr/share/zoneinfo/, so /etc/localtime is simply a symbolic link to the particular file inside that directory.

The files in /usr/share/zoneinfo/ are organized by the name of the corresponding zone , therefore the file for the Africa/Nairobi zone are going to be /usr/share/zoneinfo/Africa/Nairobi. As the definitions for the daylight savings time may change, it’s important to stay the files in /usr/share/zoneinfo/ up so far . The upgrade command of the package management tool provided by the distribution should update them whenever a replacement version is out there .

The Character Sets

At their core, computers work with ones and zeros, and Linux is no different. However, for a computer to interact with humans, it needs to know how to speak our language. This is where character sets come in. A character set defines a typical code wont to interpret and display characters during a language. Quite a few different character sets are used in the world for representing characters. The most common ones are as follows:

  • ASCII: The American Standard Code for Information Interchange (ASCII) uses 7 bits to store characters found in the English language.
  • ISO-8859: The International Organization for Standardization (ISO) worked with the International Electrotechnical Commission (IEC) to supply a series of ordinary codes for handling international characters. There are 15 separate standards (ISO-8859-1 through ISO-8859 15) for defining different character sets.
  • Unicode: The Unicode Consortium, composed of many computing industry companies, created an international standard that uses a 3-byte code and can represent every character known to be in use in all countries of the world.
  • UTF: The Unicode Transformation Format (UTF) transforms the long Unicode values into either 1-byte (UTF-8) or 2-byte (UTF-16) simplified codes. For work in English speaking countries, the UTF-8 character set is replacing ASCII as the standard.

Environment Variables

Linux stores locale information during a special set of environment variables. Programs that require to work out the locale of the Linux system just got to retrieve the acceptable environment variable to ascertain what list to use. Linux provides the locale command to assist you easily display these environment variables.

Checking the Linux locale environment variables;

$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=

The above output is explained below;

  • LC_COLLATE: Sets the alphabetical ordering. One of its purposes is to define the order files and directories are listed.
  • LC_CTYPE: Sets how the system will treat certain sets of characters. It defines, for example, which characters to consider as uppercase or lowercase.
  • LC_MESSAGES: Sets the language to display program messages (mostly GNU programs).
  • LC_MONETARY: Sets the money unit and currency format.
  • LC_NUMERIC: Sets the numerical format for non-monetary values. Its main purpose is to define the thousand and decimal separators.
  • LC_TIME: Sets the time and date format.
  • LC_PAPER: Sets the standard paper size.
  • LC_ALL: Overrides all other variables, including LANG.

The output of the locale command defines the localization information in the format language_country.character set.In the example above the Linux system is configured for United States English, using the UTF-8 character set to store characters. The LC_ environment variables themselves each represent a category of more environment variables that relate to the locale settings.

Displaying Detailed Setting of LC_Environmental Variables

You can explore the environment variables contained within a category by using the -ck option, along with the category name.

The detailed settings for the LC_MESSAGES localization category;

$ locale -ck LC_MESSAGES
LC_MESSAGES
yesexpr="^[+1yY]"
noexpr="^[-0nN]"
yesstr="yes"
nostr="no"
messages-codeset="UTF-8"

The environment variables shown above control what characters and formats are used for representing messages values. Programmers can fine-tune each of the individual environment variables to customize exactly how their programs behave within the locale.

Setting Your Locale

There are components to how Linux handles localization. A locale defines the language, the country, and therefore the list the system uses. Linux provides a few different ways for you to change each of these localization settings.

Changing Your Locale

After you’ve already installed the Linux operating system, you can still change the localization values that the system uses. Two methods are available that let you do that;

  • Setting the LC_ environment variables manually.
  • Using the localectl command.

Manually Changing the Environment Variables

For the manual method, change the individual LC_ localization environment variables just as you would any other environment variable by using the export command;

$ export LC_MESSEGES=en_GB.UTF-8

The above command works well for changing individual settings, but doing so would be tedious if you wanted to change the entire localization for the system.

Instead of having to change all of the LC_ environment variables individually, the LANG environment variable controls all of them at one place;

$ export LANG=en_GB.UTF-8

This method changes the localization for your current login session. If you need to permanently change the localization, you’ll need to add the export command to the .bashrc file in your $HOME folder so that it runs each time you log in.

Using The localectl Command

Linux distribution that utilizes the systemd set of utilities have the localectl command available. By default, the localectl command just displays the current localization settings.

$ localectl
   System Locale: LANG=en_US.UTF-8
       VC Keymap: n/a
      X11 Layout: us
       X11 Model: pc105

The localectl command supports many options, but the most common are to list all of the locales installed on your system with the list-locales option.

Changing the localization by using the set-locale options;

$ localectl set-locale LANG=en_GB.utf8
==== AUTHENTICATING FOR org.freedesktop.locale1.set-locale ===
Authentication is required to set the system locale.
Authenticating as: frank,,, (frank)
Password: 
==== AUTHENTICATION COMPLETE ===

Encoding Conversion

Sometimes text may appear with unintelligible characters when displayed on a system with a personality encoding configuration apart from the system where the text was created. The command iconv are often wont to solve this issue, by converting the file from its original character encoding to the specified one. for instance , to convert a file named original.txt from the ISO-8859-1 encoding to the file named new.txt using UTF-8 encoding.

example;

$ iconv -f ISO-8859-1 -t UTF-8 original.txt > new.txt

The option -f ISO-8859-1 (or --from-code=ISO-8859-1) sets the encoding of the original file and option -t UTF-8 (or --to-code=UTF-8) sets the encoding for the converted file. All encoding supported by command iconv are listed with command iconv -l or iconv --list. Instead of using the output redirection, like in the example, option -o new.txt or --output new.txt could also be used.

Conclusion

This mark the end of guide on Working with Locale Settings and Environment Variables in Linux. We hope this guide have been helpful, more LPIC2 guides are coming don’t miss.

Similar guides;

LPIC 102 – Managing Linux System Date and Time with UTC…