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]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1m
[/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

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

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

vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash

A quick shell script code:

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

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

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/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)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 22 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
22 comments… add one
  • z0mbix Jun 27, 2008 @ 12:15

    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

    • dkp Apr 6, 2015 @ 4:36

      I really wish the author would update the article to include that because you know 7 years later and still no fix? Worse yet, it’s the first Google search result for “linux check if group exists”.

  • Ramesh | The Geek Stuff Jun 27, 2008 @ 14:29

    If you are using NIS do the following:

    ypcat passwd | grep vivek


  • Pedro Jun 27, 2008 @ 16:00

    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.

  • davidhi Jun 27, 2008 @ 18:26

    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).

  • himadri Aug 9, 2008 @ 18:45

    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..

  • New in linux Jul 15, 2009 @ 9:08

    Can you please tell me a command to list all of existing user ?

    • Shahin Dec 15, 2011 @ 5:43

      U can try
      egrep “*” /etc/passwd
      egrep “?” /etc/passwd

  • 🐧 nixCraft Jul 15, 2009 @ 9:09


    cut -d: -f1 /etc/passwd | less
  • Praba Mar 17, 2010 @ 4:00

    Very nice site, I could get, what i want in seconds rather than in minutes

  • Rob Nov 14, 2011 @ 11:22

    `id` comand does not check if groups exist.
    `man id`

    Print user and group information for the specified USERNAME

    the -g flag prints out the primary group id for the user

    • sohaib Dec 29, 2011 @ 17:05

      have you find any solution for that?

  • sachin Nov 15, 2011 @ 10:56

    Linux Gurus,
    Is there a Command to find out user creation date ?

    or any other possible ways to find the same.

    please help me
    Its urgent.

    Thanks In Advance

  • manh Mar 14, 2012 @ 3:58

    please tell everyone you ask.
    no way to list the user is not disabled in linux.
    and has been in how long dis.

  • Sebastian Mar 26, 2012 @ 3:24

    The grep approaches are all wrong. You are assuming that an user won’t pick a name that is a started substring of an existing group. Even worse, if you choose to limit the ‘username’ string you could match a group instead of a user. You will mistakenly get output from the script thinking that the user ‘apache’ (or whatever) exists…

    You can’t play with strings without semantics. You need a tool that in fact *knows* that what you are talking about is indeed a user.

    The best approach for not playing with strings semantics is the id command:

    id – print real and effective user and group IDs


  • SMougenot Aug 1, 2012 @ 10:29

    As davidhi mentioned
    Using getent is a much better solution in my opinion

    # search for user named ‘vivek’
    getent passwd vivek

    #search for group named ‘vivek’
    getent group vivek

  • shri May 24, 2014 @ 13:17

    Thank you :-)

  • yash Mar 4, 2015 @ 16:40



    Resolved my issue..!!!

  • Shahzad Aslam Aug 25, 2015 @ 12:13

    To get the list of all users only shows their usernames
    getent passwd | awk -F: ‘{print $1}’
    To get exact number of user currently logged in
    w | grep “users” | awk -F’ ‘ ‘{print $4}’

    Hope it worked & solve ur issue
    Tested well in centOS 7

    Thanks ,

  • sharmeen Jan 11, 2016 @ 15:20

    I m new to Linux,i want to disable root login..what is the command for that…if I do that will I still be able to login as root…

  • khajamuzaffar Nov 19, 2016 @ 7:12

    “more /etc/passwd” for listing out all users in linux

  • Chinta Nov 25, 2016 @ 5:12

    You Rock!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum