UNIX / Linux Command To Check Existing Groups and Users

How do I check the existing Linux / UNIX users and groups under Linux operating system?

You can easily check the existing users and groups under a Linux or Unix-like systems such as HP-UX, AIX, FreeBSD, Apple OSX and more using the following commands:[donotprint][/donotprint]

[a] getent command : Fetch details for a particular user or group from a number of important text files called databases on a Linux or Unix-like systems. This is portable and recommended way to get information on users and groups.

[b] Directly query /etc/passwd for user names or /etc/group file for group names.

Method #1: getent command to lookup username and group name

The syntax is as follows to find out if user named foo exists in system:

getent passwd userNameHere
getent passwd foo

getent passwd userNameHere
getent passwd foo

The syntax is as follows to find out if group named bar exists in system:

getent group groupNameHere
getent group bar

getent group groupNameHere
getent group bar

Sample demo of all commands:

Fig.01: getent and friends demo on a Linux or Unix system to find out user and group names

Method #2: Find out if user exists in /etc/passwd file

/etc/passwd file stores essential information required during login. All you have to do is search this file for user name using the following syntax:
grep username /etc/passwd
OR
egrep -i "^username" /etc/passwd
For, example find out if vivek user exists or not, enter:
$ egrep -i "^vivek" /etc/passwd
OR
$ egrep -i "^vivek:" /etc/passwd
Sample outputs:

vivek:x:1000:1000:SXI ADMIN,,,,:/home/vivek:/bin/bash

A quick shell script code:

#!/bin/bash
# init
USERID="$1"
#....
/bin/egrep  -i "^${USERID}:" /etc/passwd
if [ $? -eq 0 ]; then
   echo "User $USERID exists in /etc/passwd"
else 
   echo "User $USERID does not exists in /etc/passwd"
fi
# ....

#!/bin/bash
# init
USERID="$1"
#….
/bin/egrep -i "^${USERID}:" /etc/passwd
if [ $? -eq 0 ]; then
echo "User $USERID exists in /etc/passwd"
else
echo "User $USERID does not exists in /etc/passwd"
fi
# ….

Normally, exit status is 0 returned if user accounts (lines) are found and 1 otherwise.

Use awk command to search user name

The syntax is as follows to search user named ‘apache’

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

awk -F’:’ ‘/^apache/{print $1}’ /etc/passwd

Find out if group exists in /etc/group file

/etc/group is an text file which defines the groups to which users belong under Linux and UNIX operating system. Again, you have to search /etc/group file using following syntax:
$ egrep -i "^groupname" /etc/group
For, example find out if vivek group exists or not, enter:
$ egrep -i "^vivek" /etc/group

Say hello to id command

The id command is another option to display user / group information for any USERNAME, or the current user. To find out more about user called, tom, enter:
$ id tom
Sample outputs:

uid=516(tom) gid=516(tom) groups=516(tom)

id command exit status is 0 returned if user accounts (lines) are found and 1 otherwise. A sample shell script using id command:

#!/bin/bash
USERID="$1"
/bin/id $USERID 2>/dev/null
[ $? -eq 0 ] && echo "User found" || echo "User not found"
 
/bin/id -g $USERID 2>/dev/null
[ $? -eq 0 ] && echo "Group found" || echo "Group not found"

#!/bin/bash
USERID="$1"
/bin/id $USERID 2>/dev/null
[ $? -eq 0 ] && echo "User found" || echo "User not found" /bin/id -g $USERID 2>/dev/null
[ $? -eq 0 ] && echo "Group found" || echo "Group not found"

Further readings:
  • Man pages – group(5)

Posted by: SXI ADMIN

The author is the creator of SXI LLC 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.