Adding a user to a group in Linux is a common task for system administrators. Groups allow users to share permissions, access files and execute tasks collectively. This article will discuss adding a user to a group in Linux.
Linux has a user management tool called useradd
, which creates new users on the system. However, to add a user to an existing group, we will use the usermod
command.
Before we begin, it’s important to note that you need to have root or sudo privileges to add a user to a group. Without these privileges, you won’t be able to make any changes to the system.
To add a user to a group in Linux, follow these steps:
Step 1: Check existing groups
To check the groups available on your Linux system, run the following command:
cat /etc/group
This command will display a list of all groups on your system, along with their group ID (GID) and a list of users who belong to the group.
Step 2: Add the user to the group
To add a user to a group, use the usermod
command followed by the -aG
option and the group name you want to add the user to. For example, if you’re going to add a user named “john” to the “sudo” group, you would run the following command:
sudo usermod -aG sudo john
- The
-a
option tells usermod to append the new group to the user’s existing groups.
- The
-G
option specifies the name of the group you want to add the user to.
Step 3: Verify the user’s group membership
To verify that the user has been added to the group, run the id
command followed by the username of the user you just added. For example:
id john
This command will display information about the user, including their user ID (UID), group ID (GID), and a list of all the groups they belong to.
Step 4: Verify group permissions
Finally, it’s essential to verify that the user has access to the resources available to the group. For example, if the group you added the user to has permission to access a particular directory, please ensure that the user can access that directory.
In conclusion, adding a user to a group in Linux is a straightforward process. By following these steps, you can easily add a user to a group, allowing them to access shared resources and execute tasks collectively with other users in the same group.
📕 Related articles about Linux
- How to Create 7z File Archive in Linux
- How to Execute Remote SSH Command in Background
- How to Enable GSSAPI Authentication in SSH
- How to recover lost partition using Linux
- How to get hardware information in Linux
- How to take screenshots from Linux terminal