15 C
Texas
Muhammad Nabeel
Network and System Administrator

How to Add Users and Groups in Linux

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.

  1. Create users
  2. Create Groups
  3. Add users to groups

Create users

You can use any of following command to create user.

adduser [user-name]
- Advertisement -

OR

useradd [user-name]

For example in following image, I have created a user “john”

Set Password to User Account

passwd [user-name]

Now type password

Setting password for user “john” in below image.

Create groups

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

groupadd [group-name]

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

id user-name

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.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article