Users and groups are used on GNU/Linux for access and control to the system’s files, directories, and peripherals. So, Every Linux user must know that how to create users and groups as it is very important, without it you will be very limited in various situation.
In this article, you will learn that how to create Linux users, how to set their password and how to add users in groups.
- Create users
- Create Groups
- Add users to groups
You can use any of following command to create user.
For example in following image, I have created a user “john”
Set Password to User Account
Now type password
Setting password for user “john” in below image.
There are two types of user’s groups:
Primary Group: This is the group created when you create a user and it is created with same name as your username. But we can also change a user’s primary group. The primary group is used by default when create or modify files/folders. A user can be a part of only one primary group.
Secondary Groups: It is the optional group for a user. A single user can be assigned to multiple secondary groups.
So, below command is used to create group in Linux
In below example image, I have created two groups “it” and “hr”
How to Add users to Groups?
Then, In this step we will add users in groups.
Set User Primary group
If you want to set or change a user’s primary group, then use the following command.
usermod -g group-name user-name
–g It means primary group
So for now, In below image i have changed user “john” primary group to “it”
Set User Secondary group
But if you want to set or change a user’s secondary group then use the below command.
usermod -G group-name user-name
-G Capital G means secondary group
So, I have changed user “john” secondary group to “hr”
See a User ID and Assigned Groups
Finally, you can see in image that group “it” and “hr” are assigned to user “john”
Remove User From a Group
Following command is used to remove a user from a group.
gpasswd -d user-name group-name
So, In example image; I am removing user “john” from “hr” group
That’s it, Now you learned that how to create/modify users and groups in Linux.