by using this command we can get groups under that username
groups linux
no username is passed then this command will display group membership for the current user
$ groups
it id of linux group
$ id linux
it gives all the group names
$ id -ng
by using this we can get the list of all groups
getent groups
print the first field containg the group name
$getent group| awk -F: '{print $1}'
cuts the first filed and displays only the group name
$ getent group cut -d: -f1
Example Output:
devops sudo docker
Explanation:
Displays all groups the current user belongs to.
groups
Output Example:
alice : alice developers sudo
Explanation:
Shows all groups that alice belongs to.
groups username
Output Example:
root : root admin sudo
Explanation:
Displays all groups assigned to the root user.
sudo groups root
Explanation:
Displays all system groups and their members.
getent group
Example:
groups alice | grep -w sudo
Explanation:
Checks if alice belongs to the sudo group.
groups username | grep -w groupname
Output Example:
sudo:x:27:alice,bob,admin
Explanation:
Shows all users in the sudo group
getent group sudo
Explanation:
Adds user alice to the docker group.
sudo usermod -aG docker alice
Explanation:
Removes user alice from the developers group.
sudo deluser alice developers
Example:
id -gn bob
Output Example:
bob
Explanation:
Shows the primary group of user bob.
id -gn username
Example:
id -G -n alice
Output Example:
alice developers sudo
Explanation:
Displays group names instead of numeric IDs.
id -G -n username
Displays the groups a user belongs to.
groups pritesh