0% found this document useful (0 votes)
33 views

UNIX - Linux Command To Check Existing Groups and Users

This document describes how to check for existing users and groups in Linux using the /etc/passwd and /etc/group files and the id command. The egrep command can be used to search these files for a specific username or groupname. The id command displays user and group information for a given username and exits with a status of 0 if the user is found or 1 if not found, which can be checked in shell scripts.

Uploaded by

Shakeel Mohammed
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

UNIX - Linux Command To Check Existing Groups and Users

This document describes how to check for existing users and groups in Linux using the /etc/passwd and /etc/group files and the id command. The egrep command can be used to search these files for a specific username or groupname. The id command displays user and group information for a given username and exits with a status of 0 if the user is found or 1 if not found, which can be checked in shell scripts.

Uploaded by

Shakeel Mohammed
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

A. You can easily check the existing users and groups under Linux using the following
commands.

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 following syntax:
$ egrep -i "^username" /etc/passwd
For, example find out if vivek user exists or not, enter:
$ egrep -i "^vivek" /etc/passwd
Sample output:

vivek:x:1000:1000:Vivek Gite,,,,:/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
# ....

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

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

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

id: tom: No such user


To find out ftpuser group, enter:
$ id -g ftpuser
Sample output:

id: ftpuser: No such user

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"
 

You might also like