Now,
let's explore how to create delete and modify local groups and group memberships.
And Linux,
each user can belong to one or more groups.
Now, why are these useful?
Here's an example,
let's say we have a directory full of files that our developers need to work on.
We need to create permissions for John
and Jack so they can do their job running the website
and then we hire Jane
and we must know all the permissions for John
and Jack and duplicate those exact permissions for Jane.
If any of them leave or change positions, we'll have to modify their permissions.
Again.
An elegant solution to this problem is to create a new group called developers.
The developers group is the owner of these files and we change permissions
so that the developers group can read and write to those files.
Then we add our three users to the developers group.
Now John being part of the developers group can easily
edit those files as well as Jack and Jane.
And if we want to temporarily deny John access,
we just remove him from the developers group
or if a new member joins our team,
we can just add their user account to the developers group with a simple
change and they'll have read and write access to all of those files.
We can see how this makes things
easier to understand from an administrator's perspective.
It's like assigning roles to user accounts
or like user accounts have a label,
is a developer or is not a developer all of this
by simply deciding if they will be part of the developer's group
or not a part of that group.
And speaking of roles for user accounts groups can have other special effects.
For example, being a part of some group can grant special privileges on the system.
Here are two common examples.
That's because as soon as the user logs in this becomes his or her main group,
but it's hard to understand with theory alone.
So let's see what's so special about this primary group.
Here are two practical examples.
When a user launches a program,
it is said that it runs under that user account and group
otherwise said the program runs with the same privileges
that the user account and primary group have.
And here's another more visible example.
When a user creates a file,
this file will automatically be owned by their
user account and their primary login group.
If you want to follow along with this exercise,
you'll need a user named John beforehand.
It's easy enough to create a new group called developers with a command like this.
But how do we add our user John to this new group?
The easiest way to add a user to a group is with the help of the G password command.
This name comes from the words group password,
but don't let the names confuse you
nowadays, group passwords are almost never used in practice.
So the main use case for the G PASSWD utility
is to add or remove users from certain groups.
And we can add John to our developers group with this command.
If we want to confirm that this worked,
we can see the groups that John belongs to with this command
groups. John, the first group after the colon is the primary or login group,
the rest are secondary or supplementary groups.
Now it's important not to confuse this with the capital
dash G as that option changes the secondary groups,
not the primary one
to avoid this mistake.
We can make a habit of using the equivalent long option dash G id instead of dash G.
And now we look at John's groups and we could say that the primary group was changed.
Note the difference G pass WD first expected the username, then the group name,
but usermod has the reverse order of group name and then username.
And you can use
gpasswd
help for a quick refresher.
Now to rename the group called developers to programmers. We can type in this
using groupmod or instead of dash dash, new dash name,
you can just do dash it
to delete the programmer group.
We use a command called groupdel
or group del.
Now you might see an error message like this
to fix this. We can change John's primary group back to the John group
and then we run it again
and it works
if a user is part of a secondary group and we want to delete it,
the command will work without issues.
However, if there are people using that group as their primary group,
you won't be able to delete it.