hoatranlab.io.vn Zalo: 0917516878 Hotline: 0917516878 [email protected]
HoaTranLab Logo HoaTranLab
Linux Bible 11th Edition • Chapter 11

Managing User Accounts

Quản lý tài khoản người dùng trên Linux: tạo, sửa, xóa user/group, phân quyền ACL, sticky bit và cộng tác tệp tin nhóm.

Mục tiêu học tập

Tạo User với useradd

Nắm vững cú pháp lệnh useradd với các tùy chọn -c, -d, -g, -G, -s, -u để tạo tài khoản người dùng đầy đủ thông tin.

Sửa User với usermod

Sử dụng usermod để thay đổi thông tin tài khoản, thêm group, khóa/mở tài khoản với các tùy chọn -L, -U, -aG.

Xóa User với userdel

Xóa tài khoản người dùng an toàn với userdel và tùy chọn -r, tìm file còn sót bằng find trước khi xóa.

Quản lý Group

Hiểu cấu trúc /etc/group, tạo group với groupadd, sửa với groupmod và phân quyền chia sẻ file theo nhóm.

Access Control Lists (ACL)

Thiết lập quyền chi tiết bằng setfacl/getfacl, hiểu cơ chế mask và ACL mặc định (default ACL) kế thừa.

Sticky Bit & Set GID

Tạo thư mục cộng tác với Set GID bit và thư mục hạn chế xóa với Sticky bit để bảo vệ file của người dùng.

Lý thuyết chi tiết

Cấu trúc /etc/passwd và /etc/shadow

File /etc/passwd lưu thông tin cơ bản của tất cả user. Mỗi dòng gồm 7 trường phân cách bởi dấu :. Mật khẩu được lưu mã hóa trong /etc/shadow.

Terminal — /etc/passwd format

$ cat /etc/passwd | head -3

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

sara:x:1002:1007:Sara Green:/home/sara:/bin/tcsh

# Format: username:password:UID:GID:comment:home:shell

$ sudo cat /etc/shadow | grep sara

sara:$6$abc123$...:19000:0:99999:7:::

# Format: username:hashed_pw:lastchange:min:max:warn:inactive:expire

Lệnh useradd — Tạo tài khoản người dùng

Lệnh useradd tạo tài khoản mới từ command line. Chỉ cần username là bắt buộc, các tùy chọn khác để cấu hình chi tiết. Mặc định đọc từ /etc/login.defs/etc/default/useradd.

Terminal — useradd examples

# Tạo user cơ bản

$ sudo useradd -c "Sara Green" sara

$ sudo passwd sara

Changing password for user sara.

New password: **********

Retype new password: **********

passwd: all authentication tokens updated successfully.

# Tạo user với đầy đủ tùy chọn

$ sudo useradd -g users -G wheel,apache -s /bin/tcsh -c "Sara Green" sara

# Xem giá trị mặc định

$ useradd -D

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/bash

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes

# useradd options:

# -c "comment" Mô tả người dùng

# -d home_dir Thư mục home

# -e expire_date Ngày hết hạn (YYYY-MM-DD)

# -g group Primary group

# -G grouplist Supplementary groups

# -m Tạo home directory

# -s shell Shell mặc định

# -u user_id UID cụ thể

usermod và userdel — Sửa và Xóa tài khoản

Terminal — usermod & userdel

# Đổi shell

$ sudo usermod -s /bin/bash chris

# Thêm supplementary groups (dùng -aG để không xóa group cũ)

$ sudo usermod -aG sales,marketing chris

# Khóa tài khoản

$ sudo usermod -L sara

# Mở khóa tài khoản

$ sudo usermod -U sara

# Tìm file thuộc user trước khi xóa

$ sudo find / -user chris -ls

# Xóa user và home directory

$ sudo userdel -r chris

# Tìm file không thuộc user nào

$ sudo find / -nouser -ls

Quản lý Group Accounts

Mỗi user có một primary group. Group cho phép nhiều user chia sẻ file. GID 0-999 dành cho admin groups, từ 1000 trở lên cho regular groups.

Terminal — group management

$ cat /etc/group | grep sara

sara:x:1007:

sales:x:1302:joe,bill,sally,sara

# Format: groupname:password:GID:members

# Tạo group mới

$ sudo groupadd kings

$ sudo groupadd -g 1325 jokers

# Sửa GID hoặc tên group

$ sudo groupmod -g 330 jokers

$ sudo groupmod -n jacks jokers

# Thay đổi primary group tạm thời

$ newgrp sales

$ touch newfile.txt

$ ls -l newfile.txt

-rw-rw-r--. 1 sara sales 0 Jan 18 22:23 newfile.txt

Access Control Lists (ACL)

ACL cho phép user thường phân quyền chi tiết cho file/thư mục mà không cần root. Dùng setfacl để đặt và getfacl để xem ACL. Dấu + trong output của ls -l báo hiệu file có ACL.

Terminal — setfacl / getfacl

[mary]$ touch /tmp/memo.txt

# Đặt quyền rw cho user bill

[mary]$ setfacl -m u:bill:rw /tmp/memo.txt

# Đặt quyền rw cho group sales

[mary]$ setfacl -m g:sales:rw /tmp/memo.txt

# Xem ACL - dấu + cho biết có ACL

[mary]$ ls -l /tmp/memo.txt

-rw-rw-r--+ 1 mary mary 0 Jan 21 09:27 /tmp/memo.txt

[mary]$ getfacl /tmp/memo.txt

# file: tmp/memo.txt

# owner: mary

# group: mary

user::rw-

user:bill:rw-

group::rw-

group:sales:rw-

mask::rw-

other::r--

# Default ACL cho thư mục (kế thừa)

[mary]$ mkdir /tmp/mary

[mary]$ setfacl -m d:g:market:rwx /tmp/mary/

# Xóa ACL

[mary]$ setfacl -x u:bill /tmp/memo.txt

# Bật ACL trong /etc/fstab

/dev/sdc1 /var/stuff ext4 acl 1 2

Sticky Bit và Set GID Bit

Set GID bit (chmod 2xxx) trên thư mục: file tạo trong đó sẽ thuộc group của thư mục. Sticky bit (chmod 1xxx) trên thư mục: chỉ owner của file mới xóa được file đó (ví dụ: /tmp).

Terminal — Set GID & Sticky bit

# Tạo thư mục cộng tác cho group sales

$ sudo groupadd -g 301 sales

$ sudo usermod -aG sales mary

$ sudo mkdir /mnt/salestools

$ sudo chgrp sales /mnt/salestools

$ sudo chmod 2775 /mnt/salestools

$ ls -ld /mnt/salestools

drwxrwsr-x. 2 root sales 4096 Jan 22 14:32 /mnt/salestools/

# 's' thay cho 'x' của group = Set GID bit đang bật

# Tạo sticky bit directory

$ sudo chmod 1777 /tmp/shared

$ ls -ld /tmp/shared

drwxrwxrwt. 2 root root 4096 Jan 22 /tmp/shared/

# 't' thay cho 'x' của other = Sticky bit đang bật

# Special bits summary

# 4xxx = SUID (Set User ID) — u+s

# 2xxx = SGID (Set Group ID) — g+s

# 1xxx = Sticky bit — o+t

Lab thực hành

Bài lab toàn diện: Tạo user, group, phân quyền ACL và cấu hình thư mục cộng tác.

1

Tạo Group và User mới

Tạo group devteam và hai user alice, bob thuộc group đó.

Terminal

$ sudo groupadd -g 1500 devteam

$ sudo useradd -m -g devteam -G wheel -s /bin/bash -c "Alice Dev" alice

$ sudo useradd -m -g devteam -s /bin/bash -c "Bob Dev" bob

$ sudo passwd alice

New password: ****

$ sudo passwd bob

2

Xác minh tài khoản đã tạo

Kiểm tra trong /etc/passwd và /etc/group.

Terminal

$ grep alice /etc/passwd

alice:x:1001:1500:Alice Dev:/home/alice:/bin/bash

$ grep devteam /etc/group

devteam:x:1500:

$ id alice

uid=1001(alice) gid=1500(devteam) groups=1500(devteam),10(wheel)

3

Tạo thư mục cộng tác Set GID

Tạo /opt/devshare với Set GID bit để file tạo trong đó tự thuộc devteam.

Terminal

$ sudo mkdir /opt/devshare

$ sudo chgrp devteam /opt/devshare

$ sudo chmod 2775 /opt/devshare

$ ls -ld /opt/devshare

drwxrwsr-x. 2 root devteam 4096 Jan 22 /opt/devshare/

4

Test Set GID hoạt động

Đăng nhập là alice và tạo file, kiểm tra group của file.

Terminal

$ su - alice

[alice]$ touch /opt/devshare/alice_file.txt

[alice]$ ls -l /opt/devshare/

-rw-rw-r--. 1 alice devteam 0 Jan 22 /opt/devshare/alice_file.txt

# Group là devteam (không phải alice) — Set GID hoạt động!

5

Thiết lập ACL cho file

Dùng setfacl để cho user charlie đọc file của alice mà không cần thêm vào group.

Terminal

[alice]$ setfacl -m u:charlie:r-- /opt/devshare/alice_file.txt

[alice]$ getfacl /opt/devshare/alice_file.txt

# owner: alice

user::rw-

user:charlie:r--

group::rw-

mask::rw-

other::r--

6

Sửa và xóa user

Thay đổi shell cho bob và xóa account alice (với home directory).

Terminal

$ sudo usermod -s /bin/zsh bob

$ grep bob /etc/passwd

bob:x:1002:1500:Bob Dev:/home/bob:/bin/zsh

# Tìm file thuộc alice trước khi xóa

$ sudo find / -user alice -ls 2>/dev/null

# Xóa user và home dir

$ sudo userdel -r alice

$ grep alice /etc/passwd

(no output — alice removed)

Câu hỏi ôn tập

1

Lệnh nào tạo user john với comment "John Smith", home /home/john, shell /bin/bash và thuộc group wheel?

sudo useradd -c "John Smith" -d /home/john -s /bin/bash -G wheel -m john
2

Sự khác biệt giữa usermod -G sales johnusermod -aG sales john là gì?

-G sales thay thế tất cả supplementary groups của john bằng chỉ "sales" (xóa các group cũ). -aG sales thêm "sales" vào danh sách group hiện tại mà không xóa các group đã có. Luôn dùng -aG khi muốn thêm group.

3

7 trường trong file /etc/passwd là gì và ý nghĩa của từng trường?

username:password:UID:GID:comment:home_dir:shell
Ví dụ: sara:x:1002:1007:Sara Green:/home/sara:/bin/bash
- username: tên đăng nhập
- x: mật khẩu trong /etc/shadow
- UID: user ID
- GID: primary group ID
- comment: mô tả
- home_dir: thư mục home
- shell: shell mặc định

4

Dấu + trong output ls -l có nghĩa là gì? Cách kiểm tra chi tiết?

Dấu + ở cuối permissions (-rw-rw-r--+) cho biết file có ACL (Access Control List) được đặt. Để xem chi tiết, dùng lệnh: getfacl /path/to/file

5

ACL mask là gì và nó ảnh hưởng như thế nào đến quyền thực tế?

Mask là quyền tối đa mà ACL user/group có thể có. Khi bạn đặt group permission bằng chmod, mask cũng thay đổi theo. Nếu mask là r-- thì dù bill có ACL rw-, quyền hiệu quả vẫn chỉ là r-- (hiển thị: user:bill:rw- #effective:r--).

6

Set GID bit trên thư mục hoạt động như thế nào? Lệnh để tạo thư mục như vậy?

Khi Set GID bit được bật trên thư mục, mọi file/thư mục tạo trong đó sẽ kế thừa group của thư mục cha thay vì primary group của user tạo ra. Nhận biết qua chữ 's' thay cho 'x' của group (drwxrwsr-x).
Lệnh: chmod 2775 /mnt/shared hoặc chmod g+s /mnt/shared

7

Sticky bit trên thư mục có tác dụng gì? Ví dụ thực tế?

Sticky bit (chữ 't' thay cho 'x' của other: drwxrwxrwt) ngăn user xóa file của người khác ngay cả khi có write permission trên thư mục. Ví dụ thực tế: thư mục /tmp — mọi user đều có quyền ghi nhưng chỉ được xóa file của chính mình hoặc nếu là root.
Lệnh: chmod 1777 /tmp/shared hoặc chmod o+t /tmp/shared

8

Làm thế nào để tìm tất cả file thuộc về user không còn tồn tại trên hệ thống?

Dùng lệnh: sudo find / -nouser -ls
Lệnh này tìm tất cả file không được gán cho bất kỳ username nào (chỉ có UID số). Đây là vấn đề bảo mật vì những file này không được quản lý. Sau khi xóa user, nên dùng find / -uid OLD_UID -ls để tìm file còn sót.