Table of contents
Linux Permissions 101
In Linux, file permissions are a fundamental aspect of its security model. They dictate who can read, write, and execute files and directories. Each file and directory has associated permissions for three categories of users: owner, group, and others. Here's a breakdown of how Linux file permissions work, along with examples:
Permission Categories:
- Owner: The user who owns the file or directory.
- Group: Users who are members of the group that the file or directory belongs to.
- Others: Any user who doesn't fall into the above categories.
Types of Permissions:
- Read (r): Allows viewing and reading the contents of a file or listing the contents of a directory.
- Write (w): Allows modifying or deleting a file, and adding, removing, or renaming files within a directory.
- Execute (x): For files, it allows execution (running) of the file as a program. For directories, it allows access (traversal) to its contents.
Symbolic Representation:
- Permissions are represented symbolically using a combination of letters: r for read, w for write, and x for execute.
- Each category of users (owner, group, others) has its set of permission symbols.
Numeric Representation:
- Permissions can also be represented numerically using octal numbers: 4 for read, 2 for write, and 1 for execute.
- The numeric representation is derived by adding the values of the permissions. For example, read (4) + write (2) + execute (1) = 7.
- Each category (owner, group, others) is represented by a three-digit number.
Examples:
- Let's consider a file named
example.txt
with the following permissions:-rw-r--r--
.- The first dash denotes it's a file.
- The owner has read and write permissions (rw-).
- The group has read-only permissions (r--).
- Others have read-only permissions (r--).
- Example of a directory named
example_dir
with permissionsdrwxr-xr-x
.- The first d denotes it's a directory.
- The owner has read, write, and execute permissions (rwx).
- The group has read and execute permissions (r-x).
- Others have read and execute permissions (r-x).
- Let's consider a file named
Changing Permissions:
- Permissions can be changed using the
chmod
command. For example:chmod u+x example.txt
adds execute permission for the owner.chmod go-w example.txt
removes write permission for the group and others.chmod 755 example_dir
sets read, write, and execute permissions for the owner, and read and execute permissions for group and others.
- Permissions can be changed using the
Understanding and managing file permissions are crucial for maintaining security and controlling access to files and directories in Linux systems.
The following table shows what this looks like:
+------------+-------------+---------+--------+-------+--------+
| Permission | Symbolic | Numeric | Binary | Octal | String |
+------------+-------------+---------+--------+-------+--------+
| Owner | rwx | 755 | 111 | 7 | rwx |
| Group | r-x | | 101 | 5 | r-x |
| Others | r-x | | 101 | 5 | r-x |
| Directory | example_dir | | | | |
+------------+-------------+---------+--------+-------+--------+
And some more examples showing different permissions:
+--------------------+-------------+---------+--------+-------+--------+
| Permission | Symbolic | Numeric | Binary | Octal | String |
+--------------------+-------------+---------+--------+-------+--------+
| No Permission | --- | 000 | 000 | 0 | --- |
| Execute | --x | 111 | 001 | 1 | --x |
| Write | -w- | 222 | 010 | 2 | -w- |
| Write+Execute | -wx | 333 | 011 | 3 | -wx |
| Read | r-- | 444 | 100 | 4 | r-- |
| Read+Execute | r-x | 555 | 101 | 5 | r-x |
| Read+Write | rw- | 666 | 110 | 6 | rw- |
| Read+Write+Execute | rwx | 777 | 111 | 7 | rwx |
| File | example.txt | | | | |
+--------------------+-------------+---------+--------+-------+--------+
Conclusion
Understanding and managing file permissions are crucial for maintaining security and controlling access to files and directories in Linux systems.