3

What I want is to find the name of the user that runs bash script, when this script is executed by sudo. The only possible way, that I know, is to parse the output of who -m (who am i) in this way:

user@UbuntuServer:~$ cat who.sh 
#!/bin/sh
whoami
echo $USER
who -m | awk '{print $1}'

user@UbuntuServer:~$ sudo ./who.sh 
root
root
user

The problem is: In Ubuntu Desktop 16.04.4 who -m (resp. who am i) do nothing. Why?

enter image description here

Another question is why the Ubuntu's online manual page for who is different from man who executed either on Ubuntu Desktop or Server?

But these questions are not so important according to my goals. And as it is mentioned in the title, the main question is: How do I find which user executes the script when is used sudo?


Practically, the original title - How do I find 'who am I' on Ubuntu Desktop? - is the same but wrongly asked question.

pa4080
  • 29,831
  • 3
    "who am I" is not a command. The command is whoami . Note no spaces and no cap I (whoamI is NOT the same as whoami). The command who is working as expected, what output were you expecting ? – Panther May 09 '18 at 06:22
  • 3
    @Panther, who am i is synopsis of who, please check the section OPERANDS in man who. I know whoami is different command. I'm expecting some any output of who -m or who am i, but there is no output. I updated the question. – pa4080 May 09 '18 at 06:30
  • from man who in my desktop 16.04 LTS : If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual but obviously those arguments and -m are not implemented there ;-) – sudodus May 09 '18 at 06:38
  • 2
    See https://unix.stackexchange.com/questions/55874/who-am-i-after-su-not-showing-new-user-id . – Panther May 09 '18 at 06:41
  • 1
    There is a descussion about why the change in Fedora - https://bugzilla.redhat.com/show_bug.cgi?id=1280724 . I can not find a Debian specific discussion but I suspect it has something to do with "You ask why this was removed. The issue was that this was the implementation requires setuid or setgid helpers to elevate privileges to write to the single shared file. In addition, the mechanism is broken because there's no cleanup when a session crashes, so the data this returns cannot be trusted for anything anyway. The same information can be retrieved from other sources in a more reasonable way." – Panther May 09 '18 at 06:57
  • which who is this? It's working fine for me on Lubuntu 16.04, Server 16.04 and Kubuntu 18.04 – I mean who -m works, not sudo who -m returning “root”. – dessert May 09 '18 at 07:21
  • @dessert, I have Ubuntu Desktop 16.04 on my desktop computer also on my laptop. I have Ubuntu Server 16.04 installed on two different VPSs. which who returns /usr/bin/who on each of these systems. But on both Desktop systems there is no output when I type who -m. – pa4080 May 09 '18 at 08:41
  • @Panther, I read the discussion that you've shared. Yes the problem is similar. Another thing that I found is that when I'm logged in tty, on Ubuntu Desktop, who -m (who am i) works. Whatever, the accepted answer provides a solution of my actual problem. – pa4080 May 09 '18 at 16:58
  • 1

2 Answers2

13

I had a similar problem a while ago, and the only way that has worked reliably for me so far, is:

echo ${SUDO_USER:-${USER}}

As long as the user works with the shell as himself, $USER contains his username. When he calls sudo, for the new subshell $SUDO_USER is set to what is $USER in the calling shell, and $USER becomes root, of course.

The trick with the operator :- is, that the whole expression evaluates to $SUDO_USER if it is set (so inside the subshell opened by sudo), and otherwise to $USER. Therefore, you always have the correct username and don't have to worry much about the context the expression is evaluated in, which I found to be convenient.

pa4080
  • 29,831
Wanderer
  • 335
  • 4
  • 14
2

While the solution provided by Wanderer works as it is expected, and it is straight answer to the question, for the past few years when I want to force the script to be executed by certain (system) user I am using the special environment variable Eeffective UID - $EUID.

#!/bin/bash
SCRIPT_UID="33"
[[ $EUID -ne $SCRIPT_UID ]] && {
  echo "Please run as $(id $SCRIPT_UID -un), use: sudo -u $(id $SCRIPT_UID -un) $0"
  exit 1
}
echo "The script is running as $(id $EUID -un)."
pa4080
  • 29,831
  • 1
    This does not work. The OP wanted to know the UID of user who started sudo. This is in $SUDO_USER or $SUDO_UID. On the other hand, $EUID (as the name implies - effective UID) gives the UID of user sudo has switched to (exactly as used in your example). So it's the opposite of what OP asked. – raj Jul 25 '22 at 11:38
  • 1
    Hello, @raj, I'm totally agree with you! However, because I'm the original poster, I remember the main purpose of this question was to make such test, so I decided this is a good place to share also this solution. – pa4080 Jul 25 '22 at 12:10