Command to check list of users in Unix

On a FreeBSD/OpenBSD/NetBSD and many other Unix-like systems, just type the following cat command/more command/less command to get a list of all user accounts:


$ cat /etc/passwd
$ more /etc/passwd
$ less /etc/passwd

Understanding file format

Consider the last line:

vnstat:*:284:284:vnStat Network Monitor:/nonexistent:/usr/sbin/nologin

Where,

  1. vnstat – Username
  2. * – Encrypted password is stored in a separate file
  3. 284 – UID (User Id)
  4. 284 – GID (Group id)
  5. vnStat Network Monitor – General information about the user
  6. /nonexistent – User’s home directory
  7. /usr/sbin/nologin – User’s login shell


How to just display a list of user names

Use the cut command as follows:


$ cut -d: -f1 /etc/passwd

OR use awk command:


$ awk -F':' '{ print $1}' /etc/passwd


How do I search for a given user name such as vyga

Use the grep command as follows:

$ grep '^userNameHere' /etc/passwd
$ grep '^vyga' /etc/passwd


Sample outputs:

vyga:*:1001:1001:vyga:/home/vyga:/bin/tcsh


How to use getent command to find out a list of users

To get entries from administrative database such as /etc/passwd use the getent command as follows:


$ getent passwd
$ getent passwd | more
$ getent passwd | grep vivek


A note about macOS Unix users

If you are using a macOS, try the following command to check list of users in Unix cli (open the Terminal app and type the following bash command):


$ dscl . list /Users

OR


$ dscacheutil -q user

The dscl is a general-purpose utility for operating on Directory Service directory nodes.

How to find which Unix users are logged in and what they are doing

Type the following w command/who command:


$ w

OR


$ who


How do I see available list of groups on my server?

Type any one of the following commands:


$ more /etc/group
$ less /etc/group
$ grep vyga/etc/group


That's it

Was this answer helpful? 0 Users Found This Useful (0 Votes)