29

Is there a way I can see the environment variable of an other user? I want to do that as root, so permissions won't be a problem I guess.

For the user himself, I use echo $PATH or set | grep PATH (or set when I don't remember the variable name). What would be a similar instruction for an other user?

For what it's worth, I'm on Ubuntu Server 13.04.

  • 4
    Users don't have environment variables, processes do. It's common for the processes executed by one user to not all have the same environment. What problem are you trying to solve? – Gilles 'SO- stop being evil' Jun 03 '13 at 18:51
  • @Gilles for example, I want to know what MAIL folder a user has. I could login as that user, then do echo $MAIL, but I thought there might be a shortcut. –  Jun 03 '13 at 19:04
  • 3
    That still doesn't define the question usefully (and for example the answer you've accepted may or may not work depending on a lot of factors). Are you looking for the default mailbox location? Are you looking for the location where the user actually receives his local mail (which may be different, and may not exist, if the user has a .forward)? Users often set a few variables in their .profile or other configuration files, and there's no way to reliably enumerate them all. Again, what problem are you trying to solve? – Gilles 'SO- stop being evil' Jun 03 '13 at 19:14

3 Answers3

26

Another option is to use env. Run this as root or with sudo:

sudo -Hiu $user env | grep $var

For example

sudo -Hiu terdon env | grep HOME
HOME=/home/terdon
terdon
  • 242,166
  • 1
    When I log in as root and execute this command for my standard user name, I get HOME=/root. That's the home directory of root, but it's not my standard home directory. If you want to know the environment that a particular user would get when he logs in, you have to run the startup scripts of the login shell of this particular user (and remember that his login shell may be different from your login shell). – Uwe Jun 03 '13 at 18:23
  • @Uwe on what system? It works fine for me. Do you mean you log in as root from the login manager? It works fine if I drop to a tty and log in as root. – terdon Jun 03 '13 at 18:34
  • I'm on a Debian machine. I log in as uwe, open an xterm, run env | grep HOME and get HOME=/home/uwe; I execute su and then once more env | grep HOME and get HOME=/root, then I run sudo -u uwe env | grep HOME and still get HOME=/root. – Uwe Jun 03 '13 at 18:42
  • @uwe strange. I am also on a Debian (LMDE) machine, I did the exact same process you described (even used xterm instead of my usual terminal) and get the expected result. – terdon Jun 03 '13 at 18:46
  • According to the manual, sudo sets the HOME variable only if the -H option is present or if the corresponding entry in /etc/sudoers is set. It seems that our sudoers files differ. Anyway, it's probably a good idea to use -H here, and possibly also -i. – Uwe Jun 03 '13 at 19:06
  • @Uwe ah, yes, I have Defaults env_reset in my /etc/sudoers, that might explain the difference. Anyway added -Hi to be on the safe side. – terdon Jun 03 '13 at 19:10
  • if you only want to get the value out of that, you can do sudo -Hiu $user env | grep $var | cut -d '=' -f 2 – Andrei-Niculae Petre Mar 28 '19 at 20:40
  • When I try to do this as user root for user www-data, I get this error: This account is currently not available. – Nathan Wailes Oct 09 '19 at 13:01
  • @NathanWailes if you have a new question, please ask it separately. That way you can give us the necessary context. Normally, however, www-data doesn't have a login shell, so cannot be used with sudo. If you ask a new question, explaining what you need, we should be able to find a workaround. – terdon Oct 09 '19 at 13:23
  • @terdon Thanks, I'll do that now. – Nathan Wailes Oct 09 '19 at 13:26
2

For one user, you can do like this:

su - <username> -c '. ~/.profile; echo $PATH'

List $PATH of all user:

for user in $(cat /etc/passwd | awk -F: '{print $1}'); do
  su - $user -c '. ~/.profile; printf "%s\n" "$PATH"'
done

@Camil Staps

. ~/.profile is thus a trick I learn from my favourite person, Peteris Krumins. He explained the trick here . Maybe later bash version had building with option NON_INTERACTIVE_LOGIN_SHELLS.

cuonglm
  • 153,898
  • Thanks! For $PATH it also works without the . ~/.profile; part - what is it for? –  Jun 03 '13 at 17:46
  • @CamilStaps What do you mean by "it works"? Yes, you get a value for $PATH even without sourcing ~/.profile, but it's almost certainly not the one that you want (= the value that this user would see when he logged in). Even sourcing ~/.profile isn't fully reliable, since the user might use a shell whose startup file is different from ~/.profile. – Uwe Jun 03 '13 at 18:29
  • @Uwe I got the same output without, but I guess that's just a coincidence then. –  Jun 03 '13 at 18:41
  • You're assuming the user is not using csh. – glenn jackman Jun 03 '13 at 19:09
  • @CamilStaps The first question is whether that particular user modifies his PATH in his ~/.profile at all. But in any case, the behavior of sudo can be configured quite a lot in /etc/sudoers (see my discussion with terdon), so when our systems behave differently, that's probably caused by differences in the configuration files. – Uwe Jun 03 '13 at 19:19
  • Here's another variation on the theme: su - $user -c env | grep ^PATH= | cut -d'=' -f2-, so to use it in a script.. some_user_path=$(su - ${user} -c env | grep ^PATH= | cut -d'=' -f2-) – Ville Mar 04 '18 at 22:32
0

from root you can su - to the user and then grep the environment variable that you want to see:

su - <username> -c 'echo $PATH'

Raza
  • 4,109
  • for some reason this doesn't work for me, even if command is executed as a different user. I ended up using "env", so sth like runuser -l <username> -c "env | grep <var>" | cut -d '=' -f 2 works fine for me. If you're not root, need to prefix command with sudo. – Andrei-Niculae Petre Mar 28 '19 at 20:37