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

Chạy nhiều version PHP trên một Ubuntu 18.04 – server

Như chúng ta đã biết thì PHP thường được sử dụng để làm ngôn ngữ trong việc tạo ra một trang...
30/12/2020

[Apache] Cấu hình Basic Authentication

Basic Authentication là kiểu xác thực cơ bản nhất của một Web Server Apache Mô hình Các bước...
30/12/2020

Installing Anaconda Python on CentOS 8

Anaconda is a Python distribution designed for data science, machine learning, deep learning etc.  In this article, I...
29/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024