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.
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.
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):
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
There are two legacy commands that you should be able to find in all Linux distributions for working with time and date values:
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.
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.
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.
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 programuser
: The amount of user CPU time the program tooksys
: The amount of system CPU time the program tookThis 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.
This exercise will demonstrate how to check the current time, date, and time zone on your Linux system.
su
or by using sudo
with each of the following commands.locale
to display the current localization settings for your system. Write down the current character set assigned for your systemLANG
environment variable. Type export LANG=en_GB.UTF-8
to make the change.locale
to display the updated localization settings.localectl
to display the system localization defined on your system.localectl set-locale "LANG=en_GB_UTF-8"
.locale
or localectl
command.date
command.date
command on your Linux system by typing time date
. The output shows how long it took your Linux system to process the request.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.
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.
Which character set uses 7 bits to store characters?
What two character sets use a transformation code to store characters? (Choose two.)
locale
Which character set uses a 3-byte code and can represent characters from most languages used in the world?
LC_ALL
What Linux command displays all of the localization environment variables and their values?
date
time
hwclock
LANG
locale
What two environment variables control all of the localization settings?
LC_MONETARY
LC_NUMERIC
LANG
LC_CTYPE
LC_ALL
___________ is the ability to adapt a Linux system to a specific language.
locale
What Systemd utility allows you to change the localization on your Linux system?
timedatectl
time
date
localectl
locale
Which Linux command changes the value of a localization environment variable?
time
export
locale
date
hwclock
Which LC_
environment variable determines how Linux displays dollar and cents values?
LC_NUMERIC
LC_MONETARY
LC_CTYPE
LC_TIME
LC_COLLATE
A ___________ determines the time relative to the UTC time in a specific location.
Which Linux commands allow you to retrieve the time from the physical workstation or server? (Choose two.)
date
hwclock
time
locale
timedatectl
What file does Red Hat–based systems use to define the time zone for the Linux system?
/etc/localtime
/etc/timezone
/usr/share/zoneinfo
/usr/share/timezone
/usr/share/localtime
Which folder contains template files for each time zone that Linux supports?
/etc/localtime
/usr/share/zoneinfo
/etc/timezone
$HOME
/usr/share/timezone
Which command displays the current date, system time, hardware time, and time zone?
date
timedatectl
time
hwclock
localectl
Which command do you use to display the current time and date using a specific output format?
date
time
timedatectl
localectl
hwclock
Which commands allow you to set the Linux system time to the workstation BIOS clock time? (Choose two.)
hwclock
date
time
timedatectl
localectl
What commands allow you to set the workstation BIOS clock time to the Linux system time? (Choose two.)
hwclock
date
time
timedatectl
localectl
Which environment variable can programmers use to temporarily change the time zone setting for just their environment?
LANG
LC_MONETARY
LC_NUMERIC
LC_ALL
TZ
Which character set has replaced ASCII as the default character set used in U.S. Linux installations?
locale
Which command lists all of the localizations installed on your Linux system?
timedatectl
localectl
locale
LANG
LC_ALL