In Linux, file permissions are a key aspect of ensuring system security and user access control. They determine who can read, write, or execute files on your system. In this blog, we’ll explore the basics of file permissions and how to manage them using simple commands like ls
, chmod
, chown
, and chgrp
.
File Permissions Overview
File permissions in Linux are structured into three main categories:
Owner: The user who owns the file.
Group: The group associated with the file.
Others: All users who aren't the owner or part of the group.
Each category can have three types of permissions:
Read (r): Allows the user to open and read the file.
Write (w): Allows the user to modify the file.
Execute (x): Allows the user to run the file as a program.
File Permissions Format
When you list files with the ls -l
command, you’ll see the permissions in a string of 10 characters. Here's an example:
-rw-rw-r-- 1 user group 1234 Nov 28 12:34 example.txt
The first character (-
or d
) tells you whether it's a file or a directory. The next nine characters are divided into three sets:
The first three characters represent the owner's permissions.
The next three represent the group's permissions.
The last three represent the others' permissions.
In the example above:
Owner has read and write permissions (
rw-
).Group has read and write permissions (
rw-
).Others have only read permissions (
r--
).
Managing Permissions with chmod
The chmod
command is used to change the permissions of a file. You can modify permissions either using symbolic notation (r, w, x) or numeric notation (0-7).
Symbolic Notation:
r
= Readw
= Writex
= Execute
You can combine them using +
, -
, and =
to add, remove, or set exact permissions:
Adding permissions:
chmod +x filename
(adds execute permission).Removing permissions:
chmod -x filename
(removes execute permission).Setting exact permissions:
chmod u+x filename
(adds execute permission for the user/owner).
Numeric Notation:
Permissions can also be set using numbers. Here’s the mapping:
Permission | Number |
--- | 0 |
--x | 1 |
-w- | 2 |
-wx | 3 |
r-- | 4 |
r-x | 5 |
rw- | 6 |
rwx | 7 |
For example, chmod 755 filename
would set the following permissions:
Owner: Read, write, and execute (7)
Group: Read and execute (5)
Others: Read and execute (5)
Changing Ownership with chown
and chgrp
Besides changing permissions, you may also need to change the ownership of a file or its associated group.
chown
: This command is used to change the owner of a file. For example:chown newowner filename
chgrp
: This command is used to change the group ownership of a file. For example:chgrp newgroup filename
Examples of Using chmod
to Modify Permissions
Let's go through a couple of examples to understand how to modify file permissions:
Read-Only Permissions for Others: If you want to give the owner and the group full permissions, but only allow others to read the file, you can use:
chmod 644 filename
This will set the following permissions:
Owner: Read and write (
rw-
)Group: Read and write (
rw-
)Others: Read-only (
r--
)
Read, Write, and Execute for Owner and Group: If you want the owner and group to have full control over the file, but others should only be able to read it, use:
chmod 775 filename
This will set the following permissions:
Owner: Read, write, and execute (
rwx
)Group: Read, write, and execute (
rwx
)Others: Read-only (
r--
)
Using ls -ltr
to Verify Changes
Once you’ve modified the permissions or ownership, use the ls -ltr
command to verify the changes:
ls -ltr filename
This command lists files with details, including the permissions, and sorts them by time.
Conclusion
Understanding file permissions is fundamental to managing a Linux system. By using commands like chmod
, chown
, and chgrp
, you can control access to files, ensuring that the right users have the appropriate permissions. Remember that file permissions are not only about protecting files, but also about improving system security and preventing unauthorized access.
Now, go ahead and practice these commands on your Linux system to get a better understanding of how file permissions work!