File Permissions and Ownership in Unix
In Unix-like operating systems, file permissions and ownership control how files and directories are
accessed and modified by users. Understanding these concepts is critical for ensuring security and
proper system functioning.
1. File Ownership
Each file or directory in Unix has three types of ownership:
- User (Owner): The person who created the file or directory. This is the file's owner.
- Group: A collection of users who may also have access to the file. The owner can assign the file to
a group.
- Others: Everyone else who is not the owner or part of the group.
2. File Permissions
There are three types of permissions for each category of user (Owner, Group, Others):
- Read (r): Allows reading the contents of the file or listing the directory.
- Write (w): Allows modifying the file or adding/removing files in a directory.
- Execute (x): Allows running the file as a program or accessing the contents of the directory.
Permissions are represented in a 10-character string:
- The first character indicates the type of file (- for a file, d for a directory, etc.).
- The next three characters are for the owner's permissions.
- The following three are for the group's permissions.
- The final three are for others' permissions.
Example:
-rwxr-xr--
This indicates:
- It's a file (-).
- The owner has read, write, and execute permissions (rwx).
- The group has read and execute permissions (r-x).
- Others have read permissions (r--).
3. Changing Ownership and Permissions
- Changing Ownership:
- To change the owner of a file: chown [owner] [file]
- To change the group: chown :[group] [file]
- To change both: chown [owner]:[group] [file]
- Changing Permissions:
- You can use chmod to change the permissions of a file:
- Symbolic: chmod u+x file (adds execute permission to the owner)
- Numeric: chmod 755 file
- 7 (owner): read, write, execute (rwx -> 4+2+1=7)
- 5 (group): read, execute (r-x -> 4+0+1=5)
- 5 (others): read, execute (r-x -> 4+0+1=5)
4. Special Permissions
- Setuid (s): When set on an executable file, it allows users to run the file with the permissions of the
file's owner.
- Setgid (s): When set on a directory, it ensures files created in the directory inherit the group of the
directory.
- Sticky Bit (t): When set on a directory, only the file's owner or the directory's owner can delete or
rename files within that directory.
Commands to apply special permissions:
- Setuid: chmod u+s file
- Setgid: chmod g+s directory
- Sticky Bit: chmod +t directory
Example of Numeric Representation
Permissions can also be represented by a three-digit number where:
- 4 stands for read (r)
- 2 stands for write (w)
- 1 stands for execute (x)
For example:
chmod 755 file
This means:
- Owner has rwx (4+2+1 = 7)
- Group has r-x (4+0+1 = 5)
- Others have r-x (4+0+1 = 5)
This summarizes how file permissions and ownership work in Unix systems.