Linux: language, locale and time zone
Configure the locale, the LANG, LC_ALL and LC_TIME variables, date formats and the time zone on Linux.
The locale sets the language of program messages as well as the date, time and number formats. It is controlled by environment variables. The date format of the date command follows the POSIX specification.
Locale variables
| Variable | Role |
|---|---|
LANG |
default locale for the session |
LANGUAGE |
fallback list of languages for messages (GNU) |
LC_ALL |
forces all categories, takes precedence over LANG |
LC_TIME |
date and time format |
Set the default locale for the session:
export LANG=en_US.UTF-8
export LANGUAGE=en
Put these lines in ~/.profile, then start a new login session. For a system-wide setup, use a file in /etc/profile.d/.
Force or remove LC_ALL:
export LC_ALL=C
unset LC_ALL
LC_ALL=C forces the default C locale, so programs print their messages in the default language (English).
Generate a locale (Debian / Ubuntu)
sudo locale-gen fr_FR
sudo locale-gen fr_BE
sudo locale-gen en_GB
sudo locale-gen en_US
sudo update-locale LANG=fr_FR
sudo update-locale LANG=en_US
If a locale is not generated, the command shows a warning like this:
setlocale: LC_ALL: cannot change locale (fr_BE): No such file or directory
Inspect and list locales:
locale
localectl status
locale -a
localectl list-locales
To add language packs, use for example sudo apt-get install language-pack-nl. Uncomment the wanted line in /etc/locale.gen, then run locale-gen if needed.
Date and time formats
date "+%T"
date "+%a %d %b %Y %r %Z"
DATE=$(date +"%d-%b-%Y")
DATE=$(date '+%Y%m%d%H%M%S')
With Bash 4.2 or later, printf avoids starting an external process:
printf -v DATE '%(%Y%m%d%H%M%S)T' -1
For a 24-hour display, set LC_TIME in /etc/locale.conf:
LANG=en_US.UTF-8
LC_TIME=en_GB.UTF-8
Configuration files by distribution
| Distribution | File |
|---|---|
| Debian / Ubuntu | /etc/default/locale |
| systemd (all distributions) | /etc/locale.conf |
| Red Hat / CentOS | /etc/sysconfig/i18n |
| PAM (user session) | ~/.pam_environment |
Time zone
timedatectl
ls -l /etc/localtime
timedatectl list-timezones
sudo timedatectl set-timezone Europe/Brussels
Replace Europe/Brussels with the wanted zone (IANA time zone name).
Remarks
- A locale must be generated before it can be used, otherwise programs fall back to a default value.
- The
LANG="en_US"examples of the source note were completed toen_US.UTF-8to match the generated UTF-8 locales.