Quyền truy cập File (File Permissions)
Cấu trúc Permission
Mỗi file và thư mục trong Linux đều có ba tập quyền: cho owner, cho group, và cho others.
- rwx r-x r-x
│ │ │ │
│ │ │ └── Others: đọc + thực thi
│ │ └─────── Group: đọc + thực thi
│ └──────────── Owner: đọc + ghi + thực thi
└──────────────── Loại: - (file) / d (dir) / l (link)
Bảng quyền — Octal Notation
| Ký tự | Octal | Ý nghĩa |
|---|---|---|
r | 4 | Read — đọc file / liệt kê thư mục |
w | 2 | Write — ghi file / tạo xoá trong thư mục |
x | 1 | Execute — thực thi file / đi vào thư mục |
rw- | 6 | Read + Write |
rwx | 7 | Read + Write + Execute |
r-x | 5 | Read + Execute |
r-- | 4 | Read only |
--- | 0 | Không có quyền |
Xem Permission
ls -la file.txt
# -rw-r--r-- 1 sontn devops 1234 Jan 15 file.txt
# Xem chi tiết hơn
stat file.txt
# File: file.txt
# Size: 1234 Blocks: 8 IO Block: 4096 regular file
# Access: (0644/-rw-r--r--) Uid: (1001/ sontn) Gid: (1001/ devops)
stat -c "%n %a %U %G" file.txt # format: tên octal owner group
chmod — Thay đổi quyền
# === Cách 1: Octal (số) ===
chmod 755 script.sh # rwxr-xr-x
chmod 644 config.txt # rw-r--r--
chmod 600 ~/.ssh/id_rsa # rw------- (SSH private key)
chmod 700 ~/.ssh/ # rwx------ (SSH directory)
chmod 777 /tmp/shared # rwxrwxrwx ⚠️ nguy hiểm
# === Cách 2: Symbolic ===
chmod u+x script.sh # thêm execute cho owner
chmod g+w file.txt # thêm write cho group
chmod o-r secret.txt # bỏ read của others
chmod a+x script.sh # thêm execute cho ALL (all = ugo)
chmod u+x,g-w,o-r file.txt # kết hợp nhiều thay đổi
# === Đệ quy ===
chmod -R 755 /var/www/html/
chmod -R 750 /opt/app/
| Octal | Symbolic | Dùng cho |
|---|---|---|
600 | rw------- | SSH private key, file password |
640 | rw-r----- | Config nhạy cảm, chỉ group đọc |
644 | rw-r--r-- | File config, web static content |
700 | rwx------ | Thư mục private, ~/.ssh/ |
750 | rwxr-x--- | Thư mục nhóm |
755 | rwxr-xr-x | Script, thư mục public |
777 | rwxrwxrwx | Tránh dùng — mọi người full quyền |
chown — Thay đổi chủ sở hữu
chown sontn file.txt # đổi owner
chown sontn:devops file.txt # đổi cả owner và group
chown :devops file.txt # chỉ đổi group
chown -R nginx:nginx /var/www/ # đổi đệ quy (web server)
chown -R ubuntu:ubuntu /home/ubuntu/
# Kiểm tra sau khi thay đổi
ls -la file.txt
stat file.txt
# chgrp — chỉ đổi group
chgrp docker /usr/bin/docker
chgrp -R devops /opt/deployments/
umask — Quyền mặc định khi tạo file
umask định nghĩa quyền bị trừ khi tạo file/thư mục mới.
umask # xem umask hiện tại (thường là 022)
umask 022 # file mới: 644, thư mục mới: 755
umask 027 # file mới: 640, thư mục mới: 750 (an toàn hơn)
File default = 666 - 022 = 644 (rw-r--r--)
Dir default = 777 - 022 = 755 (rwxr-xr-x)
Special Permissions
SUID — Set User ID (bit 4000)
Khi user chạy file này, process chạy với quyền owner của file thay vì quyền user đang chạy.
ls -la /usr/bin/passwd
# -rwsr-xr-x root root ... /usr/bin/passwd
# ↑ 's' thay cho 'x' của owner = SUID set
chmod u+s /usr/bin/myprogram # set SUID (symbolic)
chmod 4755 myprogram # set SUID (octal: tiền tố 4)
Ứng dụng thực tế: /usr/bin/passwd cần ghi vào /etc/shadow (chỉ root mới ghi được), nên SUID cho phép user thường đổi password của chính mình.
SGID — Set Group ID (bit 2000)
- Trên file: process chạy với quyền group của file
- Trên thư mục: file tạo mới trong thư mục kế thừa group của thư mục (dùng cho thư mục cộng tác nhóm)
chmod g+s /shared/project/ # set SGID trên thư mục
chmod 2775 /shared/project/ # octal: tiền tố 2
Sticky Bit (bit 1000)
User chỉ xoá được file của chính mình trong thư mục, dù có write permission.
ls -la /tmp
# drwxrwxrwt ... /tmp
# ↑ 't' = sticky bit
chmod +t /shared/uploads/
chmod 1777 /tmp # octal: tiền tố 1
Tổng kết Special Permissions
ACL — Access Control List
Permissions truyền thống (rwx) chỉ có 3 mức: owner, group, others. ACL cho phép set quyền cho từng user/group cụ thể trên một file.
Cài đặt
# Ubuntu/Debian
sudo apt install acl
# RHEL/Rocky
sudo dnf install acl
# Mount partition với ACL support (kiểm tra)
mount | grep acl
# ext4 filesystem: ACL thường được enable mặc định
getfacl — Xem ACL hiện tại
getfacl /var/www/html/app/
# # file: var/www/html/app/
# # owner: www-data
# # group: www-data
# user::rwx ← owner
# user:deployer:rwx ← ACL entry cụ thể cho user deployer
# group::r-x ← group owner
# group:devops:rwx ← ACL entry cụ thể cho group devops
# mask::rwx ← effective permission ceiling
# other::r-x ← others
getfacl project.conf # ACL của file
getfacl -R /opt/app/ # đệ quy xem ACL của cả thư mục
setfacl — Set ACL
# Cú pháp: setfacl -m u:<user>:<perms> <file>
# setfacl -m g:<group>:<perms> <file>
# Cho user alice đọc-ghi file.txt
setfacl -m u:alice:rw file.txt
# Cho group devops full quyền với thư mục
setfacl -m g:devops:rwx /opt/app/
# Đệ quy set ACL cho cả cây thư mục
setfacl -R -m g:devops:rwx /opt/app/
# Set default ACL — file tạo trong thư mục kế thừa ACL này
setfacl -d -m g:devops:rwx /opt/app/
setfacl -d -m u:deployer:rwx /opt/app/
# Xoá ACL của một user
setfacl -x u:alice /opt/app/config.yml
# Xoá toàn bộ ACL
setfacl -b /opt/app/config.yml
Use Case thực tế — Shared Project Directory
# Bài toán: /opt/project/ cần
# - developer team: rwx
# - qa team: r-x
# - deployer user: rwx
# - www-data (webserver): r-x
# Owner là root, group là devs
chown -R root:developers /opt/project/
chmod 750 /opt/project/
# Set ACL
setfacl -R -m g:qa:r-x /opt/project/ # QA đọc + chạy
setfacl -R -m u:deployer:rwx /opt/project/ # deployer full
setfacl -R -m u:www-data:r-x /opt/project/ # webserver đọc
# Default ACL — file mới tạo ra cũng có các quyền này
setfacl -R -d -m g:developers:rwx /opt/project/
setfacl -R -d -m g:qa:r-x /opt/project/
setfacl -R -d -m u:deployer:rwx /opt/project/
# Kiểm tra
getfacl /opt/project/secret.conf
# ký hiệu '+' sau permissions = có ACL đầy đủ
ls -la /opt/project/secret.conf
# -rw-rwxr--+ 1 root developers ... secret.conf
# ↑ dấu '+' = có ACL
Khi thấy -rw-r--r--+ — dấu + ở cuối nghĩa là file có ACL bổ sung. Dùng getfacl để xem đầy đủ.
lsattr / chattr — Thuộc tính đặc biệt
Ngoài permissions, Linux còn có file attributes ở cấp độ filesystem, override cả root:
# Xem attributes
lsattr /etc/passwd
# ----i--------e-- /etc/passwd
# ↑ 'i' = immutable
# Chattr attributes quan trọng:
# i (immutable) — không thể sửa, xoá, rename, kể cả root
# a (append-only) — chỉ có thể append, không xoá/sửa
# e (extents) — file dùng extents (ext4, thông thường)
# Set immutable — bảo vệ file config quan trọng
sudo chattr +i /etc/hosts
sudo chattr +i /etc/resolv.conf
# Bỏ immutable (cần root)
sudo chattr -i /etc/hosts
# Set append-only cho log file (an toàn cho log rotation)
sudo chattr +a /var/log/app/app.log
# Kiểm tra attribute đã set
lsattr /etc/hosts
# ----i--------e-- /etc/hosts
Nếu root không thể xoá/sửa file và permissions là đúng → kiểm tra lsattr — file có thể đang bị set immutable (+i).
Bảng tổng kết lệnh Permissions
| Tác vụ | Lệnh |
|---|---|
| Xem permissions | ls -la, stat file |
| Đổi quyền (octal) | chmod 755 file |
| Đổi quyền (symbolic) | chmod u+x,g-w file |
| Đổi owner | chown user:group file |
| Đổi owner đệ quy | chown -R user:group dir/ |
| Xem ACL | getfacl file |
| Set ACL cho user | setfacl -m u:alice:rw file |
| Set ACL cho group | setfacl -m g:devops:rwx dir/ |
| Default ACL | setfacl -d -m g:devops:rwx dir/ |
| Xoá ACL | setfacl -x u:alice file hoặc setfacl -b file |
| Xem file attributes | lsattr file |
| Set immutable | sudo chattr +i /etc/hosts |
| Bỏ immutable | sudo chattr -i /etc/hosts |