[opensuse] list of users of primary group
Hey, can someone tell me how to get users list of some primary group. For example, I know only the name of this group: users. This is primary group and I can see it in id(1) output. How to get list of users who has the same prrmary group? Without needing to parse files. getent(1) and id(1) can do it, but it works contrariwise: it returns name of primary group (by giving user name). Thanks for info! Cheers, Alex -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Thu, 15 Feb 2018 11:07:55 +0100 Alex Naumov <alexander_naumov@opensuse.org> wrote:
Hey,
can someone tell me how to get users list of some primary group. For example, I know only the name of this group: users. This is primary group and I can see it in id(1) output. How to get list of users who has the same prrmary group? Without needing to parse files.
I don't think there's a simple answer. Search unix.stackexchange to see what's possible. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 02/15/2018 06:09 AM, Dave Howorth wrote:
On Thu, 15 Feb 2018 11:07:55 +0100 Alex Naumov <alexander_naumov@opensuse.org> wrote:
Hey,
can someone tell me how to get users list of some primary group. For example, I know only the name of this group: users. This is primary group and I can see it in id(1) output. How to get list of users who has the same prrmary group? Without needing to parse files.
I don't think there's a simple answer. Search unix.stackexchange to see what's possible.
A stupidly simple but not terribly accurate answer is: #> less /etc/passwd |grep <users group number> I say not terribly accurate because in my system, users group is 100 and grep returns not only 100 but anything with 100 in it. You did say without parsing files. That is as close as I can get without more coffee. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Thu, Feb 15, 2018 at 3:34 PM, Stevens <fred-n-sandy@myrhinomail.com> wrote:
A stupidly simple but not terribly accurate answer is: #> less /etc/passwd |grep <users group number>
I say not terribly accurate because in my system, users group is 100 and grep returns not only 100 but anything with 100 in it. You did say without parsing files. That is as close as I can get without more coffee.
The problem in your example is not parsing. My GID can come from, for example, LDAP. In that case you will get nothing. I found this solution:
getent passwd | awk -F: '{if($4=='$gid')print $1}'
But I still have some problems. One of these is - how to convert name to number clear. I don't want to parse /etc/group for that. And I ask myself all the time - why there is no clear solution for that? It's hard to believe that this simple task can be solved only by parsing... -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 02/15/2018 04:08 PM, Alex Naumov wrote:
The problem in your example is not parsing. My GID can come from, for example, LDAP. In that case you will get nothing.
I found this solution:
getent passwd | awk -F: '{if($4=='$gid')print $1}'
But I still have some problems. One of these is - how to convert name to number clear. I don't want to parse /etc/group for that.
That wouldn't help because also the groups may come from LDAP. What about: $ getent passwd | awk -F: -v gid="$(getent group users | cut -d: -f3)" '$4 == gid' ? Or below the longer variant as script. Have a nice day, Berny #!/bin/sh # Get group name from parameter. gname="$1" \ && test "$gname" \ || { echo "Usage: $0 <group-name>" >&2; exit 1; } # Determine GID of given group. gid="$(getent group "$gname" | cut -d: -f3)" \ && test "$gid" \ || { echo "$0: error: getting GID of group failed: '$gname'" >&2; exit 1; } set -o pipefail getent passwd | awk -F: -v gid="$gid" '$4 == gid' #-eof -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Berny -- ...and then Bernhard Voelker said... % % On 02/15/2018 04:08 PM, Alex Naumov wrote: % > The problem in your example is not parsing. % > My GID can come from, for example, LDAP. In that case you will get nothing. % > ... % % That wouldn't help because also the groups may come from LDAP. Good point to you both. % % What about: % % $ getent passwd | awk -F: -v gid="$(getent group users | cut -d: -f3)" '$4 == gid' [snip] Ooooh! I like (except for the bash/ksh-centric () instead of `` form) that! Use a $G holder to make calling it easy and you could just call it right out of your history. HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 15.02.2018 16:08, Alex Naumov wrote:
The problem in your example is not parsing. My GID can come from, for example, LDAP. In that case you will get nothing.
I did not try, but if you use sssd, you should be possible to use it for that. You can use sss_groupshow to show information about a group, containing users. But you have to configure sssd correctly: - you have to include the "local" domain in sssd.conf - you have to enable "enumerate" in your sssd.conf Please read the warning about "enumerate TRUE" in "man sssd.conf"!
Alex & Stevens, et al -- ...and then Stevens said... % % On 02/15/2018 06:09 AM, Dave Howorth wrote: % >On Thu, 15 Feb 2018 11:07:55 +0100 % >Alex Naumov <alexander_naumov@opensuse.org> wrote: % > % >>can someone tell me how to get users list of some primary group. ... % >>needing to parse files. % > % >I don't think there's a simple answer. Search unix.stackexchange to see % >what's possible. I am inclined to agree; there is no simple and easy program that tells you that. You're gonna be parsing files. % % A stupidly simple but not terribly accurate answer is: % #> less /etc/passwd |grep <users group number> % % I say not terribly accurate because in my system, users group is 100 % and grep returns not only 100 but anything with 100 in it. You did % say without parsing files. That is as close as I can get without % more coffee. Well, the easy fix for this is to anchor the pattern. Something like grep ":100:" /etc/passwd (BTW, you don't have to cat or less through grep) will exclude 1100 and 1000 and so on. Unfortunately, it will also give you the user 100. The only way to limit to groups is to look at the GID field. To avoid hard-coding the GID, you should first check in the /etc/groups file to see what it is. Realistically this is pretty easy to dash off in a one-liner (with a placeholder var to make it easy; if it were a shell script you'd take the group name as input): G=groupname awk -F: -vGID=`egrep "^$G:" /etc/group | cut -d: -f3` '{if ($4==GID) print $1}' </etc/passwd Season to taste :-) I'd be VERY interested in how this could be condensed, by the way! HTH & HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Feb 15, 2018, at 10:19:08, David T-G <davidtg-robot@justpickone.org> wrote:
Alex & Stevens, et al --
...and then Stevens said... % % On 02/15/2018 06:09 AM, Dave Howorth wrote: % >On Thu, 15 Feb 2018 11:07:55 +0100 % >Alex Naumov <alexander_naumov@opensuse.org> wrote: % > % >>can someone tell me how to get users list of some primary group. ... % >>needing to parse files. % > % >I don't think there's a simple answer. Search unix.stackexchange to see % >what's possible.
I am inclined to agree; there is no simple and easy program that tells you that. You're gonna be parsing files.
% % A stupidly simple but not terribly accurate answer is: % #> less /etc/passwd |grep <users group number> % % I say not terribly accurate because in my system, users group is 100 % and grep returns not only 100 but anything with 100 in it. You did % say without parsing files. That is as close as I can get without % more coffee.
Well, the easy fix for this is to anchor the pattern. Something like
grep ":100:" /etc/passwd
(BTW, you don't have to cat or less through grep) will exclude 1100 and 1000 and so on. Unfortunately, it will also give you the user 100. The only way to limit to groups is to look at the GID field.
To avoid hard-coding the GID, you should first check in the /etc/groups file to see what it is. Realistically this is pretty easy to dash off in a one-liner (with a placeholder var to make it easy; if it were a shell script you'd take the group name as input):
G=groupname awk -F: -vGID=`egrep "^$G:" /etc/group | cut -d: -f3` '{if ($4==GID) print $1}' </etc/passwd
Season to taste :-) I'd be VERY interested in how this could be
Let me see if I can write simple C program using glibc system functions for iterating over /etc/password and getting primary GID for every user. Should be doable, but it's been a while since I worked with the functions used to iterate and get information from the password struct. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Thu, Feb 15, 2018 at 4:43 PM, Tom Kacvinsky <tom.kacvinsky@suse.com> wrote:
Let me see if I can write simple C program using glibc system functions for iterating over /etc/password and getting primary GID for every user. Should be doable, but it's been a while since I worked with the functions used to iterate and get information from the password struct.
If you really want to implement it, I will recommend you to add new parameter for id(1) or getent(1). It's a bit more complicated than just parses /etc/passwd and /etc/group. As I mentioned before, data can come from LDAP or NIS and id(1) has such requests already. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Hello, On Thu, 15 Feb 2018, David T-G wrote:
G=groupname awk -F: -vGID=`egrep "^$G:" /etc/group | cut -d: -f3` '{if ($4==GID) print $1}' </etc/passwd
Season to taste :-) I'd be VERY interested in how this could be condensed, by the way!
awk -F: -vG=groupname ' BEGIN { while( getline <"/etc/group" ) { if( $1 == G ) { GID=$3; } } } $4 == GID { print $1; } ' < /etc/passwd HTH, -dnh -- Listen, three eyes, don't you try to outweird me. I get stranger things than you free with my breakfast cereal. -- Zaphod Beeblebrox -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
David, et al -- ...and then David Haller said... % % Hello, Hiya! % % On Thu, 15 Feb 2018, David T-G wrote: ... % >Season to taste :-) I'd be VERY interested in how this could be % >condensed, by the way! % % awk -F: -vG=groupname ' % BEGIN { % while( getline <"/etc/group" ) { % if( $1 == G ) { GID=$3; } % } % } % $4 == GID { print $1; } % ' < /etc/passwd Hmmm... Nice in that it doesn't make a separate call to look through the group file, but I don't know that I could go so far as to say that that's condensed from my previous one-liner :-) % % HTH, % -dnh HAND :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Hello, On Thu, 15 Feb 2018, David T-G wrote:
% On Thu, 15 Feb 2018, David T-G wrote: ... % >Season to taste :-) I'd be VERY interested in how this could be % >condensed, by the way! % % awk -F: -vG=groupname ' % BEGIN { % while( getline <"/etc/group" ) { % if( $1 == G ) { GID=$3; } % } % } % $4 == GID { print $1; } % ' < /etc/passwd
Hmmm... Nice in that it doesn't make a separate call to look through the group file, but I don't know that I could go so far as to say that that's condensed from my previous one-liner :-)
Yes. Oh, BTW, you can also call getent for both groups and passwd. ==== $ getent passwd | awk -F: -vG=groupname ' BEGIN { while( "getent group" | getline ) { if( $1 == G ) { GID=$3; } ; } } $4 == GID { print $1; }' ==== So it'll work with NIS/LDAP too. -dnh -- 135: Druckertreiber pure virtual Araber (Martin Neumann) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Hello, On Thu, 15 Feb 2018, Alex Naumov wrote:
getent(1) and id(1) can do it, but it works contrariwise: it returns name of primary group (by giving user name).
Wrong. Try this: $ getent group users [| cut -d: -f4] -dnh -- "If you are using an Macintosh e-mail program that is not from Microsoft, we recommend checking with that particular company. But most likely other e-mail programs like Eudora are not designed to enable virus replication" -- http://www.microsoft.com/mac/products/office/2001/virus_alert.asp -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 02/15/2018 02:07 AM, Alex Naumov wrote:
Hey,
can someone tell me how to get users list of some primary group. For example, I know only the name of this group: users. This is primary group and I can see it in id(1) output. How to get list of users who has the same prrmary group? Without needing to parse files.
getent(1) and id(1) can do it, but it works contrariwise: it returns name of primary group (by giving user name).
Thanks for info!
Cheers, Alex
... or, you could simply go to Yast=> User and Group Management, and click on the groups tab. -- -Gerry Makaro aka Fraser_Bell on the forums, IRC, and mail at openSUSE.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
participants (9)
-
Alex Naumov
-
Bernhard Voelker
-
Dave Howorth
-
David Haller
-
David T-G
-
Florian Gleixner
-
Fraser_Bell
-
Stevens
-
Tom Kacvinsky