5.4 Special Directories and Filesย (Weight: 1)
๐Linux Essentials (LPI 010-160)
1. File Ownership in Linux
Every file and directory in Linux has an owner and a group.
Owner
The owner is usually the user who created the file.
Example:
-rw-r--r-- 1 student staff 1200 Mar 10 notes.txt
Here:
- student โ file owner
- staff โ group owner
Group
A group is a collection of users. Files can be assigned to a group so that multiple users can share access.
For example:
- Developers group may access application code
- Administrators group may access system configuration files
2. File Permission Types
Linux permissions define what actions are allowed on a file or directory.
There are three basic permission types.
| Permission | Symbol | Meaning |
|---|---|---|
| Read | r | View file content |
| Write | w | Modify file content |
| Execute | x | Run file as a program |
3. Permission Categories
Permissions are assigned to three different user categories.
| Category | Symbol | Description |
|---|---|---|
| Owner | u | User who owns the file |
| Group | g | Users in the file’s group |
| Others | o | All other users |
Example permission output:
-rwxr-xr--
Breakdown:
| Section | Meaning |
|---|---|
| rwx | owner permissions |
| r-x | group permissions |
| r– | others permissions |
4. Viewing File Permissions
To view file permissions, use:
ls -l
Example output:
-rw-r--r-- 1 admin developers 845 Mar 12 script.sh
Explanation:
| Field | Meaning |
|---|---|
| – | file type |
| rw-r–r– | permissions |
| admin | owner |
| developers | group |
5. Changing File Ownership
Ownership can be changed using the chown command.
Basic syntax
chown owner file
Example:
sudo chown admin report.txt
This changes the owner of report.txt to admin.
Changing owner and group together
chown owner:group file
Example:
sudo chown admin:developers project.txt
Now:
- owner = admin
- group = developers
Changing only the group
chown :group file
Example:
sudo chown :developers project.txt
6. Changing Group Ownership
Group ownership can also be changed using the chgrp command.
Syntax
chgrp group file
Example:
chgrp developers project.txt
This assigns the file to the developers group.
7. Changing Permissions
Permissions are changed using the chmod command.
chmod permissions file
There are two methods:
- Symbolic method
- Numeric method
8. Symbolic Method
The symbolic method uses letters to modify permissions.
Structure
chmod [user][operation][permission] file
Users:
| Symbol | Meaning |
|---|---|
| u | owner |
| g | group |
| o | others |
| a | all users |
Operations:
| Symbol | Meaning |
|---|---|
| + | add permission |
| – | remove permission |
| = | set exact permission |
Permissions:
| Symbol | Meaning |
|---|---|
| r | read |
| w | write |
| x | execute |
Examples
Add execute permission for owner:
chmod u+x script.sh
Remove write permission from group:
chmod g-w file.txt
Add read permission for everyone:
chmod a+r document.txt
Set owner permissions exactly:
chmod u=rwx file.sh
9. Numeric (Octal) Method
Linux also supports a numeric method for permissions.
Each permission has a number:
| Permission | Value |
|---|---|
| Read | 4 |
| Write | 2 |
| Execute | 1 |
The values are added together.
Example calculations:
| Permission | Calculation | Value |
|---|---|---|
| rwx | 4+2+1 | 7 |
| rw- | 4+2 | 6 |
| r– | 4 | 4 |
| — | 0 | 0 |
Example
chmod 755 script.sh
Meaning:
| Category | Value | Permission |
|---|---|---|
| Owner | 7 | rwx |
| Group | 5 | r-x |
| Others | 5 | r-x |
Result:
-rwxr-xr-x
Another example
chmod 644 file.txt
Permissions:
| Category | Value | Permission |
|---|---|---|
| Owner | 6 | rw- |
| Group | 4 | r– |
| Others | 4 | r– |
Result:
-rw-r--r--
10. Changing Permissions Recursively
When working with directories containing many files, permissions can be changed recursively.
chmod -R permissions directory
Example:
chmod -R 755 webapp/
This changes permissions for:
- directory
- all files inside it
11. Special Permissions
Linux supports special permission bits that provide advanced access control.
The three special permissions are:
| Permission | Purpose |
|---|---|
| setuid | Run program with owner’s privileges |
| setgid | Run program with group privileges |
| sticky bit | Restrict file deletion in shared directories |
12. SetUID (Set User ID)
SetUID allows a program to run with the permissions of the file owner.
Example command:
chmod u+s program
Example permission display:
-rwsr-xr-x
The s indicates SetUID.
Example IT usage
Some administrative utilities require elevated privileges to access system information. SetUID allows a standard user to run the program while the program temporarily uses the owner’s permissions.
13. SetGID (Set Group ID)
SetGID allows programs to run with the group’s permissions.
Command:
chmod g+s directory
Example display:
drwxr-sr-x
Example IT usage
In a shared project directory:
- files created automatically inherit the same group
- useful for development teams sharing project files
14. Sticky Bit
The sticky bit is used on shared directories.
Command:
chmod +t directory
Example display:
drwxrwxrwt
What it does
Users can only delete:
- files they own
- files owned by root
Example IT usage
Used in shared temporary directories to prevent users from deleting other users’ files.
A well-known example directory is:
/tmp
15. File Types in Permission Display
The first character in ls -l shows the file type.
| Symbol | Type |
|---|---|
| – | regular file |
| d | directory |
| l | symbolic link |
| c | character device |
| b | block device |
Example:
drwxr-xr-x
The d indicates a directory.
16. Best Practices in IT Environments
In professional Linux environments:
Restrict permissions
Files should only allow necessary access.
Example:
Configuration files:
chmod 600 config.conf
Use groups
Teams working on shared resources should use group permissions.
Avoid giving write access to everyone
Permissions like:
chmod 777
should be avoided because they allow any user to modify files.
17. Commands You Must Know for the Exam
| Command | Purpose |
|---|---|
| ls -l | View permissions |
| chmod | Change permissions |
| chown | Change owner |
| chgrp | Change group |
| chmod -R | Change permissions recursively |
18. Quick Exam Summary
For the Linux Essentials exam, remember:
- Every file has owner, group, and others permissions
- Permissions include read (r), write (w), execute (x)
ls -ldisplays permissionschmodchanges permissionschownchanges file ownershipchgrpchanges group ownership- Permissions can be set using symbolic or numeric methods
- Special permissions include setuid, setgid, and sticky bit
