Linux File Permission Chmod Chown Explained: In-depth & Safe

Linux File Permission Chmod Chown Giải Thích: Chuyên Sâu & An Toàn

Getting the "Permission denied" error on Linux is really frustrating, isn't it? I also struggled with it during the first years of my career. It turns out that the key to completely solving the problem is not unfamiliar but lies in the problem of Linux file permission chmod chown explained thoroughly. Mastering this duo not only helps you fix minor errors, but is also the first step to becoming a tough admin or DevOps, knowing how to protect the system absolutely safely.

Decoding the permissions matrix: Who can do what on Linux files?

Permissions in Linux are a system of security rules that determine exactly which accounts can read, write or run a specific file or folder on the server.

When first switching from a Windows environment to a Linux operating system, the decentralized structure often confuses people because it is too strict. However, the meaning of Linux file decentralization is actually very logical. This system is designed to protect Linux file structures from unwanted interference, prevent viruses from spreading and isolate the risk of a compromised account. At Pham Hai, I always emphasize to the interns that correctly understanding authority is the number 1 foundation of security.

Who are the three core user classes: User, Group, and Others?

Linux divides users interacting with files into 3 basic groups: User (owner), Group (user group) and Others (remaining people on the system).

Understanding owners and groups in Linux will help you grant permissions to the right people, for the right crimes. Specifically as follows:

  • User (u): Đây là người tạo ra file hoặc được gán quyền sở hữu. Họ giống như "chủ nhà", thường có quyền lực cao nhất đối với tệp tin đó.
  • Group (g): Một tập hợp các user có chung mục đích làm việc. Ví dụ, team phát triển web của bạn có 5 người, bạn gom họ vào chung một group devteam để cùng chỉnh sửa source code mà không ảnh hưởng tới các team khác.
  • Others (o): Tất cả những tài khoản còn lại trên hệ thống không phải là chủ sở hữu, cũng không nằm trong nhóm sở hữu. Đây thường là "khách vãng lai" hoặc các tiến trình chạy ngầm.

What exactly are the three basic powers: Read, Write, and Execute?

Read and write permissions in Linux (abbreviated as r, w, x) specify specific actions that a user class is allowed to operate on a file or directory.

Each of these permissions has different meanings depending on whether the applied object is a file or folder:

Power Symbol Applies on File Apply on Directory
Đọc (Read) r Xem được nội dung bên trong file (ví dụ cat file.txt). Liệt kê được danh sách các file bên trong thư mục (lệnh ls).
Ghi (Write) w Edit, add or remove content or empty the file. Create new files, rename or delete files inside that folder.
Thực thi (Execute) x Chạy file như một chương trình/script (ví dụ file .sh). Cho phép dùng lệnh cd để đi vào bên trong thư mục đó.

Đọc vị nhanh quyền hạn của file và thư mục với lệnh ls -l

Lệnh ls -l hiển thị chi tiết quyền truy cập file/thư mục dưới dạng một chuỗi 10 ký tự ở đầu mỗi dòng, giúp bạn nắm bắt nhanh tình trạng phân quyền hiện tại.

Mở terminal lên và gõ ls -l, bạn sẽ thấy một danh sách trả về với cột đầu tiên trông giống như -rwxr-xr--. Việc đọc thành thạo chuỗi này là kỹ năng bắt buộc. Nếu bạn đang học Quản trị Linux server cơ bản cho developer, hãy tập phân tích chuỗi này mỗi ngày:

  • Ký tự 1: Loại tệp (- là file thông thường, d là thư mục, l là symlink).
  • 3 ký tự tiếp theo (rwx): Quyền của User (Chủ sở hữu có quyền đọc, ghi, chạy).
  • 3 ký tự tiếp theo (r-x): Quyền của Group (Nhóm chỉ được đọc và chạy, không được ghi).
  • 3 ký tự cuối cùng (r--): Quyền của Others (Người ngoài chỉ được phép đọc).

chmod - "Phù thủy" ban phát quyền lực trong Linux

Lệnh chmod (viết tắt của change mode) được sử dụng để thay đổi quyền truy cập (r, w, x) của các đối tượng user, group và others trên một file hoặc thư mục cụ thể.

Cách sử dụng lệnh chmod trong Linux rất linh hoạt và mạnh mẽ. Là một quản trị hệ thống, bạn sẽ dùng lệnh này gần như mỗi ngày. Có hai phương pháp chính để ra lệnh cho chmod: dùng số (bát phân) hoặc dùng chữ (tượng trưng). Mình khuyên bạn nên thành thạo cả hai.

Using numbers to give commands: Explaining the chmod modes with numbers and symbols that are easiest to understand

Octal Mode uses octal values ​​from 0 to 7 to represent the sum of the r (4), w (2), and x (1) permissions for each user group.

Many of you have just found the explanation of chmod modes with numbers and symbols very confusing. In fact, the principle of octal values ​​is extremely simple. You just need to remember this table of fixed values:

  • Quyền Đọc (r) = 4
  • Quyền Ghi (w) = 2
  • Quyền Thực thi (x) = 1
  • Không có quyền (-) = 0

Để thiết lập quyền, bạn cộng các số này lại. Ví dụ: Quyền đọc và ghi (r+w) sẽ là 4 + 2 = 6. Quyền đọc, ghi, thực thi (r+w+x) là 4 + 2 + 1 = 7. Khi gõ lệnh chmod 644 config.php, hệ thống sẽ hiểu:

  • First 6 number (User): 4+2 (Read and Write).
  • Second 4 number (Group): 4 (Read only).
  • Third 4 number (Others): 4 (Read only).

Chmod 777 - Ultimate power or double-edged sword? Why do I advise you to never use it?

Chmod 777 grants full rights (read, write, execute) to everyone (User, Group, Others) on the system, creating an extremely serious security hole.

Hồi mới vào nghề, hễ cứ web lỗi không upload được ảnh là mình lại gõ chmod -R 777. Nhiều bạn cũng hay thắc mắc chmod 777 là gì có an toàn không? Câu trả lời từ kinh nghiệm xương máu của mình là: Tuyệt đối KHÔNG. Khi bạn cấp quyền 777, bất kỳ ai truy cập vào server (kể cả một đoạn mã độc chạy ẩn) cũng có thể xóa sạch file của bạn. Để hệ thống sống sót, bạn nên ưu tiên tìm hiểu các phương pháp Bảo mật VPS Linux chống hack tấn công đúng chuẩn thay vì lạm dụng 777 để chữa cháy tạm thời.

Use text to communicate: Symbolic Mode for people who like clarity

Symbolic Mode uses representative letters (u, g, o, a) combined with operators (+, -, =) to add, remove or assign permissions accurately.

If you don't like adding and subtracting numbers, the symbolic mode is true love. Its syntax is very close to natural language:

  • Đối tượng: u (user), g (group), o (others), a (all - tất cả).
  • Toán tử: + (thêm quyền), - (tước quyền), = (gán quyền tuyệt đối, xóa các quyền cũ). Ví dụ thực tế: Bạn muốn thêm quyền chạy file cho chủ sở hữu, hãy gõ chmod u+x script.sh. Nếu muốn tước quyền ghi của những người ngoài, gõ chmod o-w data.txt.

"Quét" toàn bộ thư mục: Đừng quên tùy chọn -R thần thánh

Tùy chọn -R (Recursive) áp dụng lệnh chmod đệ quy cho thư mục hiện tại và toàn bộ các file, thư mục con nằm sâu bên trong nó.

Khi thực hiện phân quyền thư mục trong Linux, đặc biệt là các thư mục chứa mã nguồn với hàng ngàn file, bạn không thể gõ lệnh cho từng tệp được. Lệnh chmod -R và chown -R sinh ra để tự động hóa việc này. Chẳng hạn, lệnh chmod -R 755 /var/www/html/ sẽ "quét" từ trên xuống dưới, áp dụng quyền 755 cho mọi thứ bên trong thư mục html. Tuy nhiên, hãy dùng cẩn thận vì nó có thể làm sai lệch quyền của các file cấu hình nhạy cảm.

chown - Trao quyền sở hữu, xác định "chủ nhân" của file

Lệnh chown (viết tắt của change owner) giúp quản trị viên thay đổi chủ sở hữu (user) và nhóm sở hữu (group) của một tệp hoặc thư mục.

Trong công việc hàng ngày của một kỹ sư DevOps, cách dùng lệnh chown trong Linux luôn song hành với chmod. Nếu chmod quy định "được làm những gì", thì chown quyết định "ai là người làm chủ". Bạn không thể phân quyền hiệu quả nếu file đang thuộc về sai người.

Who is the file owner? How to change owner (user) and group (group)

Bạn sử dụng cú pháp chown new_user filename để đổi chủ sở hữu, hoặc lệnh chgrp new_group filename nếu chỉ muốn đổi nhóm sở hữu của file.

Mặc định, tài khoản nào tạo ra file thì tài khoản đó là chủ sở hữu. Nhưng trong thực tế, bạn thường xuyên phải giao file cho user khác quản lý. Lưu ý quan trọng: vì việc đổi "sổ đỏ" của file ảnh hưởng đến bảo mật, bạn luôn phải thêm lệnh sudo phía trước để có đủ thẩm quyền root thực thi. Ví dụ: sudo chown john report.pdf.

Cú pháp user:group - Bí quyết thay đổi cả chủ và nhóm chỉ bằng một lệnh

Cú pháp chown user:group filename cho phép bạn thay đổi đồng thời cả chủ sở hữu và nhóm sở hữu chỉ trong một dòng lệnh duy nhất.

Đây là "tuyệt chiêu" giúp anh em admin tiết kiệm thời gian gõ phím. Thay vì dùng hai lệnh rời rạc, bạn gộp chúng lại bằng dấu hai chấm :. Ví dụ, khi bạn thực hành Cấu hình VPS Ubuntu chạy WordPress từ đầu, một thao tác bắt buộc là phải trao quyền sở hữu toàn bộ thư mục web cho user của web server bằng lệnh: sudo chown www-data:www-data /var/www/html.

Thay đổi chủ sở hữu hàng loạt với chown -R

Tương tự như chmod, việc thêm cờ -R vào lệnh chown sẽ thay đổi chủ sở hữu cho toàn bộ cây thư mục bên trong một cách đệ quy.

Cú pháp thực hiện cực kỳ gọn gàng: sudo chown -R alice:devteam /var/projects/. Sau khi nhấn Enter, toàn bộ file cũ, file mới, thư mục con nằm trong projects sẽ lập tức đổi chủ sang user alice và thuộc về nhóm devteam.

Real combat situations: Fix "Permission denied" errors and common scenarios

The "Permission denied" error occurs when the current account does not have enough permissions (read/write/execute) or the wrong owner to operate on the target file/folder.

With experience configuring hundreds of servers, I find that fixing the Permission denied error on Linux is a "regular" job. Below is how we diagnose and treat the most common cases.

Case study 1: Grant execution permission to a just downloaded script file

File script cài đặt tải từ internet mặc định không có quyền thực thi để bảo vệ an toàn. Bạn cần dùng chmod +x để cấp quyền trước khi chạy.

Bạn dùng wget tải một file install.sh về, gõ ./install.sh và hệ thống báo lỗi từ chối truy cập. Đừng hoảng! Linux làm vậy để ngăn virus tự động chạy. Cách giải quyết chuẩn xác nhất là gõ chmod +x install.sh để thêm quyền (x) cho file, sau đó chạy lại.

Case study 2: Delegate permissions to the web server directory (eg /var/www/html) for safety

Setting security standards for the web server is to assign permissions 755 to the entire folder, permissions 644 to all files, and set the owner as the user of the web service.

Tuyệt đối không cấp quyền bừa bãi cho thư mục web. Tùy thuộc vào việc bạn đang dùng công nghệ nào, user sở hữu sẽ khác nhau. Nếu bạn đang nghiên cứu Nginx vs Apache so sánh web server 2026, hãy nhớ: Apache thường dùng user www-data (trên Ubuntu) hoặc apache (trên CentOS), trong khi Nginx thường dùng nginx. Lệnh chuẩn để set quyền hàng loạt là:

  • Đổi chủ: sudo chown -R www-data:www-data /var/www/html
  • Set quyền thư mục: find /var/www/html -type d -exec chmod 755 {} ;
  • Set quyền file: find /var/www/html -type f -exec chmod 644 {} ;

Case study 3: User cannot write files to folder even though permission is granted?

A parent directory lacking execute permissions (x) or a file locked by extended attributes (chattr) will prevent the user from writing data even though the file has w permissions.

Gặp ca này anh em rất hay vò đầu bứt tai. Mình gợi ý bạn kiểm tra ngược lên thư mục chứa file đó. Nếu thư mục cha bị mất quyền x (truy cập), hệ thống sẽ chặn đứng bạn từ vòng ngoài, bạn không thể chạm tới file bên trong dù file đó có quyền 777 đi nữa. Ngoài ra, hãy dùng lệnh lsattr để xem file có bị gán cờ i (immutable - không thể sửa đổi) hay không.

Improve your skills: "Pro" decentralization concepts you should know

In addition to basic r-w-x, Linux also provides umask, SUID, SGID, and Sticky Bit to control complex permission flow in multi-user environments.

To truly master the system, you need to go beyond basic command typing. Mastering these advanced concepts will help you design professional security architecture that separates you from the masses.

umask là gì? Thiết lập quyền mặc định cho file và thư mục mới tạo

User mask (umask) is the value subtracted from the maximum permissions (666 for files, 777 for directories) to determine the default permissions when a new file is created.

Every time you create a new file, Linux does not ask what permissions you want to assign. It uses umask to decide for itself. The default user mask value on most distros is usually 022. The calculation is as follows:

  • Newly created folder: 777 - 022 = 755 (rwxr-xr-x).
  • File mới tạo: 666 - 022 = 644 (rw-r--r--). Bạn có thể gõ lệnh umask trong terminal để kiểm tra, hoặc thêm lệnh umask 027 vào file ~/.bashrc để siết chặt bảo mật hơn cho tài khoản của mình.

Special permission bits: SetUID (SUID), SetGID (SGID) and Sticky Bit

These are 3 special flags that allow file execution with owner permissions (SUID/SGID) or prevent unauthorized deletion of files in shared folders (Sticky Bit).

In large systems, you will encounter these flags constantly:

  • setuid (SUID - Ký hiệu s ở quyền User): Khi chạy file này, tiến trình sẽ mượn quyền của người tạo ra file. Ví dụ điển hình là lệnh passwd. Dù bạn là user thường, nhờ có setuid, lệnh này tạm thời mượn quyền root để sửa file /etc/shadow nhằm đổi mật khẩu.
  • setgid (SGID - Ký hiệu s ở quyền Group): Đặt trên thư mục, bất kỳ file nào tạo bên trong sẽ tự động kế thừa group của thư mục cha, thay vì group của người tạo. Rất hữu ích cho thư mục làm việc chung của team.
  • sticky bit (Ký hiệu t ở quyền Others): Đặt trên các thư mục public (như /tmp). Nó quy định luật chơi: "Ai tạo file thì người đó mới được xóa". Điều này ngăn chặn user A xóa nhầm hoặc cố tình xóa file của user B trong thư mục chung.

Decentralization in Linux is not as dry and complicated as you think. It's like you are the guardian of your house, deciding who can enter, who can use things, and who can just stand and watch. Mastering the nature of Linux file permission chmod chown explained means you are holding the universal key to both master the system and build a solid security barrier. Don't just read, open the terminal and try it now!

Do you have a more "sweet" decentralization situation? Please share in the comments section, I and everyone will dissect it together!

Note: The information in this article is for reference only. For the best advice, please contact us directly for specific advice based on your actual needs.

Categories: Bảo Mật Wordpress

mrhai

Để lại bình luận