How to check list of users in Unix

in Categories , , last updated January 2, 2018

I am a new Unix system user. How can I view system users in Unix operating system using the command line option?

The password file /etc/passwd contains one line for each user account. The passwd files are the local source of password information. Some system stores encrypted passwords in a separate file for security reasons. For example, on a FreeBSD you have /etc/master.passwd or on a Linux you have /etc/shadow file. The /etc/group file is the local source of group information/membership. This tutorial explains how to check the list of users in Unix operating system such as macOS, FreeBSD, OpenBSD, NetBSD, Unix-like system.

Command to check list of users in Unix

On a FreeBSD/OpenBSD/NetBSD and many other Unix-like system, 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

Sample outputs:
How to check list of users in Unix

Understanding file format

Consider the last line:

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

Where,

  1. vnstat – User name
  2. * – Encrypted password is stored in a 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
Sample outputs:

root
daemon
operator
bin
build
sshd
....

_iscsid
_smtpq
_file
_radiusd
_eigrpd
_vmd
_tftp_proxy
_ftp_proxy
_sndiop
_syspatch
_slaacd
nobody
vivek

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

Use the grep command as follows:
$ grep '^userNameHere' /etc/passwd
$ grep '^vivek' /etc/passwd

Sample outputs:

vivek:*:1001:1001:vivek:/home/vivek:/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 command:
$ more /etc/group
$ less /etc/group
$ grep vivek /etc/group

See also

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 1 comments/add one below):

1 comment

    Have a question? Post it on our forum!