The “Permission Denied” Error
Every beginner to Linux eventually hits the exact same wall. You spend an hour writing a brilliant bash script to automate a backup task. You save the file as backup.sh. You proudly type ./backup.sh into your terminal to run it, and Linux instantly rejects you with a cold, red error message:
bash: ./backup.sh: Permission denied
You own the server, you wrote the file, and you are logged in as the administrator. Why are you denied permission? Because Linux is fundamentally paranoid. By default, it assumes every new file is just a dumb text document. For security reasons, it strictly forbids any new file from executing as a program unless you explicitly grant it permission to do so.
To grant that permission, you must use the chmod (Change Mode) command. It is the gatekeeper of the Linux file system, controlling exactly who is allowed to read, write, or execute every single file and folder on the server.
The Three Permissions
Linux breaks file security down into three basic permissions:
- Read (r): Can you look at the contents of the file?
- Write (w): Can you edit or delete the file?
- Execute (x): Can you run the file as a program/script?
The Three Categories of Users
Linux then applies those three permissions to three distinct groups of people:
- User (u): The specific person who owns the file (usually you).
- Group (g): A specific team of users (e.g., the ‘developers’ group).
- Others (o): Every single other person on the server (the public).
Method 1: The Symbolic Method (The Easy Way)
The most intuitive way to use chmod is to use letters and plus/minus signs. You specify the user category (u, g, o), add or subtract ( +, – ), and specify the permission (r, w, x).
Making a Script Executable
To fix the “Permission denied” error on your backup.sh script, you want to grant the User (u) the permission to eXecute (x) it.
The syntax is: chmod u+x filename
chmod u+x backup.sh
If you run ./backup.sh now, the script will execute perfectly.
Removing Permissions
If you have a highly sensitive file containing passwords, you want to ensure that “Others” (the public) cannot read it. You want to subtract (-) the Read (r) permission from Others (o).
chmod o-r passwords.txt
Method 2: The Numeric Method (The Fast Way)
While the symbolic method is easy to read, professional system administrators never use it. It is too slow. They use a three-digit numeric code.
In this system, every permission is assigned a mathematical value:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
To assign permissions, you simply add the numbers together. If you want a user to have Read and Write, but not Execute (4 + 2 = 6). If you want them to have everything (4 + 2 + 1 = 7). If you want them to have nothing (0).
You write three numbers in a row: the first number is for the User, the second for the Group, and the third for Others.
The Most Common Numeric Codes
chmod 755 (The Standard Executable)
chmod 755 myscript.sh
- 7 (User): Read, Write, and Execute (4+2+1). You have full control.
- 5 (Group): Read and Execute (4+1). They can run the script, but cannot edit the code.
- 5 (Others): Read and Execute (4+1).
chmod 644 (The Standard Text File)
chmod 644 config.txt
- 6 (User): Read and Write (4+2). You can edit the text.
- 4 (Group): Read only (4). They can look, but not touch.
- 4 (Others): Read only (4).
chmod 600 (The Ultra-Secure File)
chmod 600 secret_key.pem
- 6 (User): Read and Write (4+2).
- 0 (Group): Nothing. Access denied.
- 0 (Others): Nothing. Access denied.
(Note: If you are setting up SSH keys, Linux will actually refuse to use the key unless you have specifically locked it down with chmod 600).
Conclusion
Understanding chmod is not optional if you want to work with Linux. Whether you are using the beginner-friendly symbolic method (+x) or the rapid-fire numeric codes (755), mastering file permissions ensures your scripts run correctly and your sensitive data remains locked down.