Chapter 9
Adjusting Localization Options

  • Objective 1.6: Given a scenario, configure localization options

images Linux has become a worldwide phenomenon. You’ll find Linux desktops and servers all over the world, in many different kinds of environments. However, because of its worldwide popularity, Linux must support a wide variety of languages, date and time formats, and monetary formats.

This chapter walks through how to configure your Linux system to blend in with the local environment where it’s running. First, the chapter discusses how Linux handles different language characters, including how it formats monetary values. Then it moves on to how Linux handles times and dates as used in different countries.

Understanding Localization

The world is full of different languages. Not only does each country have its own language (or sometimes, sets of languages), each country has its own way for people to write numerical values, monetary values, and the time and date. 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 the ability to adapt a Linux system to a specific locale. To accomplish this, the Linux system must have a way to identify how to handle the characters contained in the local language. The following sections discuss just how Linux does that.

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 standard code used to interpret and display characters in a language. There are quite a few different character sets used in the world for representing characters. Here are the most common ones you’ll run into (and the ones you’ll see on the Linux+ exam):

  • ASCII: The American Standard Code for Information Interchange (ASCII) uses 7 bits to store characters found in the English language.
  • Unicode: 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), which 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.

Once you’ve decided on a character set for your Linux system, you’ll need to know how to configure your Linux system to use it, which is shown in the following section.

Environment Variables

Linux stores locale information in a special set of environment variables (see Chapter 25). Programs that need to determine the locale of the Linux system just need to retrieve the appropriate environment variable to see what character set to use.

Linux provides the locale command to help you easily display these environment variables. Listing 9.1 shows the locale environment variables as set on a CentOS system installed in the United States.

Listing 9.1: The Linux locale environment variables

$ locale
LANG=en_US.UTF-8
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 output of the locale command defines the localization information in this format:

language_country.character set

In the example shown in Listing 9.1, the Linux system is configured for United States English, using the UTF-8 character set to store characters.

Each LC_ environment variable itself represents a category of more environment variables that relate to the locale settings. You can explore the environment variables contained within a category by using the -ck option, along with the category name, as shown in Listing 9.2.

Listing 9.2: The detailed settings for the LC_MONETARY localization category

$ locale -ck LC_MONETARY
LC_MONETARY
int_curr_symbol="USD "
currency_symbol="$"
mon_decimal_point="."
mon_thousands_sep=","
mon_grouping=3;3
positive_sign=""
negative_sign="-"
. . .
monetary-decimal-point-wc=46
monetary-thousands-sep-wc=44
monetary-codeset="UTF-8"
$

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

Setting Your Locale

As shown in Listing 9.1, there are three components to how Linux handles localization. A locale defines the language, the country, and the character set the system uses. Linux provides a few different ways for you to change each of these localization settings.

Installation Locale Decisions

When you first install the Linux operating system, one of the prompts available during the install process is for the default system language. Figure 9.1 shows the prompt from a CentOS 7 installation.

When you select a language from the menu, the Linux installation script automatically sets the localization environment variables appropriately for that country and language to include the character set required to represent the required characters. Often that’s all you need to do to set up your Linux system to operate correctly in your locale.

Changing Your Locale

After you’ve already installed the Linux operating system, you can still change the localization values that the system uses. There are two methods available to do that. You can manually set the LC_ environment variables, or you can use the localectl command.

The figure shows a snapshot of the prompt from a CentOS 7 installation.

Figure 9.1 The language option in a CentOS installation

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_MONETARY=en_GB.UTF-8

That works well for changing individual settings, but it would be tedious if you wanted to change all the localization settings 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
$ locale
LANG=en_GB.UTF-8
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=
$

Some Linux systems require that you also set the LC_ALL environment variable, so it’s usually a good idea to set that along with the LANG environment variable.

images 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 it runs each time you log in.

The localectl command

If you’re using a Linux distribution that utilizes the systemd set of utilities (see Chapter 6), you 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: us
      X11 Layout: us
$

Not only does it show the LANG environment variable setting, it also shows the keyboard layout mapping as well as the X11 graphical environment layout.

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 and to change the localization by using the set-locale option:

$ localectl set-locale LANG=en_GB.utf8

That makes for an easy way to change the localization settings for your entire Linux system.

Looking at Time

The date and time associated with a Linux system are crucial to the proper operation of the system. Linux uses the date and time to keep track of running processes, to know when to start or stop jobs, and in logging important events that occur. Having your Linux system coordinated with the correct time and date for your location is a must.

Linux handles the time as two parts—the time zone associated with the location of the system and the actual time and date within that time zone. The following sections walk through how to change both values.

Working with Time Zones

One of the most important aspects of time is the time zone. Each country selects one or more time zones, or offsets from the standard Coordinated Universal Time (UTC) time, to determine time within the country. If your Linux environment includes having servers located in different time zones, knowing how to set the proper time zone is a must.

Most Debian-based Linux systems define the local time zone in the /etc/timezone file, while most Red Hat–based Linux systems use /etc/localtime. These files are not in a text format, so you can’t simply edit the /etc/timezone or /etc/localtime file to view or change your time zone. Instead, you must copy a template file stored in the /usr/share/zoneinfo folder.

To determine the current time zone setting for your Linux system, use the date command, with no options:

$ date
Fri Oct  5 21:15:33 EDT 2018
$

The time zone appears as the standard three-letter code at the end of the date and time display, before the year.

To change the time zone for a Linux system, just copy or link the appropriate time zone template file from the /usr/share/zoneinfo folder to the /etc/timezone or /etc/localtime location. The /usr/share/zoneinfo folder is divided into subfolders based on location. Each location folder may also be subdivided into more detailed location folders. Eventually, you’ll see a time zone template file associated with your specific time zone, such as /usr/share/zoneinfo/US/Eastern.

Before you can copy the new time zone file, you’ll need to remove the original timezone or localtime file:

# mv /etc/localtime /etc/localtime.bak
# ln -s /usr/share/zoneinfo/US/Pacific /etc/localtime
$date
Fri  5 Oct 18:20:05 PDT 2018
$

The new time zone appears in the output from the date command.

images If you just need to change the time zone for a single session or program, instead of changing the system time zone, you can set the time zone using the TZ environment variable. That overrides the system time zone for the current session.

Setting the Time and Date

Once you have the correct time zone for your Linux system, you can work on setting the correct time and date values. There are a few different commands available to do that.

Legacy Commands

There are two legacy commands that you should be able to find in all Linux distributions for working with time and date values:

  • hwclock displays or sets the time as kept on the internal BIOS or UEFI clock on the workstation or server.
  • date displays or sets the date as kept by the Linux system.

The hwclock command provides access to the hardware clock built into the physical workstation or server that the Linux system runs on. You can use the hwclock command to set the system time and date to the hardware clock on the physical workstation or server. Or, it also allows you to change the hardware clock to match the time and date on the Linux system.

The date command is the Swiss Army knife of time and date commands. It allows you to display the time and date in a multitude of formats in addition to setting the time and/or date. The + option allows you to specify the format used to display the time or date value by defining command sequences:

$ date +"%A, %B %d, %Y"
Monday, October 08, 2018
$

Table 9.1 shows the different command sequences available in the date command.

Table 9.1 The date format command sequences

Sequence Description
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time
%C Century (e.g., 20)
%d Numeric day of month
%D Full numeric date
%e Day of month, space padded
%F Full date in SQL format (YYYY-MM-dd)
%g Last two digits of year of ISO week number
%G Year of the ISO week number
%h Alias for %b
%H Hour in 24-hour format
%I Hour in 12-hour format
%j Numeric day of year
%k Hour in 24-hour format, space padded
%l Hour in 12-hour format, space padded
%m Numeric month
%M Minute
%n A newline character
%N Nanoseconds
%p AM or PM
%P Lowercase am or pm
%r Full 12-hour clock time
%R Full 24-hour hour and minute
%s Seconds since 1970-01-01 00:00:00 UTC
%S Second
%t A tab character
%T Full time in hour:minute:second format
%u Numeric day of week, 1 is Monday
%U Numeric week number of year, starting on Sunday
%V ISO week number
%w Numeric day of week, 0 is Sunday
%W Week number of year, starting on Monday
%x Locale's date representation as month/day/year or day/month/year
%X Locale’s full time representation
%y Last two digits of the year
%Y Full year
%z Time zone in +hhmm format
%:z Time zone in +hh:mm format
%::z Time zone in +hh:mm:ss format
%:::z Numeric time zone with : to necessary precision
%Z Alphabetic time zone abbreviation

As you can see from Table 9.1, the date command provides with numerous ways for you to display the time and date in your programs and shell scripts.

You can also set the time and date using the date command by specifying the value in the following format:

date MMDDhhmm[[CC]YY][.ss]

The month, date, hour, and minute values are required, with the year and seconds assumed, or you can include the year and seconds as well if you prefer.

The timedatectl Command

If your Linux distribution uses the Systemd set of utilities (see Chapter 6), you can use the timedatectl command to manage the time and date settings on your system:

$ timedatectl
      Local time: Fri 2018-10-05 21:46:02 EDT
  Universal time: Sat 2018-10-06 01:46:02 UTC
        RTC time: Sat 2018-10-06 01:45:58
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2018-03-11 01:59:59 EST
                  Sun 2018-03-11 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2018-11-04 01:59:59 EDT
                  Sun 2018-11-04 01:00:00 EST
$

The timedatectl command provides one-stop shopping to see all of the time information, including the hardware clock, called RTC, the date information, and the time zone information.

You can also use the timedatectl command to modify any of those settings as well by using the set-time option:

# timedatectl set-time "2018-10-06 10:35:00"

You can also use the timedatectl command to synchronize the workstation or server hardware clock and the Linux system time.

images Most Linux systems connected to the Internet utilize the Network Time Protocol (NTP) to keep the time and date synchronized with a centralized time server. If your Linux system does this, you won't be able to alter the time or date by using either the date or the timedatectl command.

Watching System Time

While not related to the time and date specifically, the Linux+ exam also covers the time command. The time command displays the amount of time it takes for a program to run on the Linux system:

$ time timedatectl
      Local time: Mon 2018-10-08 10:50:44 EDT
  Universal time: Mon 2018-10-08 14:50:44 UTC
        RTC time: Mon 2018-10-08 14:50:42
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2018-03-11 01:59:59 EST
                  Sun 2018-03-11 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2018-11-04 01:59:59 EDT
                  Sun 2018-11-04 01:00:00 EST

real 0m0.030s
user 0m0.001s
sys 0m0.001s

$

After the normal command output, you’ll see three additional lines of information:

  • real: The elapsed amount of time between the start and end of the program
  • user: The amount of user CPU time the program took
  • sys: The amount of system CPU time the program took

This information can be invaluable when troubleshooting programs that seem to take additional system resources to process.

Exercise 9.1 walks through using the different locale, time, and date functions you have available in Linux.

Exercise 9.1 Experimenting with time

This exercise will demonstrate how to check the current time, date, and time zone on your Linux system.

  1. Log in as root, or acquire root privileges by using su or by using sudo with each of the following commands.
  2. Type locale to display the current localization settings for your system. Write down the current character set assigned for your system
  3. Change the localization to another country, such as Great Britain, by setting the LANG environment variable. Type export LANG=en_GB.UTF-8 to make the change.
  4. Type locale to display the updated localization settings.
  5. If your Linux distribution uses the Systemd utilities, type localectl to display the system localization defined on your system.
  6. Change the localization by typing localectl set-locale "LANG=en_GB_UTF-8".
  7. Change the localization back to your normal locale by using either the locale or localectl command.
  8. Display the current date and time on your system by typing the date command.
  9. Observe how long it takes to run the date command on your Linux system by typing time date. The output shows how long it took your Linux system to process the request.

Summary

The Linux system supports many different languages by incorporating different character sets. A character set defines how the Linux system displays and uses the characters contained in the language. While Linux supports many different character sets, the most common ones are ASCII, Unicode, UTF-8, and UTF-16. The ASCII character set is only useful for English language characters, while the UTF-8 and UTF-16 character sets are commonly used to support other languages.

The Linux system maintains the character set settings as a set of environment variables that begin with LC_. The locale command displays all of the localization environment variables. Each individual LC_ environment variable represents a category of other environment variables that fine-tune the localization settings even further. You can display those environment variable settings by including the -ck option to the locale command.

You change the individual LC_ environment variables by using the Linux export command. Instead of changing all of the LC_ environment variables, you can set the special LANG or LC_ALL environment variables. Changing those will automatically change the other environment variables. Alternatively, if your Linux distribution supports the Systemd utilities, you can use the localectl command to display and change localization environment variable values.

You must define a time zone for your Linux system. Debian-based Linux distributions use the /etc/timezone file to determine the system time zone, while Red Hat–based Linux distributions use the /etc/localtime file. Both files utilize a binary format, so you can’t edit them directly. Linux maintains a library of time zone files in the /usr/share/zoneinfo folder. Just copy or link the appropriate time zone file from the /usr/share/zoneinfo folder to the time zone file for your Linux system.

There are three commands that you can use in Linux to display or change the time and date. The hwclock command displays the time as kept on the hardware clock for your Linux system. You can set the Linux system time to that or set the hardware clock to your Linux system time. The date command allows you to display the Linux system time and date in a multitude of formats by using a command-line sequence. It also allows you to set the date and time for the Linux system from the command line.

The time command doesn’t have anything to do with the current system time but instead provides information on the amount of time an individual application uses on the Linux system. You can use the time command to see how must real time elapsed between when the application started and it finished as well as to display the amount of system or user CPU time it required.

Exam Essentials

Describe how Linux works with different languages. Linux stores and displays language characters by using character sets. ASCII, Unicode, and UTF-8 are the most commonly used character sets for Linux.

Explain how to change the current character set on a Linux system. You can use the export command to change the LANG or LC_ALL environment variables to define a new character set. If your Linux distribution uses the Systemd utilities, you can also use the localectl command to display or change the system character set.

Describe how the time zone is set on a Linux system. Time zones are defined in Linux by individual files in the /usr/share/zoneinfo folder. Debian-based Linux distributions copy the appropriate time zone file to the /etc/timezone file, while Red Hat–based Linux distributions use the /etc/localtime file. To change the time zone for an individual script or program, use the TZ environment variable.

Summarize the tools you have available to work with the time and date on a Linux system. The hwclock command allows you to sync the Linux system time with the hardware clock on the system, or vice versa. The date command allows you to display the time and date in a multitude of formats or set the current time and date. The timedatectl command is from the Systemd utilities and allows you to display lots of different information about the system and hardware time and date in addition to allowing you to set them.

Explain how you can see the amount of time it takes for an application to run on the system. The time command allows you to place a timer on a specific application as it runs on the system. The output from the time command shows the actual elapsed time it took the program to run and how much user and system CPU time the application required.

Review Questions

  1. Which character set uses 7 bits to store characters?

    1. UTF-8
    2. UTF-16
    3. ASCII
    4. Unicode
    5. UTF-32
  2. What two character sets use a transformation code to store characters? (Choose two.)

    1. UTF-8
    2. UTF-16
    3. ASCII
    4. Unicode
    5. locale
  3. Which character set uses a 3-byte code and can represent characters from most languages used in the world?

    1. ASCII
    2. LC_ALL
    3. UTF-8
    4. UTF-16
    5. Unicode
  4. What Linux command displays all of the localization environment variables and their values?

    1. date
    2. time
    3. hwclock
    4. LANG
    5. locale
  5. What two environment variables control all of the localization settings?

    1. LC_MONETARY
    2. LC_NUMERIC
    3. LANG
    4. LC_CTYPE
    5. LC_ALL
  6. ___________ is the ability to adapt a Linux system to a specific language.

    1. locale
    2. Localization
    3. Character set
    4. Unicode
    5. ASCII
  7. What Systemd utility allows you to change the localization on your Linux system?

    1. timedatectl
    2. time
    3. date
    4. localectl
    5. locale
  8. Which Linux command changes the value of a localization environment variable?

    1. time
    2. export
    3. locale
    4. date
    5. hwclock
  9. Which LC_ environment variable determines how Linux displays dollar and cents values?

    1. LC_NUMERIC
    2. LC_MONETARY
    3. LC_CTYPE
    4. LC_TIME
    5. LC_COLLATE
  10. A ___________ determines the time relative to the UTC time in a specific location.

    1. Time zone
    2. Localization
    3. Character set
    4. Locale
    5. Hardware clock
  11. Which Linux commands allow you to retrieve the time from the physical workstation or server? (Choose two.)

    1. date
    2. hwclock
    3. time
    4. locale
    5. timedatectl
  12. What file does Red Hat–based systems use to define the time zone for the Linux system?

    1. /etc/localtime
    2. /etc/timezone
    3. /usr/share/zoneinfo
    4. /usr/share/timezone
    5. /usr/share/localtime
  13. Which folder contains template files for each time zone that Linux supports?

    1. /etc/localtime
    2. /usr/share/zoneinfo
    3. /etc/timezone
    4. $HOME
    5. /usr/share/timezone
  14. Which command displays the current date, system time, hardware time, and time zone?

    1. date
    2. timedatectl
    3. time
    4. hwclock
    5. localectl
  15. Which command do you use to display the current time and date using a specific output format?

    1. date
    2. time
    3. timedatectl
    4. localectl
    5. hwclock
  16. Which commands allow you to set the Linux system time to the workstation BIOS clock time? (Choose two.)

    1. hwclock
    2. date
    3. time
    4. timedatectl
    5. localectl
  17. What commands allow you to set the workstation BIOS clock time to the Linux system time? (Choose two.)

    1. hwclock
    2. date
    3. time
    4. timedatectl
    5. localectl
  18. Which environment variable can programmers use to temporarily change the time zone setting for just their environment?

    1. LANG
    2. LC_MONETARY
    3. LC_NUMERIC
    4. LC_ALL
    5. TZ
  19. Which character set has replaced ASCII as the default character set used in U.S. Linux installations?

    1. Unicode
    2. UTF-16
    3. UTF-8
    4. UTF-32
    5. locale
  20. Which command lists all of the localizations installed on your Linux system?

    1. timedatectl
    2. localectl
    3. locale
    4. LANG
    5. LC_ALL
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27