- Linux Show All Members of a Group Command
- Linux Show All Members of a Group Commands
- Linux: List all members of a group using /etc/group file
- Display group memberships for each Linux user
- Linux List all members of a group using members command
- How to list all users in a Linux group using lid command
- To see users contained in group named ‘ftponly’:
- To show information about groups containing user named ‘nixcraft’:
- How to list groups in Linux
- Conclusion
- How to List All Users of a Group in Linux
- List all users of a group in Linux
- 1. List members of a group in Linux using /etc/group file
- 2. List group members in Linux with getent command
- 3. List users in a group using ‘members’ command
- Linux List All Users In The System Command
- Linux list all users account using the /etc/passwd file
- How to list users in Linux using pagers
- Linux list user names only
- Get a list of all users using the getent command
- Find out whether a user account exists in the Linux server
- How to count user accounts in the Linux server
- A Note About System and General Users
- Users and groups
- Contents
- Overview
- Permissions and ownership
- Shadow
- File list
- User management
- Example adding a user
- Example adding a system user
- Change a user’s login name or home directory
- Other examples of user management
- User database
- Automatic integrity checks
- Group management
- Group list
- User groups
- System groups
- Pre-systemd groups
- Unused groups
- Other tools related to these databases
Linux Show All Members of a Group Command
Linux Show All Members of a Group Commands
- /etc/group file – User group file
- members command – List members of a group
- lid command (or libuser-lid on newer Linux distros) – List user’s groups or group’s users
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | None |
Est. reading time | 1m |
There are two types of groups in Linux:
- Primary group – is the main group that is associated with user account. Each user is a member of exactly one primary group.
- Secondary group – used to provide additional rights to user. For example, access to the dvd/cdrom drive can be granted with help of cdrom group.
Linux: List all members of a group using /etc/group file
Use the grep command or cat command/more command as follows:
$ grep ‘grpup-name-here’ /etc/group
$ grep ‘ftponly’ /etc/group
$ cat /etc/group
$ less /etc/group
$ grep -i —color ‘ftponly’ /etc/group
We can also type the compgen command or getend command to list all group names on Linux:
$ compgen -g
$ getent group
To get just a list of all members of a group called ftponly , type the following awk command:
Display group memberships for each Linux user
Want to see group memberships for each given USERNAME under Linux? The syntax is as follows for the groups command:
groups
groups
groups vivek
The following outputs indicates that the user named ‘vivek’ is part of four groups including ‘vivek’ primary group:
Linux List all members of a group using members command
Warning: members command is not installed on most Linux distros. Use yum command or apt-get command/apt command to install the same:
$ sudo apt-get install members
To outputs members of a group called ftponly, enter:
$ members
$ members ftponly
Fig. 01: members command in action to list members in a group
How to list all users in a Linux group using lid command
You can displays information about groups containing user name, or users contained in group name using lid command as follows.
Warning: lid command is not installed on most distros. Use yum command or apt-get command to install the same:
$ sudo apt-get install libuser
To see users contained in group named ‘ftponly’:
# lid -g ftponly
Please note that newer version of libuser renamed the lid command to libuser-lid . Thus, use it as follows:
$ sudo libuser-lid -g ftponly
Sample outputs:
To show information about groups containing user named ‘nixcraft’:
Use lid command or libuser-lid command on Linux to show all members of a group named nixcraft:
# lid nixcraft
OR
$ sudo libuser-lid nixcraft
Sample outputs:
See lid command man page for more information.
How to list groups in Linux
To see all users, run less command/more command:
less /etc/group
OR
more /etc/group
Another option is to type the following getent command:
getent group
For example, locate the members of a group with the name vboxusers, run:
getent group vboxusers
Sample outputs indicating vivek and raj users are part of vboxusers group:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Finally, you can use the id command to display real and effective user and group IDs:
id
id vivek
id -nG raj # show all group IDs for raj user
id -ng raj # show only effective group ID for raj user
Conclusion
Now you know how to use various Linux commands to show all members of a group. I suggest you read the man pages for more info by typing the following man command:
$ man libuser-lid
$ man members
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to List All Users of a Group in Linux
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
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.
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.
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:
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.
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.
That’s it…
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?
Источник
Linux List All Users In The System Command
Tutorial requirements | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Requirements | Linux | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Root privileges | No | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Difficulty | Easy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Est. reading time | 5 mintues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
File | Purpose |
---|---|
/etc/shadow | Secure user account information |
/etc/passwd | User account information |
/etc/gshadow | Contains the shadowed information for group accounts |
/etc/group | Defines the groups to which users belong |
User management
To list users currently logged on the system, the who command can be used. To list all existing user accounts including their properties stored in the user database, run passwd -Sa as root. See passwd(1) for the description of the output format.
To add a new user, use the useradd command:
-m / —create-home the user’s home directory is created as /home/username . The directory is populated by the files in the skeleton directory. The created files are owned by the new user. -G / —groups a comma separated list of supplementary groups which the user is also a member of. The default is for the user to belong only to the initial group. -s / —shell a path to the user’s login shell. Ensure the chosen shell is installed if choosing something other than Bash.
If an initial login group is specified by name or number, it must refer to an already existing group. If not specified, the behaviour of useradd will depend on the USERGROUPS_ENAB variable contained in /etc/login.defs . The default behaviour ( USERGROUPS_ENAB yes ) is to create a group with the same name as the username.
When the login shell is intended to be non-functional, for example when the user account is created for a specific service, /usr/bin/nologin may be specified in place of a regular shell to politely refuse a login (see nologin(8) ).
See useradd(8) for other supported options.
Example adding a user
To add a new user named archie , creating its home directory and otherwise using all the defaults in terms of groups, folder names, shell used and various other parameters:
Although it is not required to protect the newly created user archie with a password, it is highly recommended to do so:
The above useradd command will also automatically create a group called archie and makes this the default group for the user archie . Making each user have their own group (with the group name same as the user name) is the preferred way to add users.
You could also make the default group something else using the -g option, but note that, in multi-user systems, using a single default group (e.g. users ) for every user is not recommended. The reason is that typically, the method for facilitating shared write access for specific groups of users is setting user umask value to 002 , which means that the default group will by default always have write access to any file you create. See also User Private Groups. If a user must be a member of a specific group specify that group as a supplementary group when creating the user.
In the recommended scenario, where the default group has the same name as the user name, all files are by default writeable only for the user who created them. To allow write access to a specific group, shared files/folders can be made writeable by default for everyone in this group and the owning group can be automatically fixed to the group which owns the parent directory by setting the setgid bit on this directory:
Otherwise the file creator’s default group (usually the same as the user name) is used.
If a GID change is required temporarily you can also use the newgrp command to change the user’s default GID to another GID at runtime. For example, after executing newgrp groupname files created by the user will be associated with the groupname GID, without requiring a re-login. To change back to the default GID, execute newgrp without a groupname.
Example adding a system user
System users can be used to run processes/daemons under a different user, protecting (e.g. with chown) files and/or directories and more examples of computer hardening.
With the following command a system user without shell access and without a home directory is created (optionally append the -U parameter to create a group with the same name as the user, and add the user to this group):
If the system user requires a specific user and group ID, specify them with the -u / —uid and -g / —gid options when creating the user:
Change a user’s login name or home directory
To change a user’s home directory:
The -m option also automatically creates the new directory and moves the content there.
Make sure there is no trailing / on /my/old/home .
To change a user’s login name:
Changing a username is safe and easy when done properly, just use the usermod command. If the user is associated to a group with the same name, you can rename this with the groupmod command.
Alternatively, the /etc/passwd file can be edited directly, see #User database for an introduction to its format.
Also keep in mind the following notes:
- If you are using sudo make sure you update your /etc/sudoers to reflect the new username(s) (via the visudo command as root).
- Personal crontabs need to be adjusted by renaming the user’s file in /var/spool/cron from the old to the new name, and then opening crontab -e to change any relevant paths and have it adjust the file permissions accordingly.
- Wine’s personal folders/files’ contents in
/.local/share/applications/wine/Programs and possibly more need to be manually renamed/edited.
or $HOME variables for home directories.
Other examples of user management
To enter user information for the GECOS comment (e.g. the full user name), type:
(this way chfn runs in interactive mode).
Alternatively the GECOS comment can be set more liberally with:
To mark a user’s password as expired, requiring them to create a new password the first time they log in, type:
User accounts may be deleted with the userdel command:
The -r option specifies that the user’s home directory and mail spool should also be deleted.
To change the user’s login shell:
User database
Local user information is stored in the plain-text /etc/passwd file: each of its lines represents a user account, and has seven fields delimited by colons.
- account is the user name. This field can not be blank. Standard *NIX naming rules apply.
- password is the user password.
Broken down, this means: user jack , whose password is in /etc/shadow , whose UID is 1001 and whose primary group is 1003. Jack Smith is his full name and there is a comment associated to his account; his home directory is /home/jack and he is using Bash.
The pwck command can be used to verify the integrity of the user database. It can sort the user list by GID at the same time, which can be helpful for comparison:
Automatic integrity checks
Instead of running pwck / grpck manually, the systemd timer shadow.timer , which is part of, and is enabled by, installation of the shadow package, will start shadow.service daily. shadow.service will run pwck(8) and grpck(8) to verify the integrity of both password and group files.
If discrepancies are reported, group can be edited with the vigr(8) command and users with vipw(8) . This provides an extra margin of protection in that these commands lock the databases for editing. Note that the default text editor is vi, but an alternative editor will be used if the EDITOR environment variable is set, then that editor will be used instead.
Group management
/etc/group is the file that defines the groups on the system (see group(5) for details). There is also its companion gshadow which is rarely used. Its details are at gshadow(5) .
Display group membership with the groups command:
If user is omitted, the current user’s group names are displayed.
The id command provides additional detail, such as the user’s UID and associated GIDs:
To list all groups on the system:
Create new groups with the groupadd command:
Add users to a group with the gpasswd command (see FS#58262 regarding errors):
Alternatively, add a user to additional groups with usermod (replace additional_groups with a comma-separated list):
Modify an existing group with the groupmod command, e.g. to rename the old_group group to new_group :
To delete existing groups:
To remove users from a group:
The grpck command can be used to verify the integrity of the system’s group files.
Group list
This section explains the purpose of the essential groups from the filesystem package. There are many other groups, which will be created with correct GID when the relevant package is installed. See the main page for the software for details.
User groups
Non-root workstation/desktop users often need to be added to some of following groups to allow access to hardware peripherals and facilitate system administration:
Group | Affected files | Purpose |
---|---|---|
adm | Administration group, commonly used to give read access to protected logs. It has full read access to journal files. | |
ftp | /srv/ftp/ | Access to files served by FTP servers. |
games | /var/games | Access to some game software. |
http | /srv/http/ | Access to files served by HTTP servers. |
log | Access to log files in /var/log/ created by syslog-ng. | |
rfkill | /dev/rfkill | Right to control wireless devices power state (used by rfkill). |
sys | Right to administer printers in CUPS. | |
systemd-journal | /var/log/journal/* | Can be used to provide read-only access to the systemd logs, as an alternative to adm and wheel [1]. Otherwise, only user generated messages are displayed. |
uucp | /dev/ttyS6+ , /dev/tts/5+ , /dev/ttyUSB9+ , /dev/ttyACM9+ , /dev/rfcomm2+ | RS-232 serial ports and devices connected to them. |
wheel | Administration group, commonly used to give privileges to perform administrative actions. It has full read access to journal files and the right to administer printers in CUPS. Can also be used to give access to the sudo and su utilities (neither uses it by default). |
System groups
The following groups are used for system purposes, an assignment to users is only required for dedicated purposes:
Group | Affected files | Purpose |
---|---|---|
dbus | used internally by dbus | |
kmem | /dev/port , /dev/mem , /dev/kmem | |
locate | /usr/bin/locate , /var/lib/locate , /var/lib/mlocate , /var/lib/slocate | See Locate. |
lp | /dev/lp3* , /dev/parport9* | Access to parallel port devices (printers and others). |
/usr/bin/mail | ||
nobody | Unprivileged group. | |
proc | /proc/pid/ | A group authorized to learn processes information otherwise prohibited by hidepid= mount option of the proc file system. The group must be explicitly set with the gid= mount option. |
root | /* | Complete system administration and control (root, admin). |
smmsp | sendmail group. | |
tty | /dev/tty , /dev/vcc , /dev/vc , /dev/ptmx | |
utmp | /run/utmp , /var/log/btmp , /var/log/wtmp |
Pre-systemd groups
Before arch migrated to systemd, users had to be manually added to these groups in order to be able to access the corresponding devices. This way has been deprecated in favour of udev marking the devices with a uaccess tag and logind assigning the permissions to users dynamically via ACLs according to which session is currently active. Note that the session must not be broken for this to work (see General troubleshooting#Session permissions to check it).
There are some notable exceptions which require adding a user to some of these groups: for example if you want to allow users to access the device even when they are not logged in. However, note that adding users to the groups can even cause some functionality to break (for example, the audio group will break fast user switching and allows applications to block software mixing).
Group | Affected files | Purpose |
---|---|---|
audio | /dev/audio , /dev/snd/* , /dev/rtc0 | Direct access to sound hardware, for all sessions. It is still required to make ALSA and OSS work in remote sessions, see ALSA#User privileges. Also used in JACK to give users realtime processing permissions. |
disk | /dev/sd[a-zA-Z]*7* | Access to block devices not affected by other groups such as optical , floppy , and storage . |
floppy | /dev/fd5* | Access to floppy drives. |
input | /dev/input/event1* , /dev/input/mouse9* | Access to input devices. Introduced in systemd 215 [2]. |
kvm | /dev/kvm | Access to virtual machines using KVM. |
optical | /dev/sr2 , /dev/sg8 | Access to optical devices such as CD and DVD drives. |
scanner | /var/lock/sane | Access to scanner hardware. |
storage | /dev/st1*[lma]* , /dev/nst2*[lma]* | Used to gain access to removable drives such as USB hard drives, flash/jump drives, MP3 players; enables the user to mount storage devices.[3] Now solely for direct access to tapes if no custom udev rules is involved.[4][5][6][7] |
video | /dev/fb/0 , /dev/misc/agpgart | Access to video capture devices, 2D/3D hardware acceleration, framebuffer (X can be used without belonging to this group). |
Unused groups
The following groups are currently not used for any purpose:
Group | Affected files | Purpose |
---|---|---|
bin | none | Historical |
daemon | ||
lock | Used for lockfile access. Required by e.g. gnokii AUR . | |
mem | ||
network | Unused by default. Can be used e.g. for granting access to NetworkManager (see NetworkManager#Set up PolicyKit permissions). | |
power | ||
uuidd | ||
users | The primary group for users when user private groups are not used (generally not recommended), e.g. when creating users with USERGROUPS_ENAB no in /etc/login.defs or the -N / —no-user-group option of useradd. |
Other tools related to these databases
This article or section is a candidate for merging with #Shadow.
The factual accuracy of this article or section is disputed.
getent(1) can be used to read a particular record.
As warned in #User database, using specific utilities such as passwd and chfn , is a better way to change the databases. Nevertheless, there are times when editing them directly is looked after. For those times, vipw , vigr are provided. It is strongly recommended to use these tailored editors over using a general text editor as they lock the databases against concurrent editing. They also help prevent invalid entries and/or syntax errors. Note that Arch Linux prefers usage of specific tools, such as chage, for modifying the shadow database over using vipw -s and vigr -s from util-linux . See also FS#31414.
Источник