Linux show all users in group

This post and this website contains affiliate links. See my disclosure about affiliate links.

linux: how to list users?…show all users or just local users

Linux doesn’t provide a straight forward command to list all users in the system. You can list users who are currently logged in, or you can find groups that the user belongs to, but really no simple way to list users or to get a comprehensive list of all users in the system or a list of users in a specific group.

However, this being Linux there are several different commands that you can use to list users, if you are willing to use the command line to stitch some commands together. First, you might be better off by understanding how and where the user names and the associated user data is stored in Linux to understand how and why the commands detailed below will work.

/etc/passwd

The /etc/passwd file holds all the necessary information about the local users such as the user id, password info, login, primary group and home directory. But this file also lists the local users to the system including pseudo users such as adm, mail, news, apache etc etc. These are pseudo users that the system uses to run some applications or services and are not real world users with login privileges.

This file however does not include users that are remote to the system but can log in to the system, such as users in external databases like NIS or LDAP.

/etc/group

The /etc/group file holds the information pertaining to the user groups in the system. There is a field in the file that lists the logins of users who belong to that particular group. This field can be useful when listing the users.

Now lets’ see how you might be able to list users in the system.

list all local linux users

First let’s see how we can list all the local users in the system. You can list all the local users by doing a simple cat of the passwd (/etc/passwd) file.

This prints out all the information on the file. If you are just interested in the login names of the users, then you can cut out just the pertinent information. The login name is the first field in each line separated by the colon (:) delimiter.

You can format your output so that it prints out more than just the login names. In order to print out the full name and home directory along with the login, you can use the awk command…

You can substitute the variables ($1, $5 and $6 in the above example command) to print out more (or less) information as needed. You can add more fields to the list as well.

Читайте также:  Как установить firefox линукс

Another command in Linux that essentially does the same is the passwd command. You can get essentially the same information as detailed above by using the passwd command as in the example below.

To print out some of other user information such as password change date, minimum age, max age and inactivity period etc, you will need to use the passwd command instead of the passwd file directly.

list users in linux

list only “real” linux users

The above example lists users irrespective of their status or privileges which means it will also contain the pseudo users as described above. If you like to list only “real world” users with login privileges and a home directory which is probably what you meant by “list users“, then you will need to filter the above command output to weed out these pseudo users.

Let’s assume that the real users on the system have a home directory at /home.

$ cat /etc/passwd | grep ‘/home’ | cut -d: -f1

If you happen to have users on multiple partitions such as /h1, /h2 and /h3 ….then you can modify the grep command to filter all of them out using some regular expressions such as in the example below.

$ cat /etc/passwd | grep -E ‘:/h1|:/h2|:/h3’ | cut -d: -f1

Remember that you will need to filter the users using grep before using cut to print only the login names. The above examples will list users that have a home directory, under the assumption that all “real” users will have a home directory.

list all users in linux

The above lists only the local users. If you like to get a list all the users that have access to the system across many database such as NIS, LDAP etc, then the command you can use is getent. You can use the cut, grep and awk commands to modify and format the output as described the previous commands.

If you would like to list only logged in users, then you will need to use other linux commands such as who, w or finger.

list all linux users in a group

Again there is no single and simple command to do this. The /etc/group file contains all the group names and the users who are in that group, but may or may not have the users who have the particular group as their primary group. That information is stored in the /etc/passwd file and it uses the group id rather than the group name.

$ grep ^usb /etc/groups | cut -d: -f4

The above command will list users who are in the user group usb, but may not (sometimes it does though, depending on your linux distro or configuration!) include the users who happens to have usb as their primary group. We can find all the users whose primary group is usb by looking at the /etc/passwd file.

We need to get the group id first, from the group file for the group usb, for that we will have to grep the /etc/group and get the third field which is the group id.

$ grep ^usb /etc/group | cut -d: -f3

Now, we can plug this into another grep command that will search the /etc/passwd file. This will print out the users that are in the passwd file with the primary group usb.

$ grep :`grep ^usb /etc/group | cut -d: -f3`: /etc/passwd

Читайте также:  Linux edit binary file

Now, merging all of the above commands into a single line command….

$ grep ^usb /etc/group |cut -d: -f4 && grep :`grep ^usb /etc/group | cut -d: -f3`: /etc/passwd | cut -d: -f1

Ok, that is definitely a long command to type out if you do this often. You do have the option of converting this into an alias or a shell script so that you can use it easily and repeatedly without having to retype.

This still might list some users twice if they exist in both the group as well as the passwd files, and the output is comma separated and in multiple lines. You might also want to convert this into a shell script that accepts the group as an argument. I will leave that as an assignment for you.

Источник

How to List All Users of a Group in Linux

Wondering which users are members of a specific group? This tutorial shows you three simple ways to list all the users of a group in Linux command line.

I have already shown you to list all the users in Linux command line. This quick tip is related and yet different from that.

I presume that you are a bit familiar with the concept of groups and users in Linux. There are several groups and a variety of users in a Linux system. A group can have multiple members while a user can be a member of several groups.

You can check which groups a particular user belongs to and you can also find all the users of a group.

List all users of a group in Linux

List Group Members In Linux

In this quick tutorial, I’ll show you different ways to list users in a group in Linux command line.

1. List members of a group in Linux using /etc/group file

The group information is contained in the file /etc/group. You can view the content of this file and look for the information about the members.

Normally, this file has entry in the following format:

Here’s the explanation of the fields:

  • adm is the group name
  • x represents password field (you won’t see password in clear text of course)
  • 4 is the Group ID aka GID
  • syslog and abhishek are the users belonging to the group adm

If you find manual searching for a group in the file difficult, you can use a combination of the grep command and the cut command.

grep '^group_name:.*$' /etc/group | cut -d: -f4

The above command looks for all the lines starting with the specified group name and then the cut command extract the fourth column separated with : delimiter. The result is just the name of the group members.

grep '^adm:.*$' /etc/group | cut -d: -f4 syslog,abhishek

2. List group members in Linux with getent command

getent is a multipurpose command that is used to query from database files in the /etc directory. So you can use it to query the /etc/group file and get the users of the specified group in the following manner:

This will display the line matching the group name and in here you can see the members of the group:

getent group sudo sudo:x:27:abhishek

3. List users in a group using ‘members’ command

There is a tiny command line tool that simplifies the process of listing all the members of a specific group.

Читайте также:  What is encryption in linux

The members command is usually not installed in all the systems so you have to install it on your own.

On Debian/Ubuntu based systems, you can install it using the following command:

If the command is not found in Ubuntu, you should enable the universe repository and try it again.

Once you have the command installed, you can run it like this:

For example, if you want to check which users have sudo access, you can use the members command like this:

And the output will list all the users of the sudo group.

See, it was absolutely simple to get the users belonging to a group. You learned three ways to do it.

Which method did you like the most? Or, do you use some other way to list group members in Linux? Why not share it with us here?

Источник

How to list all users in a Linux group?

I’m new here, I found out that SF exists right after I posted the question. I agree it belongs either to SF or SO.

20 Answers 20

It is portable across both Linux and Solaris, and it works with local group/password files, NIS, and LDAP configurations.

Unfortunately, there is no good, portable way to do this that I know of. If you attempt to parse /etc/group, as others are suggesting, you will miss users who have that group as their primary group and anyone who has been added to that group via a mechanism other than UNIX flat files (i.e. LDAP, NIS, pam-pgsql, etc.).

If I absolutely had to do this myself, I’d probably do it in reverse: use id to get the groups of every user on the system (which will pull all sources visible to NSS), and use Perl or something similar to maintain a hash table for each group discovered noting the membership of that user.

Edit: Of course, this leaves you with a similar problem: how to get a list of every user on the system. Since my location uses only flat files and LDAP, I can just get a list from both locations, but that may or may not be true for your environment.

Edit 2: Someone in passing reminded me that getent passwd will return a list of all users on the system including ones from LDAP/NIS/etc., but getent group still will still miss users that are members only via the default group entry, so that inspired me to write this quick hack.

 #!/usr/bin/perl -T # # Lists members of all groups, or optionally just the group # specified on the command line # # Copyright © 2010-2013 by Zed Pobre (zed@debian.org or zed@resonant.org) # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # use strict; use warnings; $ENV = "/usr/bin:/bin"; my $wantedgroup = shift; my %groupmembers; my $usertext = `getent passwd`; my @users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm; foreach my $userid (@users) < my $usergrouptext = `id -Gn $userid`; my @grouplist = split(' ',$usergrouptext); foreach my $group (@grouplist) < $groupmembers-> = 1; > > if($wantedgroup) < print_group_members($wantedgroup); >else < foreach my $group (sort keys %groupmembers) < print "Group ",$group," has the following members:\n"; print_group_members($group); print "\n"; >> sub print_group_members < my ($group) = @_; return unless $group; foreach my $member (sort keys %<$groupmembers>) < print $member,"\n"; >> 

Источник

Оцените статью
Adblock
detector