Bird
Raised Fist0
Linux CLIscripting~10 mins

chmod (change permissions) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - chmod (change permissions)
Start
↓
Input chmod command
↓
Parse permissions and target file
↓
Check user rights to change permissions
Yes No↓
Apply new permissions
↓
Permissions updated
↓
End
The chmod command takes user input, checks permissions, and updates file permissions if allowed.
Execution Sample
Linux CLI
chmod u+x script.sh
ls -l script.sh
Adds execute permission for the user on script.sh and then lists its permissions.
Execution Table
StepCommandActionPermissions BeforePermissions AfterOutput
1ls -l script.shShow current permissions-rw-r--r---rw-r--r---rw-r--r-- 1 user user 0 date script.sh
2chmod u+x script.shAdd execute permission for user-rw-r--r---rwxr--r--
3ls -l script.shShow updated permissions-rwxr--r---rwxr--r---rwxr--r-- 1 user user 0 date script.sh
4End of commands-rwxr--r---rwxr--r--
💡 Permissions updated successfully after chmod command
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Permissions of script.sh-rw-r--r---rw-r--r---rwxr--r---rwxr--r---rwxr--r--
Key Moments - 2 Insights
Why does the execute permission only change for the user and not for group or others?
Because the command uses 'u+x' which means add execute permission only for the user, as shown in step 2 of the execution_table.
What happens if you try to change permissions without proper rights?
The system will show a 'Permission denied' error and permissions won't change, as indicated in the concept_flow where 'No' leads to an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what permission is added to script.sh?
ARead permission for others
BWrite permission for group
CExecute permission for user
DExecute permission for others
💡 Hint
Check the 'Permissions After' column at step 2 in the execution_table.
At which step does the permissions of script.sh change?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Permissions After' column to see when the change happens.
If the command was 'chmod g+x script.sh', how would the permissions change compared to the example?
AAdd execute permission for group instead of user
BAdd execute permission for others
CRemove execute permission for user
DNo change
💡 Hint
Refer to the meaning of 'u+x' vs 'g+x' in the key_moments and execution_table.
Concept Snapshot
chmod changes file permissions.
Syntax: chmod [who][+/-][permission] file
Who: u=user, g=group, o=others, a=all
Permissions: r=read, w=write, x=execute
Example: chmod u+x file adds execute for user.
Full Transcript
The chmod command changes file permissions by adding or removing rights for user, group, or others. In the example, 'chmod u+x script.sh' adds execute permission for the user only. Before the command, the file had '-rw-r--r--' permissions. After, it changed to '-rwxr--r--'. The ls -l command shows these permissions before and after the change. If you lack rights to change permissions, chmod will show an error and not update the file. This visual trace shows each step and how permissions update.

Practice

(1/5)
1. What does the chmod 755 filename command do in Linux?
easy
A. Removes all permissions from everyone
B. Sets read and write for owner; execute for group and others
C. Sets read, write, execute for owner; read and execute for group and others
D. Only changes the file owner

Solution

  1. Step 1: Understand numeric permission 755

    The digits 7, 5, 5 represent permissions for owner, group, and others respectively. 7 means read(4)+write(2)+execute(1)=7, 5 means read(4)+execute(1)=5.
  2. Step 2: Apply permissions to file

    Owner gets full permissions (rwx), group and others get read and execute (r-x). This matches the description in Sets read, write, execute for owner; read and execute for group and others.
  3. Final Answer:

    Sets read, write, execute for owner; read and execute for group and others -> Option C
  4. Quick Check:

    chmod 755 = rwxr-xr-x [OK]
Hint: Remember 7=rwx, 5=rx, 6=rw [OK]
Common Mistakes:
  • Confusing numeric codes with letters
  • Thinking 755 removes permissions
  • Assuming chmod changes ownership
2. Which of the following is the correct syntax to add execute permission for the user on a file named script.sh?
easy
A. chmod +x u script.sh
B. chmod script.sh u+x
C. chmod x+u script.sh
D. chmod u+x script.sh

Solution

  1. Step 1: Recall chmod symbolic syntax

    The correct format is chmod [who][operator][permission] filename. Here, 'u' means user, '+' adds permission, 'x' is execute.
  2. Step 2: Match syntax to options

    chmod u+x script.sh matches the correct syntax: chmod u+x script.sh. Other options have incorrect order or misplaced arguments.
  3. Final Answer:

    chmod u+x script.sh -> Option D
  4. Quick Check:

    chmod u+x filename adds execute for user [OK]
Hint: Use chmod [u/g/o][+/-][r/w/x] filename [OK]
Common Mistakes:
  • Placing permissions before user
  • Putting filename before options
  • Using spaces inside symbolic mode
3. What will be the output of ls -l file.txt after running chmod 640 file.txt on a file owned by user 'alice' and group 'staff'?
medium
A. -rw-r----- 1 alice staff 0 date file.txt
B. -rwxr-xr-x 1 alice staff 0 date file.txt
C. -rw-rw-r-- 1 alice staff 0 date file.txt
D. -r--r----- 1 alice staff 0 date file.txt

Solution

  1. Step 1: Decode chmod 640 permissions

    6 = read(4)+write(2) for owner, 4 = read(4) for group, 0 = no permissions for others.
  2. Step 2: Translate to ls -l format

    Owner: rw-, Group: r--, Others: ---. So the permission string is '-rw-r-----'. The rest shows ownership and file info.
  3. Final Answer:

    -rw-r----- 1 alice staff 0 date file.txt -> Option A
  4. Quick Check:

    chmod 640 = rw-r----- [OK]
Hint: Match digits to rwx: 6=rw-, 4=r--, 0=--- [OK]
Common Mistakes:
  • Mixing group and others permissions
  • Assuming execute permission is set
  • Confusing numeric values
4. You ran chmod 777 myfile but ls -l myfile still shows -rw-r--r--. What is the most likely reason?
medium
A. You forgot to run the command as root or with sudo
B. The file system is mounted as read-only
C. The chmod command syntax is incorrect
D. The file is a directory, not a file

Solution

  1. Step 1: Understand chmod effect and file system state

    chmod 777 should set full permissions. If permissions don't change, it may be due to file system restrictions like read-only mount.
  2. Step 2: Evaluate other options

    Running as root is not always required for own files. Syntax is correct. File type does not prevent permission changes. Read-only file system blocks changes.
  3. Final Answer:

    The file system is mounted as read-only -> Option B
  4. Quick Check:

    Read-only FS blocks chmod changes [OK]
Hint: Check if filesystem is read-only if chmod fails [OK]
Common Mistakes:
  • Assuming sudo always needed
  • Blaming syntax without checking
  • Ignoring file system mount options
5. You want to set permissions so that the owner can read and write, the group can only read, and others have no permissions on a file named data.txt. Which command achieves this?
hard
A. chmod 640 data.txt
B. chmod 750 data.txt
C. chmod 664 data.txt
D. chmod 600 data.txt

Solution

  1. Step 1: Translate desired permissions to numeric code

    Owner: read+write = 6, Group: read only = 4, Others: no permissions = 0. So numeric code is 640.
  2. Step 2: Match numeric code to options

    chmod 640 data.txt is chmod 640 data.txt, which matches the desired permissions exactly.
  3. Final Answer:

    chmod 640 data.txt -> Option A
  4. Quick Check:

    Owner=rw, Group=r, Others=none = 640 [OK]
Hint: Use chmod 640 for rw-r----- permissions [OK]
Common Mistakes:
  • Using 750 which gives execute permission
  • Using 664 which allows others read
  • Using 600 which denies group read
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard