0% found this document useful (0 votes)
173 views4 pages

UNIX - Linux Command To Check Existing Groups and Users

This document discusses several commands that can be used to check existing users and groups on Linux/Unix systems, including getent, grep, awk, and id. Getent allows looking up user and group names from password and group databases. Grep and awk can search the /etc/passwd and /etc/group files directly. The id command displays user and group IDs and can check if a user or group is found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views4 pages

UNIX - Linux Command To Check Existing Groups and Users

This document discusses several commands that can be used to check existing users and groups on Linux/Unix systems, including getent, grep, awk, and id. Getent allows looking up user and group names from password and group databases. Grep and awk can search the /etc/passwd and /etc/group files directly. The id command displays user and group IDs and can check if a user or group is found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/14/2016 UNIX/LinuxCommandToCheckExistingGroupsandUsers

ME NU

nixCraft
LinuxandUnixtutorialsfornewandseasonedsysadmin.

UNIX/LinuxCommandToCheckExistingGroups
andUsers
byV I V E K GI TE onJ UNE 27, 2008 las t updat ed A P RI L 6, 2015
inLI NUX ,UNI X ,US E R MA NA GE ME NT

H
owdoIchecktheexistingLinux/UNIXusersandgroups
underLinuxoperatingsystem?

YoucaneasilychecktheexistingusersandgroupsunderaLinuxor
UnixlikesystemssuchasHPUX,AIX,FreeBSD,AppleOSXandmoreusingthe
followingcommands:

[a]getentcommand:Fetchdetailsforaparticularuserorgroupfromanumberof
importanttextfilescalleddatabasesonaLinuxorUnixlikesystems.Thisisportable
andrecommendedwaytogetinformationonusersandgroups.

[b]Directlyquery/etc/passwdforusernamesor/etc/groupfileforgroupnames.

Method#1:getentcommandtolookupusernameandgroupname

Thesyntaxisasfollowstofindoutifusernamedfooexistsinsystem:

getentpasswduserNameHere
getentpasswdfoo

http://www.cyberciti.biz/faq/linuxcheckexistinggroupsusers/ 1/16
4/14/2016 UNIX/LinuxCommandToCheckExistingGroupsandUsers

Thesyntaxisasfollowstofindoutifgroupnamedbarexistsinsystem:

getentgroupgroupNameHere
getentgroupbar

Sampledemoofallcommands:

Fig.01:getentandfriendsdemoonaLinuxorUnixsystemtofindoutuserandgroupnames

Method#2:Findoutifuserexistsin/etc/passwdfile

/etc/passwdfilestoresessentialinformationrequiredduringlogin.Allyouhavetodois
searchthisfileforusernameusingthefollowingsyntax:

grepusername/etc/passwd

OR

egrepi"^username"/etc/passwd

For,examplefindoutifvivekuserexistsornot,enter:

$egrepi"^vivek"/etc/passwd

OR

http://www.cyberciti.biz/faq/linuxcheckexistinggroupsusers/ 2/16
4/14/2016 UNIX/LinuxCommandToCheckExistingGroupsandUsers

$egrepi"^vivek:"/etc/passwd

Sampleoutputs:

vivek:x:1000:1000:VivekGite,,,,:/home/vivek:/bin/bash

Aquickshellscriptcode:

#!/bin/bash
#init
USERID="$1"
#....
/bin/egrepi"^${USERID}:"/etc/passwd
if[$?eq0];then
echo"User$USERIDexistsin/etc/passwd"
else
echo"User$USERIDdoesnotexistsin/etc/passwd"
fi
#....

Normally,exitstatusis0returnedifuseraccounts(lines)arefoundand1otherwise.

Useawkcommandtosearchusername
Thesyntaxisasfollowstosearchusernamedapache

awkF':''/^apache/{print$1}'/etc/passwd

Findoutifgroupexistsin/etc/groupfile

/etc/groupisantextfilewhichdefinesthegroupstowhichusersbelongunderLinux
andUNIXoperatingsystem.Again,youhavetosearch/etc/groupfileusingfollowing
syntax:

$egrepi"^groupname"/etc/group

For,examplefindoutifvivekgroupexistsornot,enter:
http://www.cyberciti.biz/faq/linuxcheckexistinggroupsusers/ 3/16
4/14/2016 UNIX/LinuxCommandToCheckExistingGroupsandUsers

$egrepi"^vivek"/etc/group

Sayhellotoidcommand

Theidcommandisanotheroptiontodisplayuser/groupinformationforany
USERNAME,orthecurrentuser.Tofindoutmoreaboutusercalled,tom,enter:

$idtom

Sampleoutputs:

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

idcommandexitstatusis0returnedifuseraccounts(lines)arefoundand1otherwise.
Asampleshellscriptusingidcommand:

#!/bin/bash
USERID="$1"
/bin/id$USERID2>/dev/null
[$?eq0]&&echo"Userfound"||echo"Usernotfound"

/bin/idg$USERID2>/dev/null
[$?eq0]&&echo"Groupfound"||echo"Groupnotfound"

Furtherreadings:

Manpagesid(1),getent(1),passwd(5),group(5)

http://www.cyberciti.biz/faq/linuxcheckexistinggroupsusers/ 4/16

You might also like