Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

UNIX $USER and $LOGNAME environment variables

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-16-2018   -   Original Discussion by lsatenstein
lsatenstein's Unix or Linux Image
lsatenstein lsatenstein is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 16 April 2018, 4:59 PM EDT
Location: Montreal Canada
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
UNIX $USER and $LOGNAME environment variables

I have some comments about a previously closed topic whose name is the same as above

Omitted from the discussion was the situation with a "sudo command or sudo within a script".

There is an inconsistency between systems. On some systems $LOGNAME is invariant, on others,

on RedHat sudo su and echo $LOGNAME reports the name of the person invoking sudo.
on SUSE, sudo su and echo $LOGNAME reports root. $LOGNAME and $USER report root

Here is a workaround that I discovered and propose:

sudo xxx or sudo su sets up an environment variable SUDO_USER, SUDO_UID,SUDO_GID, and SUDO_COMMAND

It always reports the name of the logon invoking sudo (the caller)

What happens when I sudo su and as root su a third_user?
my system says $LOGNAME and $USER contain the "third_user" id. But
SUDO was invariant

I did not check what happens if the third_user issues as I have liimited my admin logon to one ID.

Please evaluate this posting and re-open that above mentioned topic to see if it is worth merging them together.



Sponsored Links
    #2  
Old Unix and Linux 04-16-2018   -   Original Discussion by lsatenstein
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 25 May 2018, 6:09 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,765
Thanks: 112
Thanked 1,687 Times in 1,239 Posts
Quote:
Originally Posted by lsatenstein View Post
[LEFT]I have some comments about a previously closed topic whose name is the same as above

Omitted from the discussion was the situation with a "sudo command or sudo within a script".

There is an inconsistency between systems. On some systems $LOGNAME is invariant, on others,

on RedHat sudo su and echo $LOGNAME reports the name of the person invoking sudo.
on SUSE, sudo su and echo $LOGNAME reports root. $LOGNAME and $USER report root

Here is a workaround that I discovered and propose:

sudo xxx or sudo su sets up an environment variable SUDO_USER, SUDO_UID,SUDO_GID, and SUDO_COMMAND

It always reports the name of the logon invoking sudo (the caller)

What happens when I sudo su and as root su a third_user?
my system says $LOGNAME and $USER contain the "third_user" id. But
SUDO was invariant

I did not check what happens if the third_user issues as I have liimited my admin logon to one ID.

Please evaluate this posting and re-open that above mentioned topic to see if it is worth merging them together.
I have no idea which thread you are referring to, perhaps you can provide a link to this thread?

Anyway, variables like "$LOGNAME" or "$USER" may be common but are in no way standardised (and even less is their content) and hence it is bad practice to rely on any of them. I suggest you use the id command to get the current user ID.

If you want a script you call via sudo to know from which UID it has been called use an argument to do so. Consider this template command:



Code:
sudo su - root -c /some/command -abc arg1 arg2

its call could be turned into:



Code:
chUser="$(id -un)"
sudo su - root -c /some/command -abc "$chUser" arg1 arg2

and subsequently you could inspect the arguments inside /some/command to find out by whom you have been called. Wouldn't that be a more robust way than to rely on some variables which may be defined readonly on one system and different on another?

I hope this helps.

bakunin
Sponsored Links
    #3  
Old Unix and Linux 04-16-2018   -   Original Discussion by lsatenstein
lsatenstein's Unix or Linux Image
lsatenstein lsatenstein is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 16 April 2018, 4:59 PM EDT
Location: Montreal Canada
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
Its 5pm in Montreal. I will do my due diligence tomorrow AM.
It looks interesting

My original problem was a script that had multiple functions such as

function 1
function 2
function 4.
function ...

The script invokes functions 1,2,3,... and I needed to do sudo for function 4 and continue with function 5..end

I am aware that I cannot sudo a function so, I have been trying other techniques to achieve what needs to be done. This research led to this posting.
the sudo'd function needs to know who invoked sudo in a consistent manner. I found it different between different vendors of Linux (SUSE and RedHat)

Thank you for the response.
    #4  
Old Unix and Linux 04-17-2018   -   Original Discussion by lsatenstein
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 26 May 2018, 8:29 AM EDT
Location: Simplicity
Posts: 4,085
Thanks: 351
Thanked 1,379 Times in 1,243 Posts
Traditionally there was a difference between SysV su and BSD su, only the latter sets USER (and LOGNAME does not exist).
Portable is only su - that is a kind of login shell that sets LOGNAME (if exists) and USER.
GNU/Linux started more BSD-compatible, but later partly changed to Posix that is more similar to SysV.
So yes, it is a mess, and sudo, being a kind of su, makes it even worse.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Doing math using user defined input and system environment variables saravanapandi Shell Programming and Scripting 3 01-31-2014 11:46 PM
Run script through cron with user environment variables gacanepa Shell Programming and Scripting 2 12-17-2013 09:02 AM
Using Datastage environment variables in Unix script bghosh Shell Programming and Scripting 1 02-29-2012 06:18 AM
Unix $USER and $LOGNAME environment variables royalibrahim Shell Programming and Scripting 7 01-20-2011 05:18 AM
by using c++ how to set environment variables in unix sada@123 UNIX for Advanced & Expert Users 1 03-21-2006 12:59 AM



All times are GMT -4. The time now is 11:43 AM.