10

I'm trying to see if id -r will print out the UID or username of the user who logged into the system despite any su's or sudo's. I'm interested in doing this so I can keep people a little more accountable and to tailor script functioning accordingly (i.e: they issue a sudo on a script and it pulls information from the logged in user's home directory).

I realize sudo sets SUDO_USER but I don't want to rely on this because it's a variable that can be modified by the user, and it just has the username of the user who issued the most recent sudo (i.e: sudo -i ; sudo -iu randomUser ; echo $SUDO_USER prints out "root" instead of the actual user).

Nothing in the man pages or that I can find online seems to indicate what the proper use of this command is and the obvious permutations aren't working:

[root@ditirlns03 ~]# id -r
id: cannot print only names or real IDs in default format
[root@ditirlns03 ~]# id -r jadavis6
id: cannot print only names or real IDs in default format
[root@ditirlns03 ~]# id -r root
id: cannot print only names or real IDs in default format

At this point, I'm still not sure id -r is going to print out what I want, mostly because I can't figure out how to get it to print out anything at all.

imz -- Ivan Zakharyaschev
  • 15,113
  • 15
  • 61
  • 123
Bratchley
  • 16,684
  • 13
  • 64
  • 103

3 Answers3

8

-r must be used in conjunction with another option. For example:

$ id -Gr
1000 4 24 27 30 46 109 124

Quoting the man page:

-r, --real
print the real ID instead of  the  effective  ID,  with -ugG
Frederik Deweerdt
  • 3,722
  • 17
  • 18
  • 1
    Yeah I had tried that but when looking at the output it printed 0 for `id -ru` so I thought I did something wrong. Do you know of a better way to get the user ID or username as it would appear in audit logs (the `auid`)? – Bratchley May 01 '13 at 15:53
  • 2
    It sounds like you're interested in getting the owner of the current tty. The following should do the trick for you: `stat -c "%U" $(tty)`. But that's really a different question. If you started another question it'd be easier to find. – Frederik Deweerdt May 01 '13 at 17:17
  • 1
    It looks like a bug to me. – Pavel Šimerda Feb 13 '20 at 08:29
2

The program and the docs deviate from standard practices:

If -ugG actually means -u|g|G[modifier] (which apparently it does), then it should be documented that way. The authors didn't seem to think it was worth the effort. However, when the user does the same thing (as Mr. Davis quite reasonably tried):

-ru, a legitimate, intuitive assumption, returns an error.

Apropos this tip:

stat -c "%U" $(tty)

Probably would have helped to mention that it's shell-specific.

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
-1
  • Printing the userid: id -u
  • Printing the username: id -u -n

Try man id to lean more.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Stanflows
  • 9
  • 1