Ownership and Permissions in Linux Overview
Ownership and permissions in Linux are a concept that determines who can access and modify files and directories. Every file and directory have an owner and a group, which are usually the user who created them and the user’s current group. The owner and group of a file or directory can be changed using the chown command. Permissions can be read, write, or execute, and can be changed using the chmod command. Ownership and permissions are important for security and multi-user environments in Linux as Linux also uses permissions to control the access rights of the owner, group, and others.
File and Ownership Permissions Structure
In Linux, understanding file ownership and permissions is crucial for effective system management and security.
Every file and directory in Linux have three kinds of owners:
- User: The creator of the file becomes its owner. You can change the ownership later.
- Group: Users are part of specific groups. Managing users in a multi-user environment involves creating separate groups (e.g., dev team, QA team, sysadmin team). Group membership simplifies permission management.
- Other: This group includes all users on the system, even if you’re the sole user. Everyone with access to the system belongs to this group.
Each file and directory have three permissions for all three owner types:
- Files
- Read (r): Allows viewing or copying file contents.
- Write (w): Permits modifying file content.
- Execute (x): Enables running executable files (scripts or programs).
- Directories
- Read: Lists files and copies them from the directory.
- Write: Adds or deletes files (requires execute permission).
- Execute: Allows entering the directory.
File Permissions have two modes. An absolute mode and a symbolic mode. The modes detail the type of entries required for permissions to take effect.
- Symbolic Mode
- Symbolic mode allows you to modify permissions based on their current state. You use symbols like +, -, and = to add, subtract, or set permissions.
- + Adds permissions.
- – Removes permissions.
- = Sets permissions explicitly
- Symbolic mode allows you to modify permissions based on their current state. You use symbols like +, -, and = to add, subtract, or set permissions.
- Absolute Mode
- In absolute mode, you explicitly specify the permissions using numeric values (octal notation). These values represent the combination of read, write, and execute permissions for the owner, group, and others.
- The numeric values are as follows:
- 0: No permissions (—)
- 1: Execute only (–x)
- 2: Write only (-w-)
- 3: Write and execute (-wx)
- 4: Read only (r–)
- 5: Read and execute (r-x)
- 6: Read and write (rw-)
- 7: Read, write, and execute (rwx)
- The numeric values are as follows:
- In absolute mode, you explicitly specify the permissions using numeric values (octal notation). These values represent the combination of read, write, and execute permissions for the owner, group, and others.
Viewing File and Directory Permissions
Viewing file and directory ownership and permissions in Linux can be done with each via the command line interface of your Linux server.
The first one is using the ls command with the -l flag, as such ls -l. This command would allow a user to view a list of files within the directory they are in or in another (Using ls -l /path/to/directory) to view the permissions for each file.
The second method is using the stat command to view a more in-depth scope of the file or directory involved.
Modifying File and Directory Permissions
Follow the steps below to proceed with file and directory permissions modification. For this example, we will use the absolute and symbolic modes with the file test_file.txt and the command chmod.
In this example we are modifying a file using the symbolic method by first viewing the file permissions using stat test_file.txt and modifying the permissions using chmod +x test_file.txt. Note that without specifying the user, group, other assignment, then the +x would apply all of them as seen below.
To add permissions specifically to one of the owners, you can use chmod u+x test_file.txt which will add the +x to the user owner.
In this example we are modifying a file using the absolute method by first viewing the file permissions using stat test_file.txt and modifying the permissions using chmod 744 test_file.txt. Note that after the change has been done, the permissions changed from 644 to 744 or also known as rw-r–r– to rwxr–r–.
Thank you for reading and I hope that you enjoyed this Ownership and Permissions in Linux guide. For more guides, please check out our Knowledge Base which is updated weekly!
-Written by Pascal Suissa