-3

I noticed that RHEL 8 and Fedora 30 don't update the utmp file properly.

As a result, commands such as 'who am i', 'last', 'w' etc print incorrect results (who am i actually doesn't print anything)

After a bit of googling, I found 'logname' which worked in this case but I read that gnome is dropping support for utmp altogether so it's a matter of time until this stops working too.

I wrote the following script which finds the login name of the user (even if he is using sudo the moment he runs the command) but it's way too complicated so I'm looking for alternatives.

LOGIN_UID=$(cat /proc/self/loginuid)
LOGIN_NAME=$(awk -v val=LOGIN_UID -F ":" '$3==val{print $1}' /etc/passwd)

Is there a simple alternative which is not based on proper updating of /var/run/utmp ?

Edit 1: Solutions that don't work $HOME, $USER and id return incorrect values when used in a script that has been run with the sudo command. who am i and logname depend on utmp which isn't always updated by the terminal.

Working solution: After a bit of searching, a simpler way than the aforementioned was found in https://unix.stackexchange.com/users/5685/frederik-deweerdt 's comment to his own answer

Link to answer which contains the commment: https://unix.stackexchange.com/a/74312

Answer 1

stat -c "%U" $(tty)

Second answer found at https://stackoverflow.com/a/51765389/10630167

Answer 2

`pstree -lu -s $$ | grep --max-count=1 -o '([^)]*)' | head -n 1 | sed 's/[()]//g'`
pitprok
  • 388
  • 3
  • 15
  • 1
    Can you use the output of the `id` command? – Bodo May 17 '19 at 15:35
  • Unfortunately no, I need to dynamically generate the string "/home/username/". And if the root is logged in "/root" – pitprok May 17 '19 at 15:37
  • 1
    Then [edit] your question and add what you **really** want to do. Maybe you can use `$HOME` or `~` expansion. – Bodo May 17 '19 at 15:41
  • My question was about getting the username of the logged in user even when he has used the sudo command. For example, if I run a script with sudo, $HOME $USER and `id` all point towards the root user. The code I wrote above returns the "true" user (like who am i and logname used to do), but I was looking to find a simpler and easier to remember way. – pitprok May 20 '19 at 07:21
  • 2
    This is important background information that should be part of the question. With this information we would have known that some of the proposals will not work. Please [edit] your question. You may find some background information here: https://stackoverflow.com/questions/4598001 and a way to get the home directory for a given username: https://superuser.com/questions/484277/get-home-directory-by-username – Bodo May 20 '19 at 07:56
  • https://stackoverflow.com/a/51765389/10630167 from stackoverflow.com/questions/4598001 did indeed contain a valid answer – pitprok May 20 '19 at 08:14

1 Answers1

0

Your question is not well-defined because if X and Y are not working, what are the chances that Z will work? This depends entirely on the precise failure mode you are attempting to handle, and there is nothing in your question to reveal the specific circumstances in which you need this.

With that out of the way, perhaps look at the POSIX id command, which has explicit options to print the real (login) or effective (after any setuid command) user id with -r or -u, respectively. Of course, the precise means by which it obtains this information are not specified, and will remain implementation-dependent, and thus might or might not work on your platform under your specific circumstances.

As an aside, here is a refactoring of your code to avoid polluting the variable name space with two separate variables.

LOGIN_NAME=$(awk 'NR==FNR { val=$0; next } 
    $3==val{print $1}' /proc/self/loginuid FS=":" /etc/passwd)
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • If you run a script with sudo, `id` prints the root user's info, while `who am i` and `logname` would print the "true" logged in user. X and Y DO work, IF the utmp file is updated correctly, which gnome-terminal fails to do. – pitprok May 20 '19 at 07:22
  • `id -r` should output the real user ID. I'll update the answer to link to the POSIX documentation for this command. – tripleee May 20 '19 at 07:27
  • id -r and id -u both print 0 when run with sudo, so it still won't return the "real" username or uid. BUT using your idea, I found another SO question which had this as the response, which seems to work as intended "stat -c "%U" $(tty)" – pitprok May 20 '19 at 07:35