All Blogs
Linux File Permissions explained: A Quick and Practical Guide


Linux File Permissions explained: A Quick and Practical Guide
Understanding file permissions is one of the essential skills for anyone using Linux. Whether you're managing files as a developer or just using Linux regularly, knowing how permissions work will help you maintain a secure and well organised system.
In this article, I'll break down Linux file permissions, explain how to modify them.
Overview of Linux File Permissions
Every file and directory on a Linux system has three sets of permissions, each designed for different users:
- Owner: These permissions apply to the individual who owns the file or directory.
- Group: This set of permissions affects users who are part of a specific group that the file or directory is assigned to.
- Others: These permissions apply to everyone else who isn't the owner or part of the group.
Each of these categories can have the following permissions:
- Read (r): Allows the user to view the contents of a file or list the contents of a directory.
- Write (w): Permits the user to modify or delete the file or its contents.
- Execute (x): Allows the user to run a file if it's a program or script, or access the contents of a directory.
Viewing Permissions in Linux
To view file permissions, you can use the ls -l
command in the terminal. For example, let's say you run the command in a directory and get this output:
- The first character: If it's a d, the entry is a directory. If it's a -, it's a regular file.
- The next nine characters: These represent the permissions for the owner, group, and others. They are split into three sets of three characters each:
rwx
: Permissions for the owner (read, write, execute).rwx
: Permissions for the group (read, write, execute).r-x
: Permissions for others (read and execute, but no write).
1. Changing Ownership: chown
In Linux, you can change the owner and group of a file or directory using the chown
(change owner) command. Here's how it works:
Changing the owner: To change the owner of a file to a user called naveed, use the following command:
sudo chown naveed documents
Now, if you run ls -l
, you'll see that the owner of documents
has changed to naveed
.
Changing both the owner and group: If you also want to change the group to, for example, ubuntu
, use:
sudo chown naveed:ubuntu documents
After running this, the file documents
will now belong to both the user naveed
and the group ubuntu
.
2. Modifying Permissions: chmod
To change permissions, you'll use the chmod
(change mode) command. But first, let's talk about how Linux represents permissions numerically.
The Numerical System
Permissions are represented by a three-digit number:
- Read (
r
) is assigned a value of 4. - Write (
w
) is assigned a value of 2. - Execute (
x
) is assigned a value of 1.
To determine the permission number for each group (owner, group, and others), you add up the values of each permission:
Read and write (rw-
) = 4 + 2 = 6
Read, write, and execute (rwx
) = 4 + 2 + 1 = 7
Read and execute (r-x
) = 4 + 1 = 5
For example, if a file has the permission rw-rw-r--
, the owner and group have read and write permissions (6), while others only have read permissions (4). This is represented as 664
.
Changing Permissions with chmod
Now, if you wanted to add execute permission for the owner and group, you'd modify the permissions to 774
:
sudo chmod 774 documents
This would update the permissions for the file documents to allow the owner and group to read, write, and execute the file, while others can only read it.
Here's another example for a directory:
- Owner: Full permissions (
rwx
, value 7). - Group: Full permissions (
rwx
, value 7). - Others: Read and execute only (
r-x
, value 5).
Common Permission Settings
Here are some commonly used permission settings:
- 644: Files that the owner can read and write, but everyone else can only read.
- 755: Directories where the owner can read, write, and execute, but others can only read and execute.
- 400: Strict permission for key files (like SSH key pairs) where only the owner can read the file, and no one else has any access.
Final Thoughts
Understanding and managing Linux file permissions is crucial for maintaining a secure environment. Whether you're handling sensitive data or just organizing your projects, getting comfortable with chown and chmod will make your workflow smoother.
If you find my articles helpful, be sure to follow me on GitHub! I regularly share useful boilerplate templates that can jumpstart your next project.
About the Author
