What Are File Permissions?
Linux file permissions control who can read, write, or execute a file or directory. They are set for three user groups:
- Owner: The file creator.
- Group: A user group that can access the file.
- Others: All other users.
Viewing File Permissions
Use the ls -l command to display file permissions:
ls -l
Example Output:
-rwxr-xr-- 1 user group 1234 Nov 16 14:00 file.txt
| Part | Explanation |
|---|---|
-rwxr-xr-- |
Permissions (explained below). |
1 |
Number of hard links. |
user |
Owner of the file. |
group |
Group associated with the file. |
1234 |
File size in bytes. |
Nov 16 14:00 |
Last modification date. |
file.txt |
File name. |
Permission Breakdown
Each permission has three parts:
[Type][Owner][Group][Others]
| Symbol | Type/Permission |
|---|---|
- |
Regular file. |
d |
Directory. |
r |
Read permission. |
w |
Write permission. |
x |
Execute permission (or enter a folder). |
Changing File Permissions
-
Using
chmod(Symbolic Mode):
chmod [permissions] [file]Example: Add execute permission to the owner:
chmod u+x file.txtSymbol Meaning uUser (owner). gGroup. oOthers. aAll (user, group, others). Actions:
Symbol Action +Add permission. -Remove permission. =Set exact permission. Example: Remove write permission for others:
chmod o-w file.txt
-
Using
chmod(Numeric Mode):Each permission level is represented by a number:
Permission Value r(read)4w(write)2x(execute)1-(none)0Combine values to set permissions:
Owner (u) Group (g) Others (o) Command rwx(7)r-x(5)r--(4)chmod 754 file.txt
Changing Ownership
-
Change file owner:
chown [owner] [file]Example:
sudo chown user file.txt -
Change group:
chown :[group] [file]Example:
sudo chown :staff file.txt -
Change both owner and group:
chown [owner]:[group] [file]Example:
sudo chown user:staff file.txt
Recursive Changes
To apply changes to all files and directories:
-
Change permissions recursively:
chmod -R [permissions] [directory]Example:
chmod -R 755 /path/to/folder -
Change ownership recursively:
chown -R [owner]:[group] [directory]Example:
sudo chown -R user:staff /path/to/folder
Testing Permissions
Create a test file or directory to practice:
touch test.txt
chmod 600 test.txt # Owner can read/write, others denied.
chmod 755 test.txt # Everyone can read, owner can write.
chmod -R 770 /test # Owner/group full access, others denied.
Top comments (0)