UNIX / Linux List Current Logged In Users

by on October 2, 2007 · 11 comments· LAST UPDATED March 12, 2013

in

How do I print the user names of users currently logged in to the current UNIX / Linux host / server from a command prompt?

You need to use any one of the following tool.

[a] w command - Shows information about the users currently on the machine, and their processes.

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsw or who command
Estimated completion timeN/A

[b] who command - Shows information about users who are currently logged in.

[c] users command - Shows the login names of the users currently on the system, in sorted order, space separated, on a single line. It reads all information from /var/run/utmp file.

Examples

Open a terminal (or login into remote server using ssh command) and type the following commands.

w command

Type the w command:
$ w
Sample outputs:

Fig.01: w command in action.

Fig.01: w command in action.


To see info about a user named tom, enter:
# w tom

who command

who command works on all Unix like operating systems:
# who
Sample outputs:

root     pts/0        2013-03-12 15:10 (10.1.3.177)

Pass the -a option to who command:
# who -a
Sample outputs:

           system boot  2013-03-02 04:10
           run-level 3  2013-03-02 04:10
LOGIN      /dev/ttyS1   2013-03-02 04:11              7951 id=v/tt
LOGIN      tty2         2013-03-02 04:11              7953 id=2
LOGIN      tty1         2013-03-02 04:11              7950 id=1
LOGIN      tty3         2013-03-02 04:11              7955 id=3
LOGIN      tty4         2013-03-02 04:11              7957 id=4
LOGIN      tty5         2013-03-02 04:11              7959 id=5
LOGIN      tty6         2013-03-02 04:11              7961 id=6
root     + pts/0        2013-03-12 15:10   .          7451 (10.1.3.177)
           pts/1        2013-03-08 12:29             23510 id=ts/1  term=0 exit=0

Other options

You can pass the following options to the who command (taken from the who command man page):

Just open a  -a, --all         same as -b -d --login -p -r -t -T -u
  -b, --boot        time of last system boot
  -d, --dead        print dead processes
  -H, --heading     print line of column headings
  -l, --login       print system login processes
      --lookup      attempt to canonicalize hostnames via DNS
  -m                only hostname and user associated with stdin
  -p, --process     print active processes spawned by init
  -q, --count       all login names and number of users logged on
  -r, --runlevel    print current runlevel
  -s, --short       print only name, line, and time (default)
  -t, --time        print last system clock change
  -T, -w, --mesg    add user's message status as +, - or ?
  -u, --users       list users logged in
      --message     same as -T
      --writable    same as -T
      --help     display this help and exit
      --version  output version information and exit

users command

Open a terminal or login over the ssh session and enter the following command:
$ users
Sample outputs:

abhi charvi vivek vivek zcafe
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 confy March 19, 2008 at 10:33 pm

but why not using “w”??

confy@live:~$ w
00:35:35 up 13 days, 1:35, 2 users, load average: 0.27, 0.34, 0.58
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
confy tty7 :0 06Mar08 0.00s 3:06 1.59s x-session-manager
confy pts/0 :0.0 00:33 0.00s 0.09s 0.00s w
confy@live:~$

Reply

2 هادی January 24, 2012 at 10:07 pm

tnx a lot

Reply

3 4F2E4A2E October 14, 2009 at 5:42 am

the awesome w :)

Reply

4 Ant May 27, 2011 at 1:28 am

I have a .txt file with denied users listed on it. Is there any way I can run a loop to regularly compare the ‘who’ or ‘users’ output with the list to see if denied users are logging on? I do not want to block the users completely. I am new to unix, any help would be much appreciated.

Reply

5 dikshitha November 18, 2011 at 5:31 am

we are using linux through putty.
I want to get an alert when user logged in.how can we write code for that?

Reply

6 totosus March 22, 2012 at 12:33 pm

write a bash script which stores a list of users from the previous output of users and runs it, say, once every ten seconds, and says “user user logged in/out” if something has changed. then run it with

script.sh &

. After that it will run in the background, but all output will go to your console, so you could work and see what it says. If you feel that you need to do so, you may add this line in the end of your

~/.profile

file, so that it starts every time you log in.

Reply

7 madami February 12, 2013 at 6:22 am

w – the better option here

Reply

8 Sushil R August 24, 2013 at 8:27 pm

Hi Sir,

I have a linux server, for which i have given ssh access to remote users.so now i want some kind of shell script who keep an eye on this ssh users and email me whenever they ssh into my machine.

sir it possible ? please reply

Reply

9 sulut October 17, 2013 at 2:10 am

Nice thread. it help sys.admin to show users inactive on a server

Reply

10 Wakatana October 20, 2013 at 7:34 pm

This does not print users logged throught X sessions

Reply

11 kishore April 7, 2014 at 11:06 am

Unforunately I killed root user with

pkill -STOP -u root on remote server, now I am unable to connect the remote server, what should I do now, please help me.

Thanks,
Kishore.

Reply

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: