Are you trying to add a user to Group in Linux?
This guide is for you.
Users and groups are used on GNU/Linux for access control—that is, to control access to the system's files, directories, and peripherals. Linux offers relatively simple/coarse access control mechanisms by default.
In Linux operating systems, groups allow certain user permissions to be available for group members.
For someone to access the system, one must add a user to a group in Linux.
Adding users to group can help you manage your system better as well as provide security by being able to control which users can read, alter, and execute commands.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Linux related tasks.
In this context, we shall look into how to add users to group in Ubuntu.
More about Linux Users and Groups ?
The Linux operating system allows for multiple users to use the system at one time. Users, logging in with their username, determine who initiates a process.
Files and directories are managed by a user and a group. Only the root user with sudo access can determine the extent of the permissions.
The root user is the only user who can add users to a group.
Groups in Linux are a collection of users.
There are two categories: primary and secondary.
i. A primary group is created when a user is added. This information is stored in /etc/groups and an additional entry can often be found in /etc/passwd.
ii. You must become part of a secondary group in order to inherit access to a file made by another user.
Every user can belong to exactly one primary group, and to as many secondary groups as necessary.
How to log in as Root User in Linux ?
"Add user to a group" is an administrative function only available for root users. In other words, you have to become one to perform this function.
Use the special command, su which stands for "super-user" or "switch user" to login. Enter the password if you have it once prompted:
You can also use sudo command, which stands for "super-user do":
How to add an Existing User to a Linux Group ?
Now that you’ve successfully logged in as a root user account, you can now add an existing user to the group using the usermod command:
sudo usermod -a -G group_to_add username
-a flag – prompts the usermod command to add a user to a group. If you fail to use -a, you cannot add a user to the groups listed after -G successfully.
-G flag – indicates which secondary group you want the new user to belong.
-g flag – used to change a user's primary group.
Let's say you want to add existing user "ibmimedia" to the group “mail”. You can do so with the following usermod command:
sudo usermod -a -G mail ibmimedia
There will be no output once you execute the command. You will however notice a change in privileges once you access the files associated to the Linux groups.
In any case, if the group name does not exist, it will display a prompt.
If you wish to add a user on multiple groups, "mail and sysadmin", separate the group names with a comma (,) as follows:
sudo usermod -a -G sudo,sysadmin ibmimedia
How create a New User in Linux, & Add User to Group in One Command ?
You might encounter cases where you want to create a new user and right then and there, make them part of a group.
You can easily do this with the useradd command:
sudo useradd -g primary_group -G secondary_group username
For example, you want to make a new user, "ibmimedia", and add him to the primary group “developers”, and secondary group “mail”. This is the command you would type in:
sudo useradd -g developers -G mail ibmimedia
To add a new user on multiple Linux groups, separate the user group names with a comma as follows:
sudo useradd -g developers -G mail,demo,news ibmimedia
How to Check a User's Groups in Linux?
To check whether or not you were able to successfully add a user to a group in Linux, you can do so with the user id command:
Let's say for example we want to check the username "ibmimedia" in groups:
It should display an output of:
uid=501(ibmimedia) gid=20(developers) groups=20(developers),30(mail),67(news),...
This information states that ibmimedia's primary group is developers. It should display the user's long list of secondary groups, along with the primary after "groups".
If you just want the user's supplementary or secondary groups to be displayed, use the groups command:
developers mail news adm demo
If you don't specify a username, the command will execute for the current logged in user's groups data.
How to Remove a User From a Group ?
If you wish to remove a user from a group, simply use the gpasswd command along with the -d option, which means you want to delete them. The command should look like this:
sudo gpasswd -d username groupname
For example, if you want to delete “ibmimedia” from the group “developers”:
sudo gpasswd -d ibmimedia developers
Again, there will be no output displayed.
How to Create a New Group in Linux ?
If you want to make a new group, use the groupadd command and your assigned groupname:
sudo groupadd groupname
For example, to make a new group called “content”, you may do so with the following command:
sudo groupadd content
How to Delete a Group in Linux ?
To delete an existing group, use the groupdel command followed by the group name you wish to delete:
sudo groupdel groupname
For example, if you want to delete the group, “developers”, you may do so with the following command:
$sudo groupdel developers
No outputs will be displayed for creating new groups and deleting groups once the command is executed.
How to Change a User's Primary Group ?
To change a user primary group, enter the following on the command line:
sudo usermod -g groupname username
For this example, we changed the primary group of the user ibmimedia to developers:
sudo usermod -g developers ibmimedia