Skip to main content

[Thủ Thuật Linux] Tìm hiểu phân quyền File trong Linux

Trong Linux phân chia quyền (permissions) , thuộc tính (attributes) và quyền sở hữu (ownership control) các file và thư mục theo cấp độ quyền truy cập và xử lý đối với người dùng hoặc hệ thống khác nhau, nhằm đảm bảo rằng chỉ những người dùng hoặc quy trình chỉ định nào được ủy quyền mới có thể truy cập vào các file và thư mục cụ thể đó mà thôi.

Phân quyền trong Linux

Về cơ bản, Linux chia hệ thống chủ sở hữu file và thư mục bằng liên kết giữa vai trò là Owner hoặc Group, từ đó sinh ra có 3 lớp phân quyền :

- Quyền Owner

- Quyền của Group các thành viên

- Quyền cho tất cả mọi thành viên

Quyền sở hữu file và thư mục thường sử dụng 2 lệnh chown chgrp.

Linux cũng chia 3 loại quyền thực thi/ hành động với file và thư mục cho người dùng gồm:

- Quyền đọc file (read)

- Quyền ghi file (write)

- Quyền thực thi (execute)

Để xem được phân quyền bạn có thể dùng lệnh ls

ls -l file_name

Kết quả sẽ như: 

-rw-r--r-- 12 linuxize users 12.0K Apr  28 10:10 file_name
|[-][-][-]-   [------] [---]
| |  |  | |      |       |
| |  |  | |      |       +-----------> 7. Group
| |  |  | |      +-------------------> 6. Owner
| |  |  | +--------------------------> 5. Alternate Access Method
| |  |  +----------------------------> 4. Others Permissions
| |  +-------------------------------> 3. Group Permissions
| +----------------------------------> 2. Owner Permissions
+------------------------------------> 1. File Type

Trong ví dụ trên rw-r--r--  được hiểu là chủ sở hữu file có quyền đọc và ghi (rw-) và người khác chỉ có quyền đọc (r--).

Tham khảo quyền của file 

Permission Character Meaning on File
Read - The file is not readable. You cannot view the file contents.
  r The file is readable.
Write - The file cannot be changed or modified.
  w The file can be changed or modified.
Execute - The file cannot be executed.
  x The file can be executed.
  s If found in the user triplet, it sets the setuid bit. If found in the group triplet, it sets the setgid bit. It also means that x flag is set.
When the setuid or setgid flags are set on an executable file, the file is executed with the file’s owner and/or group privileges.
  S Same as s, but the x flag is not set. This flag is rarely used on files.
  t If found in the others triplet, it sets the sticky bit.
It also means that x flag is set. This flag is useless on files.
  T Same as, t but the x flag is not set. This flag is useless on files.

 Tham khảo quyền của thư mục

Permission Character Meaning on Directory
Read - The directory’s contents cannot be shown.
  r The directory’s contents can be shown.
(e.g., You can list files inside the directory with ls.)
Write - The directory’s contents cannot be altered.
  w The directory’s contents can be altered.
(e.g., You can create new files, delete files ..etc.)
Execute - The directory cannot be changed to.
  x The directory can be navigated using cd.
  s If found in the user triplet, it sets the setuid bit. If found in the group triplet it sets the setgid bit. It also means that x flag is set. When the setgid flag is set on a directory, the new files created within it inherits the directory group ID (GID) instead of the primary group ID of the user who created the file.
setuid has no effect on directories.
  S Same as s, but the x flag is not set. This flag is useless on directories.
  t If found in the others triplet, it sets the sticky bit.
It also means that x flag is set. When the sticky bit is set on a directory, only the file’s owner, the directory’s owner, or the administrative user can delete or rename the files within the directory.
  T Same as t, but the x flag is not set. This flag is useless on directories.

Thay đổi phân quyền của file 

Quyền đối với file có thể được thay đổi bằng cách sử dụng lệnh chmod.

Chỉ có root, chủ sở hữu tệp hoặc người dùng có đặc quyền sudo mới có thể thay đổi quyền của tệp.

Hãy hết sức cẩn thận khi sử dụng chmod, đặc biệt là khi thay đổi đệ quy các quyền. Lệnh có thể chấp nhận một hoặc nhiều file và thư mục bằng dấu cách.

Cú pháp của lệnh chmod khi sử dụng có định dạng sau:

chmod [OPTIONS] [ugoa…][-+=]perms…[,…] FILE...

[ugoa…] là Flag của người dùng mặc định là a, trong đó:

  • u - The file owner.
  • g - The users who are members of the group.
  • o - All other users.
  • a - All users, identical to ugo

[-+=] là Flag toán tử định nghĩa như xóa, thêm hoặc gắn.

perms... là phân quyền, nó có thể là số hoặc là chữ cái 

 Nếu là số thì tương ứng như sau: 

  • r (read) = 4
  • w (write) = 2
  • x (execute) = 1
  • no permissions = 0

Một vài ví dụ

#Cấp quyền thực thi file nhưng ko được đọc hoặc viết cho một nhóm
chmod g=x filename

#xóa quyền viết cho tất cả user
chmod a-w filename

#Loại bỏ quyền thực thi cho những user khác
chmod -R o-x dirname

#xóa bỏ quyền đọc, ghi và thực thi cho tất cả user trừ chủ sở hữu
chmod og-rwx filename