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

How to Use EPEL on CentOS 7

The full form of EPEL is Extra Packages for Enterprise Linux. It is software package repository for Red Hat Enterprise...
29/12/2020

CentOS Change Password

Passwords are the oldest and yet, effective form of security measure that almost every single system in the world offers...
29/12/2020

Nextcloud [Part 3] – Quản lý User trên NextCloud

Mục lục Login vào NextCloud Tạo mới User Tạo mới Group Thêm User vào Group 1. Login vào NextCloud Thực...
30/12/2020
Bài Viết

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

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024