About Linux FAQ

Browse More FAQs:

UNIX / Linux Command To Check Existing Groups and Users

Posted by Vivek Gite [Last updated: June 27, 2008]

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"
 

Further readings:

  • Refer to id, passwd, group man pages

E-mail this to a friend      Printable version

Related Other Helpful FAQs:

Discussion on This FAQ

  1. z0mbix Says:

    don’t forget the “:” after the username otherwise you could end up with this scenario:

    $ egrep -i “^vivek” /etc/passwd
    vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash
    viveks:x:1001:1001:Vivek Smith,,,,:/home/viveks:/bin/bash

  2. Ramesh | The Geek Stuff Says:

    If you are using NIS do the following:

    ypcat passwd | grep vivek

    Ramesh
    The Geek Stuff

  3. Pedro Says:

    The ‘id’ command should be demonstrated first in this tutorial, as systems using LDAP (other or remote authentication services) will not have users in the local {passwd,group} files.

    Also why the uses of egrep when a simple grep will do. Keep it simple for the beginners your aiming at.

  4. davidhi Says:

    You should look at getent rather than grepping the local files. “getent passwd” or “getent group” will provide a unified view of users or groups available, respecting your NSS (Name Service Switch) configuration (which is important when you have additional users or groups via LDAP or NIS).

  5. himadri Says:

    hey Vivek, that was cool..

    many of us surely wont care if its grep or egrep ( or fgrep) as long as it does the job and we are taught these wonderful tricks..

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.