Obtaining the username in a root script

Archived topics about LMDE 1 and LMDE 2
Locked
swiftlinuxcreator

Obtaining the username in a root script

Post by swiftlinuxcreator »

When you're the root user in a shell, what is the command to obtain your regular username? I'm working scripts that need to obtain the regular username but must be executed as root.

I've found that "echo $USERNAME" works initially. However, I find that after I run the script, "echo $USERNAME" no longer works. It's as if the USERNAME environmental variable had been deleted.

Is there a better way?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
xircon

Re: Obtaining the username in a root script

Post by xircon »

Work-a-round, if it works initially, then set your own variable from the info held in $USERNAME sort of:

Code: Select all

myuser=$USERNAME
Anakinholland

Re: Obtaining the username in a root script

Post by Anakinholland »

xircon wrote:Work-a-round, if it works initially, then set your own variable from the info held in $USERNAME sort of:

Code: Select all

myuser=$USERNAME
This will not work using "su -", as tested by myself

Code: Select all

[anakin@stormbird ~]$ echo $USERNAME
anakin
[anakin@stormbird ~]$ su -
Password:            
[root@stormbird ~]# echo $USERNAME

[root@stormbird ~]# logout
[anakin@stormbird ~]$ su
Password: 
[root@stormbird anakin]# echo $USERNAME               
anakin
[root@stormbird anakin]# exit
[anakin@stormbird ~]$ readonly USERNAME
[anakin@stormbird ~]$ export USERNAME
[anakin@stormbird ~]$ su -
Password: 
[root@stormbird ~]# echo $USERNAME

[root@stormbird ~]# logout
[anakin@stormbird ~]$ export myname=$USERNAME
[anakin@stormbird ~]$ su -
Password: 
[root@stormbird ~]# echo $myname

[root@stormbird ~]# logout
[anakin@schuldiner ~]$ sudo echo $USERNAME
[sudo] password for anakin: 
anakin
[anakin@schuldiner ~]$ 
and confirmed here:
Any shell invoked through sudo or su is a new shell. What su - does is to invoke a login shell, so that environment variables are reset and ~/.profile or ~/.bash_profile is read.
There is a command "logname", but that doesn't work on Mint, not by default anyway.

You could circumvent this by echoing $USERNAME to a file, then read that file while being root, but there's no proper solution I know about.

Regards,

Anakin
xircon

Re: Obtaining the username in a root script

Post by xircon »

Annakin - but it does work in straight su (no -):

Code: Select all

molly2@n5010 ~ $ myuser=$USERNAME
molly2@n5010 ~ $ echo $myuser
molly2
molly2@n5010 ~ $ su
Password: 
n5010 molly2 # echo $myuser

n5010 molly2 # myuser=$USERNAME
n5010 molly2 # echo $myuser
molly2
n5010 molly2 # 
Can't see a reason to assume the user environment of the target user, so a straight su should suffice.
Anakinholland

Re: Obtaining the username in a root script

Post by Anakinholland »

xircon wrote:Annakin - but it does work in straight su (no -):

Code: Select all

molly2@n5010 ~ $ myuser=$USERNAME
molly2@n5010 ~ $ echo $myuser
molly2
molly2@n5010 ~ $ su
Password: 
n5010 molly2 # echo $myuser

n5010 molly2 # myuser=$USERNAME
n5010 molly2 # echo $myuser
molly2
n5010 molly2 # 
Yes, I tested that in my example as well. But in that situation you could also just use $USERNAME :)
xircon

Re: Obtaining the username in a root script

Post by xircon »

Without the OP's script, it has hard to see what is going wrong though :)
Locked

Return to “LMDE Archive”