User groups play a significant role in managing the Linux users and the entire system. If you are new to Linux and Unix, this concept might seem a bit complicated. But this article will provide an easy way to understand each of the topics and concepts in detail. User groups help in selecting the group of users so that they can share files easily.
This concept of Linux also helps in managing user privileges since assign privileges to groups is important rather than individual users. Let us now first understand, what Linux users are and what are group?
Linux users and files accessing privileges:
All Linux & Unix platforms allow multitasking like most of the operating systems. However, there is a slight distinction between Linux and other operating systems. It comes under the concept of multiple users. As we all know, Linux allows accessing and using its systems resources to various users concurrently.
These are Linux users. Accessing multiple resources by different users brings some security concerns to Linux users. So, to keep the files secure and put security in its position, it is essential to understand groups and protection privileges.
What are groups in Linux?
Groups are nothing but a cluster of different Linux users. The system administrator will define the group to set the privileges like reading, writing, or executing permission on an assigned file that users can share among themselves elected within the group.
Almost every Linux and Unix comes with a GUI tool or graphical application that allows the system administrator to manage these groups. Experienced system administrators prefer command-line tools over graphical tools.
Types of groups in the Linux & Unix
There are two different varieties of groups available in Linux.
- The Primary Group: When any user creates a file, the group assigned to it by default is the primary group. When a primary group is formed, the group name becomes the same as the username. It contains all the relevant file data in the /etc/passwd file.
- The Secondary Group: This is a particular group for users who wishes to share explicitly defined files that are granted explicit permissions by the admins. The system admin can provide specific permissions and rights to any files for the users residing within such groups.
Read, Write, and Execute Permissions in Linux:
Every file in the Linux systems comes with specific permission that is access and usage rights given to a particular file or directory. There are three different types of file and directory permissions. These are
- Read permission: When a file contains read permission, it permits its users to open files for readable purposes only. This permission makes the users capable to view the data only without altering anything. When the directory has read permission, the user gets the power to see and list the directory content.
- Write permission: When a file gets the write permission, it permits its user to alter or reconstruct any content from within the file. Once the admin provides this same permission for a directory, it will authorize the user to update or change the directory path and its content.
- Execute permission: This is powerful permission that allows users to run any script or program. This right allows the user to run or operate any script or program within the OS. When this permission is given to the directory, users can shift to different directories and can make any directory his/her current directory.
View file Permission:
Now, before setting up Linux Users and Groups in your system, it is essential to know how to view file permissions. For a system admin, it is necessary to check the file or directory's permission mode in Linux. To view or check the directory or file's permission, the admin should use the command:
ls -l <file> ls -l <directory>
Adding User in Linux:
Here are the steps you can follow to setup or add any Linux user to a group.
- Log in to your account as root user.
- Use the useradd command followed by the "name of the user". For example, useradd karlos
- Use "su" (super user) along with the name of the user you added just now to log on.
- Finally, use the "Exit" command. It will log you out from your current account.
Deleting Linux users from a group:
To delete a Linux user from any group, perform the following steps.
- Use the command userdel followed by the “name of the user”. For example, userdel karlos).
- Next, we have to use the command -d for setting the home directory for the user (by default it is the /home/”user name”). The -d will set the home directory for the user but will not create any new user.
- Then, we have to use the -m command for creating the home directory.
- For adding the user only, the default directory is the /home/“user name” directory. The -m will help in creating one.
- Finally, we can apply the command -r in conjunction with the userdel command for deleting the user & the home directory.
Creating a group in Linux:
It is very simple to create a new group in Linux. We have to apply the 'groupadd' command with the name of the group.
$sudo groupadd group_name
Adding an Existing User in the Group:
There might be a circumstance where you need to add a user to a secondary group. Usually, the admin adds existing users to make the group work on a project that requires team collaboration. To add an existing user in a group, you have to implement the command 'usermod -a -G', and specify the group and username in which you want to add that user.
$ sudo usermod -a -G sec_group_name user-name
$ sudo usermod -a -G pentestProject karlosray
Adding an Existing User to different Groups at a time:
Linux admin can face a circumstance where they might have to include a specific user in some secondary groups. It is possible using a single command. To implement this, the 'usermod' command along with the -G option/attribute is used. Along with this command, the admin has to serve the group names separated by commas.
$ sudo usermod -a -G group1, group2, group3 user_name
$ sudo usermod -a -G pentest, sudo, jenkins karlosRay
Remove a User from a Group:
When a project ends, Linux users are removed from groups to keep the data safe. To remove any user from the secondary group, the Linux user uses the 'gpasswd' command along with the -d option.
$ sudo gpasswd -d username group
$ sudo gpasswd -d karlos Jenkins
Linux users and groups are two different concepts that intersect at some point. Linux is secure because of the access privileges. Setting the rights and access privileges requires proper knowledge of creating and removing users & groups. Almost all Linux distributions support the above commands mentioned in the article.