In this tutorial, we will talk about groups and how to create them in Linux using
groupadd command. The objective of the group is to set privileges and allow access to such, read, write and execute permissions that are shared among users within those groups.
In Linux, we can grant certain permissions and responsibilities for each user, For example for some users, we can grant edit permission while for some we can grant execute permission for certain commands and files to be executed, and this process can be hectic and time-consuming. For that we have groups and we can assign permissions collectively.
There are two types of groups in Linux – Primary and Secondary.
Primary User Group
A primary group is configured when the first account is setup and assigned to each user in the
/etc/passwd file. Most of the time, it’s the same as your login name. Your main group would be a member of all of your processes and files, including directories and folders.
$ whoami ph0enix $ grep ph0enix /etc/passwd ph0enix:x:1000:1000:m4ster,,,:/home/ph0enix:/bin/bash $ echo "test" > file $ ls -la file -rw-rw-r-- 1 ph0enix ph0enix 5 May 25 16:16 file
Secondary User Group
Users who already have accounts can be added to secondary groups. The
/etc/group file shows secondary group memberships. On Linux, users can belong to more than one group. It can be used to share files and do other things. An admin can also fine-tune security. For example, if you are in a secondary group called cdrom, you can mount and unmount the CD-ROM drive.
How to Create a Group in Linux using Groupadd command?
To create a new group, type
groupadd and then the name of the new group.
sudo groupadd hacktoday
This command will add a new group to /etc/group and /etc/gshadow. Once the group is set up, you may add new members to it. If the group already exists, the system will display an error message:
groupadd: group 'hacktoday' already exists
Use -f (–force) to suppress the error notice and make the command exit successfully.
groupadd -f hacktoday
Adding a user to a group
Add a user to a group with usermod. How to add noor to hacktoday The “append” -a parameter and the secondary -G parameter add groups.
Type into the terminal:
- Add noor to Linux’s hacktoday group.
sudo useradd -G hacktoday noor
- Want to add user willam to the primary group hacktoday? Run:
sudo useradd -g hacktoday willam
- In Linux, we can add a user named www-data to an existing group called hacktoday:
sudo useradd -g hacktoday www-data
Change a Users Primary Group
All previous commands manage user secondary groups. A user’s primary group is usually their username.
To change a user’s primary group, type:
sudo usermod -g new_group user_name
How to Remove a user from Secondary Group?
Gpasswd manages groups. To remove a user:
sudo gpasswd -d noor hacktoday
Note: gpasswd can be used to define group administrators and specify a password for resource access in groups. The man gpasswd command in Linux provides more information.
Delete a Group
The command to delete a group is:
sudo groupdel hacktoday
How to List Groups in Linux
Linux has many groups by default. Sudo can be used to provide permissions.
To display a list of system groups in /etc/groups:
sudo nano /etc/groups
Displaying a user’s groups with the command groups:
Now you know how to create and remove Linux group members. Use
man command to display a manual in your terminal.