Skip to content

Instantly share code, notes, and snippets.

@smarteist
Last active October 14, 2023 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarteist/53b5c3792a1a67f3ed45d14094fb55ca to your computer and use it in GitHub Desktop.
Save smarteist/53b5c3792a1a67f3ed45d14094fb55ca to your computer and use it in GitHub Desktop.

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:

  1. 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.
  2. Security: Groups can ensure that only authorized users have access to specific resources.
  3. 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.

  1. 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
  1. 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).

  1. 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
  1. 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
  1. 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
  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment