10

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
  • 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... Dec 29, 2016 at 18:15
  • 1
    But what about other OSes? Solaris?
    – Bulat M.
    Dec 29, 2016 at 18:28

2 Answers 2

20
$ 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
2
  • 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, 2016 at 18:30
  • 3
    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, 2016 at 22:03
5

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

ps -o uid,gid -p ...
5
  • 1. Could you give some insight white OpenBSD project did that? Interesting, accounting its security goals.
    – Bulat M.
    Dec 29, 2016 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, 2016 at 18:37
  • 2
    @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, 2016 at 19:01
  • 3
    @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. Dec 29, 2016 at 23:57
  • ps has OUTPUT MODIFIER, to see numeric UID, just pass n to ps, e.g. ps n -ef
    – 8.8.8.8
    Sep 6, 2019 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .