Permissions
Last updated
Last updated
Each file and directory in Linux has an associated owner, group, and others. The permissions define who can do what.
Each file has three permission types:
r
Read
4
View file contents
w
Write
2
Modify file contents
x
Execute
1
Run file as a program
These permissions apply to three categories:
u
User
The owner of the file
g
Group
Users in the file’s group
o
Others
All other users
Run the ls -l
command to see file permissions.
Example output:
Breaking Down the Output
-rwxr-xr--
→ Permissions
1
→ Number of hard links
user
→ Owner of the file
group
→ Group of the file
1234
→ File size (in bytes)
Jan 1 12:00
→ Last modified date
script.sh
→ Filename
Explaining the Permissions (-rwxr-xr--
)
-
Regular file (d
for directories)
rwx
Owner (User) can read, write, and execute
r-x
Group can read and execute, but not write
r--
Others can only read
chmod
(Change Mode)Symbolic Method
To add, remove, or set permissions using letters:
Numeric Method
Each permission set is represented by a 3-digit number:
rwx = 7 (4+2+1
)
rw- = 6 (4+2
)
r-- = 4 (4
)
Example:
755
7
5
5
rwx
r-x
r-x
Use chown
to change file owner and chgrp
to change file group.
Permissions work similarly for directories:
r
→ List files (ls
)
w
→ Create/delete files
x
→ Enter the directory (cd
)
Example:
To allow everyone to enter:
s
SetUID
Files
Run file as the owner
s
SetGID
Files/Dirs
Run file as the group; inherit group
t
Sticky Bit
Directories
Only the owner can delete files
Example:
777
rwx
rwx
rwx
755
rwx
r-x
r-x
644
rw-
r--
r--
Let's say you have a web directory in /var/www/html
and want:
The owner (www-data
) to have full control (rwx
).
The group (developers
) to read and write (rw-
).
Others should only read (r--
).
Commands:
ChatGPT helped with some of the examples, and thanks to the for helping with tables and examples.