Linux is a multi-user operating system, which means multiple users can connect and do work simultaneously. So, managing what each user can and can't do is important for the smooth operation of a system. And a group is a collection of users. Groups make it easy to manage users with the same security and access privileges. Users can be added to an existing group to utilize the privileges that it grants.
We can use the
groups command to know what groups a specific user belongs to,
groups usera usera sudo sambashare friends
And to see all the groups present on our system we can check the /etc/group file. Each line of this file represents information for one group.
1. ADD USER TO GROUP
For adding users to groups, we will be depending on the
usermod command. As an example, to add
userb to the
friends group the command will look like,
sudo usermod -a -G friends userb
And to add
userb to multiple groups (
vegan) in one go,
sudo usermod -a -G friends,leo,vegan userb
-a (append) switch is important. If it is not used in this command,
userb will be removed from any group not listed after the
2. REMOVE USER FROM GROUP
For removing a user from a group, the
gpasswd command with the
-d (delete) switch is used. As an example, to remove
userb from the
sudo gpasswd -d userb vegan
And if you are using Ubuntu or a flavour/variant of it, we can use the
deluser command as shown below to achieve the same result.
sudo deluser userb vegan
Note: Be careful when using
deluser, as the command name suggests, the command can be used for deleting users from the system.