You are not logged in.

#1 2014-11-21 14:22:54

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,928
Website

[solved] echo $USER in root gives previous user

When I su into root from whatever user, echo $USER gives not "root" but the username I was su-ing from.
For example:

[john@HomeC ~]$ echo $USER
john
[john@HomeC ~]$ su mark
Password:
[mark@HomeC john]$ echo $USER
mark
[mark@HomeC john]$ su
Password:
[root@HomeC john]# echo $USER
mark
[root@HomeC john]# exit
[mark@HomeC john]$ exit
[john@HomeC ~]$ su
Password:
[root@HomeC john]# echo $USER
john

Can you help me please to track down what the problem is.

Last edited by SanskritFritz (2014-11-21 15:21:30)


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#2 2014-11-21 14:27:01

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,408

Re: [solved] echo $USER in root gives previous user

That is along the same lines as when you su into root and try to "steal" the X credentials.  Some applications will not run in some window managers/desktop environments unless the keyring is also transferred.


I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.

Offline

#3 2014-11-21 14:30:17

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] echo $USER in root gives previous user

Well sure. That invocation of su does nothing to affect the environment. The manpage even says this.

Offline

#4 2014-11-21 14:35:00

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,928
Website

Re: [solved] echo $USER in root gives previous user

So the default behaviour of normal users is to set that variable but root is an exception for this?


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#5 2014-11-21 14:36:26

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] echo $USER in root gives previous user

Your confusion is caused by your assumptions of who sets that variable. Its not the shell.

Offline

#6 2014-11-21 14:39:11

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,928
Website

Re: [solved] echo $USER in root gives previous user

falconindy wrote:

Your confusion is caused by your assumptions of who sets that variable. Its not the shell.

Oh I see. Sadly I can't find the snippet where it is set. Not .bashrc, nor .bash_profile sets it. With the risk of being a help vampire, can you tell me how $USER is set?

Last edited by SanskritFritz (2014-11-21 14:39:38)


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#7 2014-11-21 14:41:56

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,928
Website

Re: [solved] echo $USER in root gives previous user

Ah, found it: /etc/bash.bashrc

UPDATE: no, that's not it either. sad

Last edited by SanskritFritz (2014-11-21 14:42:31)


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#8 2014-11-21 15:03:39

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] echo $USER in root gives previous user

It's the login program which does this in the case of a normal login:

https://github.com/karelzak/util-linux/ … in.c#L1048

If you tell su to create a new login session (as you should always do if you insist on using su. pro tip: you shouldn't, it's bad, use sudo or runuser), then su will set it:

https://github.com/karelzak/util-linux/ … mon.c#L523

Offline

#9 2014-11-21 15:21:12

SanskritFritz
Member
From: Budapest, Hungary
Registered: 2009-01-08
Posts: 1,928
Website

Re: [solved] echo $USER in root gives previous user

Thank you guys.


zʇıɹɟʇıɹʞsuɐs AUR || Cycling in Budapest with a helmet camera || Revised log levels proposal: "FYI" "WTF" and "OMG" (John Barnette)

Offline

#10 2014-11-21 19:35:26

Leonid.I
Member
From: Aethyr
Registered: 2009-03-22
Posts: 999

Re: [solved] echo $USER in root gives previous user

falconindy wrote:

pro tip: you shouldn't, it's bad, use sudo or runuser

Learning something new everyday smile Thanks.


Arch Linux is more than just GNU/Linux -- it's an adventure
pkill -9 systemd

Offline

Board footer

Powered by FluxBB