Managing Groups in Linux

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
The sed command in Linux stands for Stream Editor. It is a powerful utility that parses and transforms text, using a simple, compact programming language. sed is particularly useful for editing large files or streams without opening them in a traditi...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

Linux group management is an essential part of Linux system administration, allowing us to organize users into groups with pre-set permissions for easier management of file permissions and system resources.
There are 2 categories of groups in the Linux operating system:
Primary group - When a new user is created on Linux using the useradd command, a group with the same name as the username is also created, and the user is added as the group's sole member. This group is the user's primary group. A user has only one primary group at any given time.
Secondary (supplementary) groups - Additional groups are used to extend permissions and access rights of a user beyond what is provided by the primary group. For example, if users need access to a shared directory or need to execute tasks that require different group permissions, they can be added to the relevant secondary groups. Users can be members of multiple secondary groups.
/etc/group: This file is a critical configuration file that defines the groups to which users belong. It's a text file that contains one entry per line, each describing a group.

x or *), indicating that the group password is not used. Group passwords are a deprecated feature for managing group membership and access./etc/gshadow: This file complements the /etc/group file by providing a secure way to store group passwords and manage group membership. Similar to /etc/group, it contains one entry per line for each group, but with a focus on security-sensitive information.

There are several common group names you might encounter in Linux:
sudo β A member of this group can use the sudo command to elevate their privileges
cdrom β Allows the user to mount the optical drive
adm β Allows the user to monitor Linux system logs
lpadmin β Allows the user to configure printers
plugdev β Allows the user to access external storage devices

This hands-on shows:
how to add, delete, and modify groups;
how to add users to and remove from the existing groups.
To find out which groups a user is a member of, run:
groups

To find out which groups another user is a member of, run:
groups username

To create a new group in Linux, run:
sudo groupadd groupname
To confirm the new group is in the Linux /etc/group file:
getent group groupname
getent: The command used to retrieve entries from databases configured in the system.
group: This specifies the database to query. In this case, group refers to the group database, which contains information about the groups on the system.
groupname: This is the name of the group you want to query.

/etc/login.defs.To create a system group, run
sudo groupadd -r groupname

To modify a group name, use the groupmod command:
sudo groupmod -n new-name old-name

To add a user to an existing group while keeping them in their current groups, use the usermod command with the -a (append) and -G (groups) options:
sudo usermod -aG groupname username
Confirm if the user has been added by using the groups command.

An alternative for adding the user to an existing group is using the gpasswd command:
sudo gpasswd -a username groupname
gpasswd - a command used for managing /etc/group and /etc/gshadow group files. It allows administrators to modify group passwords and memberships, including adding or removing users from groups and setting or changing group passwords.
-a: Option that stands for "add", indicating that you want to add a user to a group.

To remove a user from a specific group, use gpasswd command with -d option:
sudo gpasswd -d username groupname

To delete a group, use the groupdel command:
sudo groupdel groupname
