Behaviour of su(1)

Chuck Swiger cswiger at mac.com
Fri Oct 31 12:17:14 PDT 2008


Hi--

On Oct 31, 2008, at 11:33 AM, Frédéric Perrin wrote:
> When I « su - » to root (after being logged in as my normal user), the
> LOGNAME env variable is still set to my previous user, as in :
>
> ,----
> | fred at chameau:~% /usr/bin/su -l
> | Password:
> | root at chameau:~# echo $USER - $LOGNAME
> | root - fred
> `----
>
> As far as I can tell, this contradicts the fine manual that says :
>
> ,----
> | -l      Simulate a full login.  The environment is discarded  
> except for
> |         HOME, SHELL, PATH, TERM, and USER.
> `----
>
> So I would have expected LOGNAME to be either empty or set by some  
> shell
> startup script to be root. So, why is LOGNAME still equal to my  
> previous
> user ? (and where is it set ? « grep -r LOGNAME /etc » doesn't turn up
> anything...)

When you "su -l" it invokes /usr/bin/login, which per "man login" sets  
up up $LOGNAME:

      The login utility enters information into the environment (see
      environ(7)) specifying the user's home directory (HOME), command  
inter-
      preter (SHELL), search path (PATH), terminal type (TERM) and  
user name
      (both LOGNAME and USER).

I believe it looks up the actual username from the wtmp record  
associated with your open tty, so $USER corresponds to the effective  
userid, but $LOGNAME corresponds to the actual username used to login,  
aka your "real userid"...?

Regards,
-- 
-Chuck



More information about the freebsd-questions mailing list