Posts On sudo & User Environment Variables

On sudo & User Environment Variables

Recently I’ve been forced to remember how sudo carries user’s environment into the root user’s space when used. However, in order to make sense of my situation, some background is necessary.

In most cases, a computer’s and its users locale settings are the same unless there’s a user coming from a different country. My situation is somewhat tangent to this scenario: I’m running my computer in US English language while, all my other locale settings (time, date, measurement, paper size, etc.) are in line with my country. This creates oddities like some programs are running in a different language but, it’s 99% managable. Another gotcha is the root user of the system is set to full US English locale. Last but, not the least KDE overrides some of my locale settings as it pleases.

Recently, I’ve found out that my GRUB language has changed and suprised. My initial thoughts were about some systemd and localectl magic overreach and got upset. However I’ve found out that the GRUB language is directly derived from the locale (relevant file is at /etc/grub.d/00_header). Then, I’ve thought that the locale may be leaking to root user’s space via sudo.

A simple test of running echo $LANG | cut -d . -f 1 verified my suspicion. When run with sudo, my user environment is carried to root space and used as is. However if I switch to root user via su -, the root user’s environment is loaded and used. Running update-grub with the root user changed the language and made everything as it should be.

So, please remember: For a safer system update process either use su - directly or use sudo su - to reset the environment. Otherwise, some funny things may happen.

This post is licensed under CC BY 4.0 by the author.
Trending Tags



Trending Tags