Linux - permissions in Linux
What Are Permissions in Linux?
Linux is a multi-user system, meaning many users can share one machine. To keep things safe and organized, Linux assigns permissions to every file and folder.
These permissions determine:
-
Who can read the file
-
Who can write/edit the file
-
Who can execute/run the file (if it’s a script or program)
Three Types of Users
Each file/folder has permissions for three categories of users:
-
Owner (u) – The person who created the file
-
Group (g) – A group the owner belongs to
-
Others (o) – Everyone else
Three Types of Permissions
Symbol | Permission | Meaning |
---|---|---|
r |
read | Can view the file or list directory contents |
w |
write | Can edit the file or add/delete files in a directory |
x |
execute | Can run the file (if it's a script or program) |
Viewing Permissions
Use the ls -l
command:
ls -l
Example output:
-rwxr-xr-- 1 user group 1234 Aug 6 file.sh
Breakdown:
-
First character:
-
= file,d
= directory -
Next 3:
rwx
(owner) -
Next 3:
r-x
(group) -
Last 3:
r--
(others)
So here:
-
Owner can read, write, execute
-
Group can read, execute
-
Others can read only
Changing Permissions with chmod
The chmod
command changes permissions.
Symbolic Mode (letters)
chmod u+x file.sh # Give execute permission to the owner
chmod g-w file.txt # Remove write from group
chmod o+r file.txt # Add read to others
Numeric Mode (numbers)
Each permission is a number:
-
r
= 4 -
w
= 2 -
x
= 1
Add the values to get the full permission.
Permission | Number |
---|---|
rwx |
7 |
rw- |
6 |
r-- |
4 |
--x |
1 |
--- |
0 |
So chmod 755 file.sh
means:
-
Owner:
7
=rwx
-
Group:
5
=r-x
-
Others:
5
=r-x
Changing Ownership with chown
You can change the owner and group of a file:
sudo chown user:group file.txt
Checking Effective Permissions
You can check your access to a file using:
ls -l file.txt
Or:
namei -l /path/to/file
Example Use Cases
Task | Required Permission |
---|---|
Read a text file | r (read) |
Edit a file | w (write) |
Run a script or program | x (execute) |
Enter a directory | x (execute on the directory) |
List files in a directory | r (read on the directory) |