Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Linux - Groups

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Linux supports multiple users. Imagine a scenario where you have to give different permissions to multiple users. Doing it one by one is time-consuming and also confusing. A simple and easy way to do that is to create a group with the required permissions and add users to that group.

Linux Groups

This blog will explain Linux groups and how we can create new groups and add users to that groups.

What exactly are groups?

In Linux, groups are nothing but collections of users. Different users can be a part of the same group. Instead of setting permissions for each user, groups let you set permissions at the group level.

The details for groups and group members are stored in a file located at /etc/group. You can view the content of this file, and the content will look like the image shown below.

Contents of group file

Content of /etc/group file

🔶Each line in the ‘group’ file tells us about the Group and the member of that group. 

The group's name appears in the first field. The (encrypted) password for the group is in the second field (it can be empty). Group identification, or GID, is the third field. The list of members in the fourth field.

format

There are multiple commands available using which we can manage groups, add or remove users from them, and many more. Let’s have a look at those commands. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Commands to manage Group

Multiple commands like usermodgroupadd, and groupmod can be used to manage groups of Linux. Let’s discuss each of these commands with examples.

groupadd

We can use this command to create a new Group in Linux. The syntax is as follows: groupadd <group name>.

Example:

groupadd coding_ninjas

 

🔸The above command will create a new group in Linux with the name coding_ninjas.

usermod

The useradd or usermod command can be used to change a group's membership. If the group is not provided in the command, the usermod command will by default remove the user from all groups that he is a part of. This behavior is avoided when the -a (append) flag is used.

Example:

usermod -a -G coding_ninjas ninja

 

🔸The above command will add user ‘ninja’ to the group ‘coding_ninjas’.

groupmod

An existing group's name can be changed with the help of the groupmod command. The syntax is as follows: groupmod -n <newGroup> <oldGroup>.

Example:

groupmod -n Coding Ninjas Studio coding_ninjas

 

🔸The above command will change the name of group coding_ninjas to Coding Ninjas Studio.

groupdel

This command can be used to delete the group from the system. The syntax is as follows: groupdel <group name>.

Example:

groupdel Coding Ninjas Studio

 

🔸The above command will permanently delete the group Coding Ninjas Studio from the system.

groups

Using the groups command, we may find out which groups the current user is a member of.

Example:

groups

 

Output:

output

The current user is only part of the ‘wheel’ group, which is why only wheel is printed as the output of the above command.

Frequently asked questions

What is the exit code in Linux?

In Linux, an exit code indicates how a command or script performed. It ranges from 0 to 255. We can tell whether a process ran successfully or not by looking at the exit codes.

Name the two types of Linux User Mode?

Command Line and GUI are the two types of Linux user modes.

What is the maximum length for a filename under Linux?

Under Linux, a filename can only be 255 bytes long.

Conclusion 

In this blog, we have discussed what groups are in Linux and how we can create, manage, or add users to groups using commands.

If you think this blog has helped you enhance your knowledge about the above question, and if you want to learn more, check out our articles. 

🔥 Linux - Decision Making
 

🔥 Linux File System
 

🔥 Linux- Shell Loops
 

🔥 Linux Security

And many more on our Coding Ninjas Studio.

Visit our website to read more such blogs. Make sure that you enroll in the courses we provide, take mock tests, solve problems available, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. 

Please upvote our blog to help other ninjas grow.

Happy Learning!

Topics covered
1.
Introduction
2.
What exactly are groups?
3.
Commands to manage Group
3.1.
groupadd
3.2.
usermod
3.3.
groupmod
3.4.
groupdel
3.5.
groups
4.
Frequently asked questions
4.1.
What is the exit code in Linux?
4.2.
Name the two types of Linux User Mode?
4.3.
What is the maximum length for a filename under Linux?
5.
Conclusion