Vous êtes sur la page 1sur 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

Vous aimerez peut-être aussi