Get environment variable of other user

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

sudo -Hiu $user env | grep $var

For example

sudo -Hiu terdon env | grep HOME
HOME=/home/terdon

Method 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 "%sn" "$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.

Method 3

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x