File and directory permissions are critical to securing a Linux system. This chapter explores how to view, modify, and manage permissions effectively, ensuring only authorized users can access sensitive data.
Linux permissions determine who can read, write, or execute files and directories. Permissions are represented as a string of 10 characters:
-rwxr-xr--
-
First Character:
-
= Regular filed
= Directoryl
= Symbolic link
-
Next Nine Characters (Grouped in threes):
r
= Read (4)w
= Write (2)x
= Execute (1)
Example:
rwx
= Full permissions (4+2+1 = 7)r--
= Read-only (4)rw-
= Read and write (4+2 = 6)
- Owner: The user who created the file.
- Group: A collection of users who share access.
- Others: All other users on the system.
ls -l
Output:
-rw-r--r-- 1 user group 1024 Jan 1 12:34 file.txt
-rw-r--r--
: Permissions1
: Number of hard linksuser
: Ownergroup
: Group1024
: File sizeJan 1 12:34
: Last modified datefile.txt
: File name
- Used to modify file or directory permissions.
chmod [options] mode file
- Give full permissions to the owner:
chmod u+rwx file.txt
- Remove write permission for the group:
chmod g-w file.txt
- Set permissions using octal notation:
chmod 755 file.txt
Octal | Permission | Description |
---|---|---|
7 | rwx |
Full |
6 | rw- |
Read and write |
5 | r-x |
Read and execute |
4 | r-- |
Read-only |
0 | --- |
No permissions |
- Modifies the owner and group of a file or directory.
chown [options] owner[:group] file
- Change the owner:
sudo chown user file.txt
- Change the owner and group:
sudo chown user:group file.txt
- Recursively change ownership for a directory:
sudo chown -R user:group /path/to/directory
- Used specifically to change the group of a file.
sudo chgrp group file.txt
- Executes a file with the permissions of the file owner.
chmod u+s file
- Executes a file with the permissions of the file's group.
chmod g+s file
- Ensures only the file owner can delete files in a directory.
chmod +t /tmp
- Defines default permissions for newly created files and directories.
umask
umask 022
- Provides fine-grained permissions for users and groups.
getfacl file
setfacl -m u:user:rwx file
By the end of this chapter, you should be able to:
- Understand Linux file and directory permissions.
- Modify permissions using
chmod
. - Change ownership with
chown
andchgrp
. - Implement special permissions like SUID, SGID, and sticky bit.
- Utilize
umask
and ACLs for advanced permission management.
- Move to Chapter 6: Process Management to learn about managing and controlling processes in Linux.
- List all files in
/var
with their permissions. - Create a file with
rw-r--r--
permissions and change its group ownership. - Add SUID to a script and verify its behavior.
- Set ACL to grant a specific user write access to a file they don’t own.
- Test sticky bit functionality in a shared directory.