In Linux, groups are a collection of users. They are used as a means to manage and control access to resources such as files and directories. By assigning appropriate permissions to groups rather than individual users, system administrators can more efficiently manage access rights.
find /path/to/base/dir -type d | xargs chmod 755
find /path/to/base/dir -type f | xargs chmod 644
or changing files with specific extention:
find /path/to/base/dir -iname "*.php" | xargs chmod 644
The key reasons for using groups in Linux are:
- Ease of management: Instead of granting or revoking access rights to specific users one by one, administrators can simply add users to or remove them from groups.
- Security: Groups can ensure that only authorized users have access to specific resources.
- Collaboration: Groups make it easier for a set of users, such as a project team, to share access to files.
Here are some essential command examples related to groups in Linux:
IMPORTANT: Refreshing groups sometimes needs to having to re-login.
- Create a new group: To create a new group, you can use the
groupadd
command. For example, to create a group named 'developers', you would use:
sudo groupadd developers
- Add a user to a group: To add a user to a group, you can use the
usermod
command. For example, to add a user 'john' to the 'developers' group, you would use:
sudo usermod -aG developers john
The -aG
option stands for append (-a
) to group (-G
).
- Remove a user from a group:
sudo usermod -rG developers john
To remove a user from a group, you can use the gpasswd
command. For example, to remove 'john' from the 'developers' group, you would use:
sudo gpasswd -d john developers
- Delete a group: To delete a group, you can use the
groupdel
command. For example, to delete the 'developers' group, you would use:
sudo groupdel developers
- Change the group ownership of a file or directory: To change the group ownership of a file or directory, you can use the
chgrp
command. For example, to change the group ownership of a directory named 'project' to 'developers', you would use:
sudo chgrp developers project
- View the groups a user is a part of: To see the groups a user belongs to, use the
groups
command. For example, to view the groups 'john' is a part of, you would use:
groups john
Remember that in order to execute these commands, you might need the appropriate permissions, which is why sudo
is used before each command. Be sure to replace 'developers', 'john', and 'project' with your actual group name, username, and directory name respectively.