How to view current logged in users in linux ?

Today we will look various different ways  to view current logged in users in linux.

The user login data is usually saved and read from /var/run/utmp file.

Lets see different command which read this binary file and display data.

Logged In Users Details on Linux
1. w command
w is most widely used and handy command available on most of linux distributions.

### see all currently logged in users
[root@nglinux ~]# w
 00:53:21 up  5:33,  7 users,  load average: 0.08, 0.02, 0.01
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
nglinux  tty1     :0               19:23    5:33m  9.97s  0.14s pam: gdm-password
nglinux  pts/0    :0.0             19:24    5:29m  0.00s  0.00s bash
nglinux  pts/1    :0.0             19:24    5:29m  0.00s  0.00s bash
nglinux  pts/2    :0.0             19:24    5:28m  0.13s  0.12s Terminal
nglinux  pts/3    :0.0             19:24    5:29m  0.00s  0.00s bash
root     pts/4    172.21.49.223    19:25    0.00s  0.08s  0.01s w
root     pts/5    172.21.49.223    20:13    3:06m  0.05s  0.05s -bash
[root@nglinux ~]# 

### Get information about a specific user say root.
[root@nglinux ~]# w root
 00:57:12 up  5:37,  7 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/4    172.21.49.223    19:25    0.00s  0.08s  0.00s w root
root     pts/5    172.21.49.223    20:13    3:10m  0.05s  0.05s -bash
[root@nglinux ~]# 

2. who command

[root@nglinux ~]# who
nglinux  tty1         May 30 19:23 (:0)
nglinux  pts/0        May 30 19:24 (:0.0)
nglinux  pts/1        May 30 19:24 (:0.0)
nglinux  pts/2        May 30 19:24 (:0.0)
nglinux  pts/3        May 30 19:24 (:0.0)
root     pts/4        May 30 19:25 (172.21.49.223)
root     pts/5        May 30 20:13 (172.21.49.223)
[root@nglinux ~]# 

### who -a shows all available details with logged in users
### it includes details with options -bdlprTtu.
###      -b    Time of last system boot.
###      -d    Print dead processes.
###      -l    Print system login processes (unsupported).
###      -p    Print active processes spawned by launchd(8) (unsupported).
###      -r    Print the current runlevel.      
###      -T    Print a character after the user name indicating the state of the
               terminal line: `+' if the terminal is writable; `-' if it is not;
               and `?' if a bad line is encountered.

###      -t    Print last system clock change (unsupported).

###      -u    Print the idle time for each user, and the associated proces

[root@nglinux ~]# who -a
           system boot  May 30 19:19
           run-level 5  May 30 19:19
LOGIN      tty2         May 30 19:19              2226 id=2
LOGIN      tty3         May 30 19:19              2228 id=3
LOGIN      tty4         May 30 19:19              2234 id=4
LOGIN      tty5         May 30 19:19              2236 id=5
LOGIN      tty6         May 30 19:19              2240 id=6
nglinux  + tty1         May 30 19:23  old         2423 (:0)
nglinux  + pts/0        May 30 19:24 05:32        4034 (:0.0)
nglinux  + pts/1        May 30 19:24 05:32        4034 (:0.0)
nglinux  + pts/2        May 30 19:24 05:31        4034 (:0.0)
nglinux  + pts/3        May 30 19:24 05:32        4034 (:0.0)
root     + pts/4        May 30 19:25   .          4137 (172.21.49.223)
root     + pts/5        May 30 20:13 03:09        4271 (172.21.49.223)
[root@nglinux ~]# 

3. Users command
It also reads all information from /var/run/utmp file.

### Shows usernames of all logged in users.
[root@nglinux ~]# users 
nglinux nglinux nglinux nglinux nglinux root root

4. whoami command

### Shows currently Logged in User
[root@nglinux ~]# whoami 
root

5. id command

### Id command shows current logged in user id with username.
### however shows only current user details.
[root@nglinux ~]# id -a
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

Leave a Reply

avatar
  Subscribe  
Notify of