• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

Linux - members in group less than 99

Member of groups which have gid less than 99 ( root or any )  , I need a command that will give me output of the usere which are in this groups

Please help me with the command

0
apunkabollywood
Asked:
apunkabollywood
1 Solution
 
BlackAdderDKCommented:
Hi

The following script should do the trick:

#!/bin/bash
let userid=100

IFS="
"
for i in `cat /etc/passwd`
do
 let j=`echo $i | cut -f 3 -d ':' 2>/dev/null`

 if [ $j -le $userid ]
  then
    echo $i|awk -F":" '{ print "Username: " $1 "\t\tUID:" $3 }'
 fi
done
Regards
'Adder
0
 
Brad_WosmekCommented:
Assuming all users are in /etc/passwd you could use this grep:

grep -E .*\:.*\:.*\:\([0-9][0-9]\|[0-9]\)\:.*  /etc/passwd | cut -d: -f1

This expression will include gids <=99 not <99.  I suspect this is what you want though.
0
 
BlackAdderDKCommented:
Hi... again

Maybe I should read the whole ;)

This fits :)

#!/bin/bash
IFS="
"
for i in `cat /etc/passwd`
do
let j=`echo $i | cut -f 4 -d ':' 2>/dev/null`

if [ $j -le "99" ]; then
   echo $i|awk -F":" '{ print "Username: " $1 "\t\tGID:" $4 }'
fi
done

regards
'Adder
0
 
singhjagsirCommented:
Just one beautification to output, Idea/Script is same as BlackAdder

#!/bin/bash
IFS="
"
for i in `cat /etc/passwd`
do
let j=`echo $i | cut -f 4 -d ':' 2>/dev/null`

if [ $j -le "99" ]; then
   echo $i|awk -F":" '{ print "GID: " $4 "\t\tUsername: " $1 }'
fi
done |
 sort -n -k2

_J
#!/bin/bash
IFS="
"
for i in `cat /etc/passwd`
do
let j=`echo $i | cut -f 4 -d ':' 2>/dev/null`

if [ $j -le "99" ]; then
   echo $i|awk -F":" '{ print "GID: " $4 "\t\tUsername: " $1 }'
fi
done |
 sort -n -k2

Open in new window

0
 
simon3270Commented:
None of the above allows for auxiliary members in /etc/group.  In fact, it is better to start from /etc/group, which should list all groups, and find out which entries in /etc/passwd have each group as its main one.

The attached script reads through each group in /etc/group and, for each one 99 or less, lists out the /etc/passwd users with that group as the main group, and the auxiliary members listed in /etc/group.

If, for a given group ID, there are no users in /etc/passwd  with that group as their main group, and there are no auxiliary members, the output will just show the group ID followed by a ":".

If there are any users with that group as their main one, or there are auxiliary members, both of these are listed after the groupID and ":".

If a user has a particular group as its main group ID, and is also listed in the auxiliary group members in /etc/group for that group ID, it will be shown twice (see "root" for group ID 0 in the list below, or "bin" for group ID 1.)

The output from my Linux box starts:
0: root,sync,shutdown,halt,operator,admin,root,admin
1: bin,root,bin,daemon
2: daemon,root,bin,daemon
3: mcadmin,root,bin,adm
4: adm,root,adm,daemon
5:
6: root
7: lp,daemon,lp
8:
9:
10: root

awk -F: '$3 <= 99{print $3,$4}' /etc/group | sort -n | while read gid mems
do
  mainmem=$(awk -F: '$4 ~ /^'${gid}'$/{print $1}' /etc/passwd)
  mainmem=$(echo $mainmem | tr ' ' ',')
  if [ "X$mainmem" = "X" ]
  then
    if [ "X$mems" = "X" ]
    then
      echo ${gid}:
    else
      echo ${gid}: ${mems}
    fi
  else
    if [ "X$mems" = "X" ]
    then
      echo ${gid}: ${mainmem}
    else
      echo ${gid}: ${mainmem},${mems}
    fi
  fi
done

Open in new window

0

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now