0
0
Linux CLIscripting~5 mins

chmod (change permissions) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes files or folders are locked so you cannot open or change them. The chmod command changes who can read, write, or run a file. This helps you control access to your files safely.
When you want to let a friend open a file but not change it
When you need to make a script file runnable on your computer
When you want to stop others from deleting or editing your important files
When you share a folder and want only certain people to add files
When you fix permission errors that stop programs from working
Commands
This command adds permission for the file owner (user) to run the script.sh file. It makes the script executable by you.
Terminal
chmod u+x script.sh
Expected OutputExpected
No output (command runs silently)
u+x - Add execute permission for the user (file owner)
This command lists the file details including permissions so you can check if the execute permission was added.
Terminal
ls -l script.sh
Expected OutputExpected
-rwxr--r-- 1 user user 123 Apr 27 12:00 script.sh
-l - Show detailed file information including permissions
This command removes read and write permissions for group and others on secret.txt, so only the owner can read or change it.
Terminal
chmod go-rw secret.txt
Expected OutputExpected
No output (command runs silently)
go-rw - Remove read and write permissions for group and others
Check the permissions of secret.txt to confirm group and others cannot read or write it.
Terminal
ls -l secret.txt
Expected OutputExpected
-rw------- 1 user user 456 Apr 27 12:05 secret.txt
-l - Show detailed file information including permissions
Key Concept

If you remember nothing else from chmod, remember: permissions control who can read, write, or run a file, and you change them by adding (+) or removing (-) rights for user (u), group (g), or others (o).

Common Mistakes
Using chmod without specifying user/group/others and plus/minus signs
The command will fail or not change permissions as expected because it needs clear instructions on what to add or remove
Always specify who (u/g/o) and what (+/-) permission to change, like 'chmod u+x file'
Trying to run a script without execute permission
The system will deny running the file because it lacks execute rights
Add execute permission with 'chmod u+x script.sh' before running
Removing all permissions accidentally
You may lock yourself out of the file and cannot read or change it
Check permissions carefully and avoid removing all rights from the user
Summary
chmod changes file permissions to control who can read, write, or execute files.
Use symbols u, g, o to specify user, group, or others and + or - to add or remove permissions.
Check permissions with 'ls -l' to confirm changes.