id, logname, groups, users, w, who, whoami and who am i

The id commands is used to show the real and effective UIDs and GIDs of users. Called with a username it shows the username, primary and secondary groups:

root:~> id marc
uid=1000(marc) gid=1000(marc) groups=1000(marc),10(wheel)

If we use it without any argument, it shows the information above for our effective UID plus the SELinux context:

root:~> id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0­s0:c0.c1023

We can also extract a subset of that information. The -u, -g and -G flags show the UID, primary GID and secondary GIDs respectively:

root:~> id ­-u
root:~> id ­-g
root:~> id -­G

We can show the data above in name rather than numeric format with -n:

root:~> id ­-un
root:~> id ­-gn
root:~> id ­-Gn

With either -u, -g or -G we can query the real user/group rather than the effective one shown by default with the -r flag:

root:~> id ­-unr
root:~> id ­-gnr
root:~> id ­-Gnr

If we need to feed either of the 3 items above to a script, we can do so with the -z flag to avoid having to strip the carriage return with awk/sed:

root:~> id -­unr
root:~> id ­-unrz


If we want to know what user we originally logged in as (disregarding any subsequent su / sudo changes) we can find that with the logname command:

root:~> logname


The groups command shows the primary & secondary groups for our own user or for others:

marc:~/.vim> groups
marc wheel
marc:~/.vim> groups qemu
qemu : qemu kvm

If we just want to find out the names of those users currently logged in we can use users:

root:~> users
marc marc alex marc

We can also show logged in users with the w command:

root:~> w
.12:04:27 up 12 days, 21:25, 4 users, load average: 1.16, 1.22, 1.19
USER      TTY      LOGIN@     IDLE      JCPU      PCPU    WHAT
marc      :0       15Oct15    ?xdm?     16:41m    0.62s   gdm­session­worker [pam/gdm­password]
marc      pts/0    15Oct15    10.00s    2.79s     10:35   /usr/libexec/gnome­terminal­server
marc      pts/3    16Oct15    0.00s     1:32      10:35   /usr/libexec/gnome­terminal­server
marc      pts/1    Mon19      1:39m     0.02s     10:35   /usr/libexec/gnome­terminal­server

The JCPU time is the time used by all processes attached to the tty, including the currently running background jobs. Background jobs already terminated are not accounted for.

The PCPU time is the time used by the current process, named in the “what” field.

We can use the flags -s for short output and -i to show IPs instead of DNSes:

root:~> w ­-s
12:06:43 up 12 days, 21:27, 4 users, load average: 1.10, 1.20, 1.19
USER      TTY      IDLE     WHAT
marc      :0       ?xdm?    gdm­session­worker [pam/gdm­password]
marc      pts/0    2:26     /usr/libexec/gnome­terminal­server
marc      pts/3    2.00s    /usr/libexec/gnome­terminal­server
marc      pts/1    1:41m    /usr/libexec/gnome­terminal­server
root:~> w -­i
12:06:55 up 12 days, 21:27, 4 users, load average: 1.07, 1.19, 1.19
USER      TTY      FROM    LOGIN@      IDLE      JCPU      PCPU      WHAT
marc      :0       :0      15Oct15     ?xdm?     16:42m    0.62s     gdm­session­worker [pam/gdm­password]
marc      pts/0    :0      15Oct15     2:38      2.79s     10:36     /usr/libexec/gnome­terminal­server
marc      pts/3    :0      16Oct15     3.00s     1:32      10:36     /usr/libexec/gnome­terminal­server
marc      pts/1    :0      Mon19       1:41m     0.02s     10:36     /usr/libexec/gnome­terminal­server


With whoami we shall get our effective user ID:

root:~> whoami


With who am i we shall see the user that is logged to our terminal (us?) plus the terminal and login time.

root:~> who am i
marc pts/3 2015­10­16 15:29 (:0)


The who command shows us who is logged in the system now …

root:~> who
marc     :0        2015­10­15 15:39 (:0)
marc     pts/0     2015­10­15 15:40 (:0)
marc     pts/3     2015­10­16 15:29 (:0)
marc     pts/1     2015­10­26 19:59 (:0)

… but it can also show us the time of last system boot …

root:~> who ­­–boot –­­heading
NAME     LINE         TIME                 PID      COMMENT
system boot  2015­10­15 15:39

… system login processes …

root:~> who –­­login –­­heading
NAME     LINE     TIME     IDLE      PID     COMMENT

… dead processes …

root:~> who ­­–dead ­­–heading
NAME        LINE      TIME            IDLE      PID      COMMENT      EXIT
.           pts/2     2015­10­26 17:57              0      id=/2        term=0 exit=0
.           pts/4     2015­10­21 20:27              0      id=/4        term=0 exit=0

… counts …

root:~> who ­­–count –heading
marc marc marc marc
# users=4

… current runlevel …

root:~> who ­­–runlevel –­­heading
NAME      LINE           TIME           IDLE      PID       COMMENT
.         run­level 5     2015­10­15 15:39

… time of last system clock change …

root:~> who –­­time –­­heading
NAME       LINE       TIME         PID       COMMENT

… and normal output plus idle time and PIDs …

root:~> who ­­–users –­­heading
NAME      LINE     TIME             IDLE       PID      COMMENT
marc      :0       2015­10­15 15:39      ?      2067      (:0)
marc      pts/0    2015­10­15 15:40  01:11      2681      (:0)
marc      pts/3    2015­10­16 15:29      .      2681      (:0)
marc      pts/1    2015­10­26 19:59  00:12      2681      (:0)

<< last, lasb, lslogins