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

CentOS Install Htop

It doesn’t matter whatever system you’re using – Windows, Linux or macOS or whatever, having a handy task manager...
29/12/2020

[NGINX] Hướng dẫn cài đặt Nginx trên CentOS 8

NGINX (đọc là “engine x”) là một phần mềm mã nguồn mở cho web serving, reverse proxying, caching,...
30/12/2020

Hướng dẫn cách sử dụng câu lệnh History trong Linux

Trong bài viết này, Onet sẽ giúp các bạn tìm hiểu làm thế nào bạn có thể sử dụng lệnh history...
30/12/2020
Bài Viết

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

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024