🔐 Command: chmod (Change Mode)
The chmod command is used to change the access permissions of files and directories. In Linux, every file has a set of rules determining who can read it, write to it, or execute it as a program.
1. The "Why"
On Linux system, security is built into the filesystem. You will use chmod when:
- You’ve written a script (like a Bash or Python script) and need to make it "executable" so it can run.
- You want to lock a private folder (like your doctoral research or e-book drafts) so other users on the system can't see it.
- You need to fix a "Permission Denied" error when trying to run a program or edit a file.
2. Understanding Permissions (The "How")
Before using the command, you need to understand the three types of users and the three types of permissions.
The Three Users:
- u (User/Owner): You.
- g (Group): A set of users.
- o (Others): Everyone else.
The Three Permissions:
- r (Read): Permission to see the contents.
- w (Write): Permission to edit or delete.
- x (Execute): Permission to run the file as a program.
3. Two Ways to Use chmod
A. The Symbolic Method (Easier to Remember)
Use letters to add (+) or remove (-) permissions.
chmod u+x script.sh(Add eXecute for the User)chmod g-w file.txt(Remove Write for the Group)chmod a+r document.pdf(Add Read for All)
B. The Absolute/Numeric Method (Faster for Professionals)
Each permission is assigned a number:
- 4 = Read
- 2 = Write
- 1 = Execute
- 0 = No permission
You add them together to get a single digit for each user (Owner, Group, Others).
- 7 (4+2+1): Full permissions (Read + Write + Execute)
- 6 (4+2): Read + Write
- 5 (4+1): Read + Execute
4. Practical Examples
A. Making a Script Executable
This is the most common use for developers:
chmod +x my_script.sh
B. Setting Standard Permissions (755)
Common for public folders: Owner can do everything, everyone else can only read and enter the folder.
chmod 755 my_folder
C. Making a File Completely Private (600)
Only the owner can read and write; nobody else can see a thing. Great for SSH keys or passwords.
chmod 600 private_notes.txt
D. Changing an Entire Folder Tree (Recursive)
To change permissions for a folder and everything inside it:
chmod -R 700 my_private_project/
5. Pro-Tips
- View Your Permissions: Use
ls -lto see the current permissions. It looks like-rwxr-xr-x. The first three are for the user, the next three for the group, and the last three for others. - Safety Warning: Never use
chmod 777unless you absolutely have to. It gives everyone permission to do anything to your file, which is a major security risk. - Arch Linux Context: When you download a script from the AUR or GitHub, you often have to
chmod +xit before it will run. Also, your.gnupgand.sshfolders require very specific permissions (usually 700) to function correctly.
6. Summary Reference
| Goal | Numeric | Symbolic |
|---|---|---|
| Make a script runnable | chmod 755 |
chmod +x |
| Only owner can Read/Write | chmod 600 |
chmod u=rw,go= |
| Read-only for everyone | chmod 444 |
chmod a=r |
| Full access for everyone (Risk!) | chmod 777 |
chmod a=rwx |