Setting the correct language and timezone on your Linux server is vital, especially for global deployments. In this post, we’ll discuss locale.sh
, a script designed to automate locale and timezone configuration on both Fedora and Ubuntu systems, which you can find in my GitHub repository. The script intelligently adapts to the distribution it’s running on, ensuring your configuration needs are met seamlessly. Let’s delve into the script to understand its purpose and functionality.
Why Configure Locale and Timezone?
Proper locale and timezone settings enhance system efficiency, conserve disk space, and ensure timestamps align with your regional preferences. Incorrect configurations can lead to confusion and inconsistencies in logs and application behavior. By automating these settings with locale.sh
, you can save time and avoid errors during server initialization.
The locale.sh
Script
The revised script below dynamically adjusts based on the operating system, allowing you to configure locale and timezone settings effortlessly:
#!/bin/bash -ex
# locale.sh - Set up language and timezone settings on a Linux system
# Set the default locale to an environment variable if it exists, otherwise use en_US.utf8
DEFAULT_LOCALE="${LOCALE_OVERRIDE:-en_US.utf8}"
# Set the timezone (change this to your preferred timezone or export TIMEZONE in the environment)
TIMEZONE="${TIMEZONE_OVERRIDE:-America/New_York}"
echo "Configuring locale settings..."
# Function to check the distribution
get_distro() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
echo "$ID"
else
echo "unknown"
fi
}
# Detect the distribution
DISTRO=$(get_distro)
# Remove unused locales and handle the locale archive based on the distribution
case $DISTRO in
ubuntu|debian)
echo "Removing unused locales on Ubuntu/Debian..."
localedef --list-archive | grep -a -v "$DEFAULT_LOCALE" | xargs -r sudo localedef --delete-from-archive
# Set system-wide locale settings
echo "Setting default locale to $DEFAULT_LOCALE on Ubuntu/Debian..."
echo "LANG=$DEFAULT_LOCALE" | sudo tee /etc/default/locale > /dev/null
update-locale LANG=$DEFAULT_LOCALE
;;
fedora|centos|rhel)
echo "Removing unused locales on Fedora/RHEL..."
localedef --list-archive | grep -a -v "$DEFAULT_LOCALE" | xargs -r sudo localedef --delete-from-archive
# Rebuild locale archive on Fedora/RHEL
echo "Rebuilding locale archive on Fedora/RHEL..."
cp /usr/lib/locale/locale-archive{,.tmpl}
build-locale-archive
# Set system-wide locale settings
echo "Setting default locale to $DEFAULT_LOCALE on Fedora/RHEL..."
echo "LANG=$DEFAULT_LOCALE" | tee /etc/locale.conf > /dev/null
localectl set-locale LANG=$DEFAULT_LOCALE
;;
*)
echo "Unsupported distribution: $DISTRO"
exit 1
;;
esac
# Set the timezone
echo "Configuring timezone to $TIMEZONE..."
timedatectl set-timezone $TIMEZONE
echo "Locale and timezone configuration completed."
Script Walkthrough
1. Environment Variables
The script starts by checking for environment variables LOCALE_OVERRIDE
and TIMEZONE_OVERRIDE
. If these variables are set, their values are used; otherwise, it defaults to en_US.UTF-8
for the locale and America/New_York
for the timezone:
DEFAULT_LOCALE="${LOCALE_OVERRIDE:-en_US.utf8}"
TIMEZONE="${TIMEZONE_OVERRIDE:-America/New_York}"
2. Distribution Detection
The script includes a function, get_distro
, to determine the Linux distribution by reading the /etc/os-release
file. This enables the script to adapt its behavior based on whether it is running on Fedora, Ubuntu, or another supported distribution.
get_distro() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
echo "$ID"
else
echo "unknown"
fi
}
3. Locale Management
Depending on the detected distribution, the script handles locale management:
- For Ubuntu/Debian: It removes unused locales and updates the system-wide locale settings in
/etc/default/locale
usinglocaledef
:
localedef --list-archive | grep -a -v "$DEFAULT_LOCALE" | xargs -r sudo localedef --delete-from-archive
- For Fedora/RHEL: It performs the same locale removal and additionally rebuilds the locale archive with
build-locale-archive
:
sudo cp /usr/lib/locale/locale-archive{,.tmpl}
sudo build-locale-archive
4. Setting the Default Locale
After handling locale removal, the script sets the default locale for the system:
echo "LANG=$DEFAULT_LOCALE" | sudo tee /etc/default/locale > /dev/null
For Fedora/RHEL, it updates /etc/locale.conf
instead:
echo "LANG=$DEFAULT_LOCALE" | sudo tee /etc/locale.conf > /dev/null
5. Configuring the Timezone
Finally, the script sets the system timezone using timedatectl
, ensuring that timestamps and scheduling are correctly aligned with your preferred time zone:
timedatectl set-timezone $TIMEZONE
Running the Script
You can execute this script on either Ubuntu or Fedora systems. Set the necessary environment variables for locale and timezone before running the script:
export LOCALE_OVERRIDE="fr_FR.UTF-8"
export TIMEZONE_OVERRIDE="Europe/Paris"
./locale.sh
This allows you to customize the script’s behavior based on your server’s needs without modifying the script directly.
Conclusion
The updated locale.sh
script provides a flexible and automated way to configure locale and timezone settings across different Linux distributions. By intelligently detecting the operating system, the script ensures that the correct commands are used, streamlining the setup process. This makes it an invaluable tool for DevOps practices and server management.
Try it out to simplify your language and timezone configuration, and enjoy a more efficient server setup!