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.

Bulat M.
  • 397
  • 2
  • 5
  • 14

2 Answers2

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
Ipor Sircer
  • 14,376
  • 1
  • 27
  • 34
  • 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
  • 3
    Per the man page - http://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
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 ...
thrig
  • 34,333
  • 3
  • 63
  • 84
  • 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
  • 2
    @Bulat M. See [this thread](https://marc.info/?t=125341144400003&r=1&w=2) on the openbsd-misc mailing list from 2009. The `procfs` filesystem was finally completely removed in [release 5.7](https://www.openbsd.org/57.html) (May 2015). – Kusalananda Dec 29 '16 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. – Gilles 'SO- stop being evil' Dec 29 '16 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 06 '19 at 05:55