Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình Bash

Thêm người dùng vào nhóm Linux: Hướng dẫn từng bước kèm ví dụ

Trong hướng dẫn này, chúng ta sẽ xem xét việc thêm người dùng vào nhóm. Lệnh Bash shell là công cụ mạnh mẽ để đạt nhu cầu cụ thể. Nhóm là cách hiệu quả để chia sẻ và bảo vệ thông tin. Quyền file trong Linux cho phép bạn thiết lập quyền sở hữu theo quyền người dùng, quyền nhóm, và toàn cục. Cũng là cách hoàn hảo để duy trì quyền người dùng đến root.

Hãy đào sâu vào đó.

Thêm người dùng kèm nhóm (wheel)

Bạn vừa tuyển admin mới, và cần làm cho họ trở thành quản trị viên trên file server. Lệnh useradd là công cụ đơn giản và mạnh mẽ để thêm người dùng. Nó cho phép thêm người dùng và gán họ vào nhóm.

NAME
useradd - create a new user or update default new user information
SYNOPSIS
useradd [options] LOGIN

Đó là cơ bản từ man page useradd, nên hãy bắt đầu từ đầu bằng cách thêm người dùng mới, Admin Bob, và Admin Jane.

Các tùy chọn phổ biến cho useradd

-c, --comment COMMENT
-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]

Đó là hai trường chính của chúng ta. Bây giờ hãy thêm Bob.

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Bob

Và hãy thêm Jane.

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Jane -G wheel

Bây giờ, chúng ta biết Bob là admin và sẽ cần quyền quản trị. Nhưng đôi khi, bạn có thể không biết, hoặc đơn giản quên. Vậy chúng ta sẽ cần thêm nhóm cho anh ta sau. Jane đã được thêm vào wheel. Nhóm wheel là nhóm tích hợp sẵn trong hầu hết hệ thống Linux. Nó là nhóm mặc định cho phép leo đặc quyền.

[admin@wsxdn.com ec2-user]$ sudo su
We trust you have received the usual lecture from the local System Administrator.
It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for Admin.Jane: Sorry, try again.

Nói về bước quên. Trông như chúng ta quên thiết lập mật khẩu. Tôi không khuyên thiết lập mật khẩu với lệnh useradd. Thay vào đó, nhớ tiếp theo với lệnh passwd. Passwd nhắc với terminal an toàn không lưu trong lịch sử. Hoặc, bạn có thể dùng read -sp để lưu mật khẩu inline.

[admin@wsxdn.com ec2-user]# passwd Admin.Jane
Changing password for user Admin.Jane.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[admin@wsxdn.com ec2-user]# su Admin.Jane
[admin@wsxdn.com ec2-user]$ sudo su
We trust you have received the usual lecture from the local System Administrator.
It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for Admin.Jane:
[admin@wsxdn.com ec2-user]#

Và xong. Một người dùng được thêm với nhóm, wheel. Không cần lệnh nhóm. Nếu cần thêm Jane vào nhiều nhóm ban đầu:

[admin@wsxdn.com ec2-user]# useradd Admin.Jane -G wheel,ec2-users
[admin@wsxdn.com ec2-user]# passwd Admin.Jane
[admin@wsxdn.com ec2-user]# su Admin.Jane
[admin@wsxdn.com ec2-user]$ groups Admin.Jane
wheel ec2-users
[admin@wsxdn.com ec2-user]$

Và xong. Admin.Jane thuộc nhóm người dùng riêng, wheel cho quyền sudo, và nhóm ec2-users.

Nhưng Bob thì sao? Vì Bob được tạo không có quyền truy cập nhóm wheel, chúng ta sẽ cần dùng lệnh khác.

Thêm người dùng vào nhóm

Chúng ta thêm Bob như user Admin mà không có quyền Admin. Hãy sửa với groupmems.

Các tùy chọn phổ biến cho groupmems

SYNOPSIS
groupmems -a user_name | -d user_name | [-g group_name] | -l | -p
OPTIONS
The options which apply to the groupmems command are:
-a, --add user_name
  Add an user to the group membership list. If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created.
-d, --delete user_name
  Delete a user from the group membership list. If the /etc/gshadow file exist, the user will be removed from the list of members and administrators of the group. If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created.
-g, --group group_name
  The superuser can specify which group membership list to modify.
-h, --help
  Display help message and exit.
-l, --list
  List the group membership list.

Hãy bắt đầu bằng lấy danh sách thành viên trong nhóm, wheel.

[admin@wsxdn.com ec2-user]# getent group wheel
wheel:x:10:ec2-user,samberry,Admin.Steve,Admin.Jane

Chúng ta biết Bob không có đó. Vậy hãy thêm Bob.

[admin@wsxdn.com ec2-user]# groupmems -a Admin.Bob -g wheel
[admin@wsxdn.com ec2-user]# groupmems -g wheel -l
ec2-user samberry Admin.Steve Admin.Jane Admin.Bob

Xong. Admin.Bob hiện là thành viên của wheel, và chúng ta xác minh bằng cách kiểm tra với tùy chọn groupmems -l, –list.

Đó là cơ bản quản lý nhóm trên Linux. Đối với instance Linux tại nhà, bạn có thể thấy nhóm mặc định là adm thay vì wheel.

admin@wsxdn.com:/home/samuelberry# groupmems -g adm -l
syslog samuelberry

Nếu tò mò, kiểm tra file cấu hình /etc/sudoers để biết bạn có nhóm nào.

admin@wsxdn.com:/home/samuelberry# cat /etc/sudoers
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
[admin@wsxdn.com ec2-user]$ cat /etc/sudoers
## Allows people in group wheel to run all commands
%wheel ALL=(ALL)ALL
## Same thing without a password
# %wheel ALL=(ALL)NOPASSWD: ALL

Bạn có thể nhận ra, chúng ta thêm người dùng vào instance EC2. Tuần sau như một phần của quản lý user/group tôi sẽ hướng dẫn thêm người dùng vào instance Linux qua EC2.