1. Introduction

When working with Linux systems, we often need to manage and configure various settings to ensure the system behaves as expected. One such important aspect is locale management, which includes character encoding, language, and keyboard layouts. These settings can be handy when the system isn’t displaying the correct language or the keyboard layout is set to a language we don’t understand.

For example, when dealing with users from different countries or regions, we often encounter diverse languages and cultural preferences. In such a situation, we might need to ensure that the system is configured properly to meet their needs. To manage these settings, we can use the localectl command.

In this tutorial, we explore the localectl command. First, we’ll discuss the basic usage of localectl. Next, we’ll go over its advanced options to add functionality and customize the output.

2. Basic Usage

When we execute the standalone localectl command, it displays the current system locale and keyboard layout settings:

$ localectl
System Locale: LANG=en_US.UTF-8
    VC Keymap: (unset)         
   X11 Layout: us
    X11 Model: pc105

We see in the output that it configures the System Locale to English (US) with UTF-8 character encoding. Additionally, the virtual console uses no specific keyboard mapping, whereas the X11 environment uses the US layout. Lastly, the system set the X11 Model to pc105.

3. Additional Options

The localectl command comes with additional options that we can use to manage the system’s locale and keyboard settings more effectively.

3.1. Listing Available Locales

We can use the list-locales subcommand to list all available locales on the system:

$ localectl list-locales
C.UTF-8
en_AG.UTF-8
en_AU.UTF-8
...
...
en_ZA.UTF-8
en_ZM.UTF-8
en_ZW.UTF-8

This option is useful to identify the correct locale code for a specific language and region.

If the desired locale isn’t listed, we can add it manually by editing the locale.gen configuration file:

$ sudo nano /etc/locale.gen

After that, we uncomment the line corresponding to the desired locale by removing the # at the beginning of the line.

Next, let’s generate the locale using the locale-gen command to apply the changes:

$ sudo locale-gen

Once the locale is generated, we can verify whether the desired locales are now available by running the localectl list-locales command again.

3.2. Setting the System Locale

After finding the desired system locale, we can use the set-locale subcommand followed by the desired local code to change it:

$ sudo localectl set-locale LANG=en_GB.UTF-8

When we execute this command, it updates the /etc/locale.conf file to set the LANG variable to en_GB.UTF-8. To verify this, we can again execute the localectl command:

$ localectl
System Locale: LANG=en_GB.UTF-8
    VC Keymap: (unset)         
   X11 Layout: us
    X11 Model: pc105

Earlier, the option was set to English (US) which has now changed to British English (GB) as the default language. Moreover, we didn’t change the character encoding in both cases.

3.3. Managing Keyboard Layouts

A keyboard layout determines which letters and symbols appear when we press different keys. Different countries and languages have different standard keyboard layouts. So, configuring the correct keyboard layout is necessary for typing accurately and efficiently.

To list all available keyboard layouts, we can use the list-x11-keymap-layouts subcommand:

$ localectl list-x11-keymap-layouts

This command displays a comprehensive list of all available keyboard layouts supported by X11, the basic framework for a graphical user interface (GUI) environment on Linux.

Once we identify the appropriate keyboard layout, we can proceed to set it using the set-x11-keymap subcommand followed by the desired layout code:

$ sudo localectl set-x11-keymap gb

Additionally, we might also need to set the keymap for the virtual console (non-GUI) environment for working in text-only mode:

$ sudo localectl set-keymap gb

After executing these commands, we can verify the changes by running the localectl command again:

$ localectl
System Locale: LANG=en_US.UTF-8
VC Keymap: gb
X11 Layout: gb
X11 Model: pc105

At this point, we’ve updated the system’s X11 keyboard and Virtual Console layout to gb.

4. Conclusion

In this article, we went over the localectl command and some of its options.

Initially, we saw how running the localectl command alone displays the current system locale and keyboard layout such as English (US) with UTF-8 encoding. Afterward, we discussed how to display the available locales, set new system locales, and manage keyboard layouts.

In conclusion, localectl is an essential tool for managing locale and keyboard settings on Linux systems.