Basic Permissions:
Each file or directory has three types of permissions:
- Read (r): Allows viewing the contents of a file.
- Write (w): Allows modifying or deleting a file.
- Execute (x): Allows running the file as a program (for scripts or executable files).
Permissions are assigned to three categories of users:
- Owner: The user who owns the file.
- Group: A group of users who share access to the file.
- Others: All other users who are not the owner or part of the group.
Command Syntax:
chmod [permissions] [file/directory]
Methods for Using chmod:
There are two ways to assign permissions with chmod: symbolic and numeric (octal).
- Symbolic Method
You can use symbols to modify permissions. These symbols include:
- + : Adds a permission
- – : Removes a permission
- = : Sets a specific permission
For example:
- u : User (owner)
- g : Group
- o : Others
- a : All (user, group, and others)
Example commands:
- Add execute permission to the owner:
chmod u+x file.txt
- Remove write permission from others:
chmod o-w file.txt
- Set read and write permissions for the user, and read permission for the group and others:
chmod u=rw,g=r,o=r file.txt
- Numeric (Octal) Method
Permissions can also be represented with a numeric code:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
To assign permissions, you sum the values for each user category. The format is three digits: owner-group-others.
For example:
- 7 = read (4) + write (2) + execute (1)
- 6 = read (4) + write (2)
- 5 = read (4) + execute (1)
- 4 = read only
- 0 = no permissions
Example commands:
- Give full permissions to the owner, read and execute to the group, and no permissions to others:
chmod 750 file.txt
Breakdown:
-
- Owner: 7 (rwx)
- Group: 5 (r-x)
- Others: 0 (—)
- Set read and write for all users:
chmod 666 file.txt
- Make a script executable for everyone:
chmod 755 script.sh
Common Examples:
- chmod 777 [file] : Give read, write, and execute permissions to everyone (owner, group, others).
- chmod 644 [file] : Give read and write permissions to the owner, and only read permission to the group and others.
- chmod 600 [file] : Only the owner has read and write access; no one else can access the file.
The chmod command is a critical tool for managing file security in Linux systems.