In Ubuntu, file permission management is fundamental to ensuring system security and data protection. Imagine the consequences if your documents could be modified by anyone at will, or if critical system files were accidentally deleted. Therefore, mastering file permission management not only safeguards your data but also prevents operational errors.
I. What Are File Permissions?¶
Ubuntu (based on Linux) controls who can perform which operations on files through permissions. Permissions act like “access control” for files, specifying the actions (read, write, execute) allowed for three types of users: owner, group, and others.
II. Three Key Elements of Permissions¶
-
Three User Types (Who has permissions?):
- Owner (u) : The creator of the file.
- Group (g) : Users belonging to the same group as the owner.
- Others (o) : All other users on the system not in the owner or group. -
Three Permission Types (What actions can be performed?):
- Read (r) : View file content (for directories, this means “enter the directory”).
- Write (w) : Modify file content (for directories, this means “create/delete files”).
- Execute (x) : Run the file (for directories, this means “enter the directory”; without execute permission,cdcannot be used to enter).
III. Permission Representation¶
1. Symbolic Form (Intuitive and Readable)¶
Permissions are represented using rwx for each user type, with each user type occupying 3 characters ( - indicates no permission). For example:
-rwxr-xr--
- The first character - denotes the file type (d for directory, - for regular file).
- Positions 2-4: rwx (Owner: read, write, execute).
- Positions 5-7: r-x (Group: read, no write, execute).
- Positions 8-10: r-- (Others: read, no write, no execute).
2. Numeric Form (Quick Calculation)¶
Permissions are simplified with numbers: r=4, w=2, x=1. Each user type’s permission is summed as a 3-digit number. For example:
- Owner rwx: 4+2+1=7
- Group r-x: 4+0+1=5
- Others r--: 4+0+0=4
Combined, this gives 754, corresponding to the symbolic form rwxr-xr--.
IV. How to View File Permissions?¶
Use the ls -l command to display detailed file information, with the first column showing permissions:
ls -l 文件名 # Example: ls -l test.txt
Output Example:
-rw-r--r-- 1 root root 1024 10-01 12:00 test.txt
- 1st column -rw-r--r--: Permission type and symbolic form.
- 2nd column 1: Number of hard links (not critical for now).
- 3rd column root: Owner.
- 4th column root: Group.
- 5th column 1024: File size (bytes).
- 6th-7th columns 10-01 12:00: Modification time.
- Last column test.txt: Filename.
V. How to Modify File Permissions?¶
1. Using chmod (Change Permissions)¶
chmod has two methods: symbolic mode (intuitive) and numeric mode (fast).
(1) Symbolic Mode: u/g/o/a + +/-/=¶
u(owner),g(group),o(others),a(all users).+(add),-(remove),=(set).- Permissions:
r(read),w(write),x(execute).
Examples:
- Add execute permission to the owner: chmod u+x test.sh
- Remove write permission from the group: chmod g-w test.txt
- Set read permission for all users: chmod a+r *.txt
(2) Numeric Mode: Directly Specify by Number¶
Syntax: chmod [number] 文件名, where the number is 3 digits (owner, group, others).
Examples:
- Set permissions to rwxr-xr-x (755): chmod 755 test.sh
- Set permissions to rw-r--r-- (644): chmod 644 test.txt
2. Modify Owner/Group (chown/chgrp)¶
chown: Change the owner. Example:sudo chown newuser test.txt(requires admin privileges).chgrp: Change the group. Example:sudo chgrp newgroup test.txt(requires admin privileges).
VI. Common Scenarios and Precautions¶
-
Special Directory Permissions:
Directory execute permission (x) is critical! Without it, even with read permission, you cannot enter the directory. For example:
Aftermkdir testdir, the default permission isdrwxr-xr-x(755). Ifxis removed:chmod o-x testdir, regular users can no longer enter this directory. -
Default Permission Rules:
New files default torw-r--r--(644), and new directories default torwxr-xr-x(755). These are determined byumask; beginners can remember the defaults. -
Permission Security Principles:
- Avoid777for non-essential files (full read/write/execute access for all, highly vulnerable to tampering).
- System-critical files (e.g.,/etc/) should only be modified by admins; regular users must usesudo.
VII. Summary¶
File permission management is a core foundation in Ubuntu. Mastery of chmod (change permissions), chown (change owner), and chgrp (change group) suffices for most scenarios. Key points: clearly express permission needs via symbolic or numeric notation, and always prioritize system security (e.g., avoid overusing 777).
For advanced learning, explore SUID/SGID/Sticky Bit, but basic permission management meets daily needs. Practice: Create a file → modify permissions → observe changes to master quickly!