1

Is there any way to retrieve UID/GID of running process? Currently, I know only way of looking it up in htop. But I don't want to depend on third-party tool, prefer to use builtin unix commands. Could you suggest a few useful oneliners?

This didn't satisfy my curiousity:

How to programmatically retrieve the GID of a running process

top shows only user but not the group.

  • 2
    Why didn't the linked question satisfy your curiosity? On Linux, /proc/PID/status gives you the real, effective, saved set and file system uid and gid... – Stephen Kitt Dec 29 '16 at 18:15
  • But what about other OSes? Solaris? – Bulat M. Dec 29 '16 at 18:28
8
$ stat -c "%u %g" /proc/$pid/
1000 1000

or

$ egrep "^(U|G)id" /proc/$pid/status
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000

or with only bash builtins:

$ while read -r line;do [ "${line:1:2}" = "id" ] && echo $line;done < /proc/17359/status 
Pid: 17359
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
  • 1. Why [GU]id have four values in a row? 2. I use GNU/Linux. however, what commands to use on other fairly POSIX-compliant unix oses? – Bulat M. Dec 29 '16 at 18:30
  • 2
    Per the man page - man7.org/linux/man-pages/man5/proc.5.html - the 4 values represent ".....Real, effective, saved set, and filesystem..." – steve Dec 29 '16 at 22:03
2

Or assuming a *BSD system (for ps is unportable, and OpenBSD ditched /proc a bunch of releases ago now)

ps -o uid,gid -p ...
  • 1. Could you give some insight white OpenBSD project did that? Interesting, accounting its security goals. – Bulat M. Dec 29 '16 at 18:36
  • 2. Could you combine with above answers for GNU/Linux systems. So I could give my vote for complete answer. – Bulat M. Dec 29 '16 at 18:37
  • 1
    @Bulat M. See this thread on the openbsd-misc mailing list from 2009. The procfs filesystem was finally completely removed in release 5.7 (May 2015). – Kusalananda Dec 29 '16 at 19:01
  • 1
    @BulatM. This answer works on many Unix variants. If you use -o user,group instead of -o uid,gid, it works on all POSIX systems but displays names instead of numeric values. – Gilles Dec 29 '16 at 23:57

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.