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.


-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
  • 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 1 '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. May 1 '13 at 17:17
  • It looks like a bug to me. Feb 13 '20 at 8:29

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.

  • Printing the userid: id -u
  • Printing the username: id -u -n

Try man id to lean more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.