How to check list of users in Unix

ADVERTISEMENTS



Command to check list of users in Unix

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.

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:



Understanding file format

Consider the last line:

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

Where,

vnstat – User name * – Encrypted password is stored in a in a separate file 284 – UID (User id) 284 – GID (Group id) vnStat Network Monitor – General information about the user /nonexistent – User’s home directory /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

Display Unix account information using logins command

The logins command shows information about user and system accounts. All you have to do is type the following command:

$ logins

root 0 wheel 0 Charlie & toor 0 wheel 0 Bourne-again Superuser daemon 1 daemon 1 Owner of many system processes operator 2 operator 5 System & bin 3 bin 7 Binaries Commands and Source tty 4 nogroup 65533 Tty Sandbox kmem 5 nogroup 65533 KMem Sandbox games 7 games 13 Games pseudo-user news 8 news 8 News Subsystem man 9 man 9 Mister Man Pages sshd 22 sshd 22 Secure Shell Daemon smmsp 25 smmsp 25 Sendmail Submission User mailnull 26 mailnull 26 Sendmail Default User bind 53 bind 53 Bind Sandbox unbound 59 unbound 59 Unbound DNS Resolver proxy 62 proxy 62 Packet Filter pseudo-user _pflogd 64 _pflogd 64 pflogd privsep user _dhcp 65 _dhcp 65 dhcp programs uucp 66 uucp 66 UUCP pseudo-user pop 68 mail 6 Post Office Owner auditdistd 78 audit 77 Auditdistd unprivileged user www 80 www 80 World Wide Web Owner _ypldap 160 _ypldap 160 YP LDAP unprivileged user hast 845 hast 845 HAST unprivileged user tests 977 tests 977 Unprivileged user for tests vivek 1001 vivek 1001 Vivek Gite nobody 65534 nobody 65534 Unprivileged user

To get information about the password change and user account expiration times, run:

$ logins -a

Only find and display information about tom and jerry accounts:

$ logins -l userName

$ logins -l tom,jerry

Want to see Unix user accounts with no password? Pass the -p option:

$ logins -p

Finally, we can see information about each account’s home directory and shell such as csh, ksh, bash and more:

$ logins -x

$ logins -x -l vivek

vivek 1001 vivek 1001 Vivek Gite /home/vivek /bin/tcsh

Conclusion