๐ง How to Use Linux chmod Command (Octal Permissions) | ๐ Includes Practice Questions
Summary
Learn how to set file and directory access permissions using the octal notation of the Linux chmod command, a common topic in the Information Processing Engineer exam.
๐ก What is the chmod
command?
chmod
, short for 'change mode', is a command in Linux and Unix-like operating systems used to change the access permissions of files or directories. In the Information Processing Engineer practical exam, you are often asked to grant specific permissions and construct the command using octal notation.
๐ Types and Targets of Access Permissions
To understand chmod
, you first need to know the types of permissions and to whom they apply.
1. Types of Permissions
Permission | Symbol | Description | Octal Value |
---|---|---|---|
Read | r | Read file content or view directory listings | 4 |
Write | w | Modify/delete file content, or create/delete files in a directory | 2 |
Execute | x | Execute a file or access a directory (cd ) | 1 |
2. Permission Targets
Target | Symbol | Description |
---|---|---|
User | u | The user who owns the file |
Group | g | The group the file belongs to |
Others | o | All other users |
๐ข Setting Permissions with Octal Notation
The core of the chmod
command is its octal notation. You create a permission combination by adding the numeric values assigned to each permission (r, w, x).
- r (Read) = 4
- w (Write) = 2
- x (Execute) = 1
For example, read and write permissions (rw-
) are 4 + 2 = 6
.
Octal | Permission Combination (rwx ) | Description |
---|---|---|
7 | rwx | Full read, write, and execute |
6 | rw- | Read and write |
5 | r-x | Read and execute |
4 | r-- | Read-only |
3 | -wx | Write and execute |
2 | -w- | Write-only |
1 | --x | Execute-only |
0 | --- | No permissions |
The command is structured as chmod [user][group][others] [filename]
. For example, chmod 754 file.txt
means:
- User (7):
rwx
(read+write+execute) - Group (5):
r-x
(read+execute) - Others (4):
r--
(read-only)
๐ Practice Problems for the Information Processing Engineer Exam
Problem 1
Problem | On a Linux system, you need to change the access permissions for `report.txt` as follows. Complete the `chmod` command using octal notation. - User: Read and write permissions - Group: Read permission - Others: No permissions |
Your Answer | |
Correct Answer | Reveal Answer |
Problem Solution
Convert the permissions for each target into octal numbers.
- User: Read(4) + Write(2) = 6
- Group: Read(4) = 4
- Others: No permissions = 0
Combining these numbers gives 640
. Therefore, the full command is:
chmod 640 report.txt
Problem 2
Problem | There is a shell script file named `script.sh`. The file's owner should have all permissions, while the group and others should only have read and execute permissions. Write the `chmod` command to set these permissions. |
Your Answer | |
Correct Answer | Reveal Answer |
Problem Solution
Convert the permissions for each target into octal numbers.
- User: Read(4) + Write(2) + Execute(1) = 7
- Group: Read(4) + Execute(1) = 5
- Others: Read(4) + Execute(1) = 5
Combining these numbers gives 755
. Therefore, the full command is:
chmod 755 script.sh