I need to retrieve $XDG_CONFIG_HOME, $XDG_RUNTIME_DIR and $XDG_DATA_HOME of all users. I try with:
sudo -Eiu user printenv | grep XDG_CONFIG_HOME
But I get nothing.
If I log on with the user account, the simple command:
printenv | grep XDG_CONFIG_HOME
Works fine.
How do I?
TLDR, these variables are not available in the sudo
case, and some can even not be set at all in the standard login case.
On my linux system (debian 8) some of the XDG
variables are defined when the user's session is initialized with the pam module pam_systemd
. The variables defined to be initialised at that point are documented in the man page, but include XDG_RUNTIME_DIR
.
This module is only loaded in the login cases - i.e. I need to ssh to the system, or login via the console/gui to get the user session to initialize via the pam_systemd
module.
This module is not loaded in the su
or the sudo
cases.
As a result, when you do a sudo
to emulate a user this variable is not set.
Other XDG
variables are only applicable when the user is actually logged in at the GUI. I don't get these variable set on the GUI that I use, but the docs for XDG variables specify what they are supposed to default to if they're not set. The default value for XDG_CONFIG_HOME
is $HOME/.config
, and the default value for XDG_DATA_HOME
is $HOME/.local/share
.
So you should only expect these variables to be set in the interactive login case, and not via the non-interactive case - sudo
would be effectively designated as a non-interactive case, even though you can do interactive things with it.
These values are very much templated - e.g. the XDG_RUNTIME_DIR
on my system is /var/run/UID
, and, per the man page, does not survive after my last logout; so it's not a place to put permanent files.
Unless you, as a user change these values, I would expect the defaults that are specified in the XDG documentation, the KDE compliance pages to be the ones that are used. There are probably others, but I just picked the KDE compliance one as it calls it out in the systems admin guide.