How to Add a User to a Group on CentOS 7

29/12/2020
Linux is a multi-user operating system. It is designed from the start to work with many users simultaneously. Which user can do what operation can be configured very well on Linux. So even though many people shared the same computer, the users and their data was secured.

On Linux systems, a user can’t access another user’s file. If in any case, shared access between many users is required, a group can be created and the users must be a member of the same group. That’s not enough. The files and directories to be shared must have required read, write and execution permission set up for the group. So you can see, Linux user and group is an essential part of Linux security.

In this article, I will show you how to add a user to a group on CentOS 7. Let’s get started.

Creating a Group on CentOS 7:

In this section, I am going to show you how to create a new group on CentOS 7.

Let’s say, you want to create a group work on your CentOS 7 operating system, you would run the following command:

$ sudo groupadd work

The group work should be added.

All the group data on your CentOS 7 machine is stored in the /etc/group file. You can read the entire file with the following command:

$ cat /etc/group

As you can see, the group file has several fields separated by colon (:).

The fields are as follows:

Group Name : Group Password : Group ID or GID : Users who are members of the group

The users list is separated by commas. The group password is by default set to x, which means no password is set by default.

In this screenshot, the group name is wheel, it has no password (x) set, it’s group ID or GID is 10, and its only user shovon is the member of the group wheel.

You can verify whether work group is added with the following command:

$ grep work /etc/group

As you can see, the work group is added, and its group ID or GID is 1001. Unless specified, ordinary groups GID starts from 1000.

Creating a User on CentOS 7:

Now let’s create some new users so that we can add them in our newly created work group in the next section.

Add user lizzy with the following command:

$ sudo useradd -m lizzy

Now set password for lizzy with the following command:

$ sudo passwd lizzy

Enter password for lizzy and press <Enter>.

Re-enter password for lizzy and press <Enter>. The password for the user should be set.

I am going to add another user ash.

$ sudo useradd -m ash

Also set password for ash.

$ sudo passwd ash

Add Users to a Group on CentOS 7:

Now that we have a group work created and our dummy users are ready, let’s see how to add them to the work group.

Now to add the user lizzy to the work group with the following command:

$ sudo usermod -aG work lizzy

Now to add the user ash to the work group with the following command:

$ sudo usermod -aG work ash

Now let’s see whether the users lizzy and ash is added to the work group with the following command:

$ grep work /etc/group

As you can see, lizzy and ash are now members of the group work.

You can also login as the user who is a member of the group work, and run the following command to see which group the user is a member of:

$ id

As you can see, the user lizzy is a member of the group work.

So that’s how you add a user to a group on CentOS 7.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Performance Testing [Phần 2] Hướng dẫn cài đặt Gatling trên Ubuntu 18.04

Gatling là công cụ kiểm tra tải thông minh, cao cấp, dể dàng sử dụng. Gatling hỗ trợ rất tốt giao...
30/12/2020

CentOS Versus Ubuntu

Your server needs a stable operating system that you can depend on, and both CentOS and Ubuntu are popular choices that...
29/12/2020

[DNS] Tìm hiểu về lệnh DIG

Với lệnh dig, bạn có thể truy vấn thông tin về các bản ghi DNS, bao gồm: host addresses, mail exchanges,...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Lý do tại sao bạn nên sử dụng proxy khi truy cập web đen
27/02/2023

Các lỗi thường gặp khi sử dụng proxy và cách khắc phục chúng.
27/02/2023

Tác động của việc sử dụng proxy đến tốc độ kết nối internet của bạn.
27/02/2023

Các tiện ích và công cụ để quản lý proxy.
27/02/2023

Các cách để kiểm tra tốc độ và độ ổn định của proxy.
27/02/2023