أساسيات الصلاحيات
في هذا الدرس 11 موضوعات
هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.
Lesson Overview
Sooner or later you will write a small script, type its name, and see Permission denied. The file is right there. The commands inside are correct. Yet the shell refuses.
The reason is that every file carries a short list of permissions that say who may read it, change it, or run it. The literal assistant in our picture follows those rules strictly: if the file is not marked "may be run", the assistant will not run it, however politely you ask.
This lesson explains the three basic permissions, how to read them in ls -l, how chmod +x fixes the most common failure, and how Windows handles the same idea differently. It is a basic tour. Real systems have more detail (groups, special bits, access lists), and you will deepen it later in the course.
You need the previous lessons: paths (./ means "in this folder"), redirection to create a small script file, and PATH from the last lesson. Commands that change permissions are shown for Bash on Linux and macOS. Windows does not use chmod, and a section explains what to do there. Details differ between systems, so check your own system's documentation if something behaves differently.
Learning Objectives
By the end of this lesson you will be able to:
- Name the three basic permissions: read, write, and execute.
- Name the three classes they apply to: owner, group, and others.
- Read a permission string such as
-rwxr-xr-xfromls -l. - Explain why a script gives "Permission denied" and fix it with
chmod +x. - Explain why you type
./script.shrather thanscript.sh. - Describe how Windows decides what can run, and what an execution policy is.
- Name two other causes of "Permission denied".
Written Explanation
Three permissions
For each file or folder, the system records whether certain people may do three things:
| Permission | Letter | On a file | On a folder |
|---|---|---|---|
| Read | r | See the contents | List what is inside |
| Write | w | Change the contents | Create, rename, or delete items inside |
| Execute | x | Run it as a program or script | Enter it (cd) and reach items inside |
The GNU chmod documentation says that x on a directory means permission to search it. That is the "enter" idea in plain words.
Three classes of people
Permissions are set separately for three classes:
- u: the user, meaning the owner of the file.
- g: the group the file belongs to. A group is a named set of accounts.
- o: others: everyone else.
The letter a means all three together.
Reading ls -l
$ ls -l
-rw-r--r-- 1 ana staff 42 Nov 3 10:15 notes.txt
-rwxr-xr-x 1 ana staff 31 Nov 3 10:20 hello.sh
drwxr-xr-x 3 ana staff 96 Nov 3 10:18 projectsThe exact columns (and the group name) vary by system. Focus on the first column, which has ten characters:
- rw- r-- r--
| | | |
| | | +-- others: read only
| | +------ group: read only
| +---------- owner: read and write
+------------ type: - means a regular file, d means a folderEach class gets three slots in the order r, w, x. A dash means "not allowed". So:
notes.txtis-rw-r--r--. The owner can read and change it, and everyone else can read it. Nobody can execute it, which is right for a text file.hello.shis-rwxr-xr-x. Everyone can read and run it, and only the owner can change it.projectsstarts withd: it is a folder, and it is enterable by all.
Why a script fails
You write a script, then try to run it by path:
$ ./hello.sh
bash: ./hello.sh: Permission deniedThe wording can differ between shells. The file exists, but its permissions have no x (look at the first column: -rw-r--r--). The shell will not start a file that is not marked as executable. A file you create with an editor or with > normally starts without x, so this happens to nearly every beginner.
The fix: chmod
chmod ("change mode") changes permissions. Its symbolic form is: who, then + or - or =, then what.
$ chmod +x hello.sh
$ ls -l hello.sh
-rwxr-xr-x 1 ana staff 31 Nov 3 10:20 hello.sh
$ ./hello.sh
Hello from my scriptchmod +x fileadds execute permission.chmod -x filetakes it away.chmod u+x fileadds it for the owner only. The GNU documentation explains that+adds to existing permissions without removing any, and that leaving out the class letters acts likea, except that bits set in your umask (a system setting for default permissions) are left alone. In practice,chmod +xandchmod u+xare enough for you.chmod u-w filewould make a file read-only for its owner.
You can also write permissions as numbers. For example chmod 755 hello.sh and chmod 644 notes.txt are common. I did not verify the digit rules on a source in this session, so use the letter form until you learn them properly; the numbers add r=4, w=2, and x=1 for each class.
Why ./hello.sh?
If you type just hello.sh, the shell looks for a program with that name in every folder listed in PATH. The current folder is normally not on that list, so it will not be found, even though the file is right there. Writing ./hello.sh gives a path (. is the current folder, Lesson 2), so the shell skips the search. You can also add the folder to PATH (Lesson 5), but be careful about doing that for the current folder.
The first line of a script: the shebang
A script's first line can say which program should interpret it:
#!/bin/bash
echo "Hello from my script"The #! followed by a program path is called a shebang. When you run the file as a program, the system uses that line to pick the interpreter. It only matters on Linux and macOS.
You can run a script with its interpreter named directly, bash hello.sh, and it usually works even without the execute permission, because the interpreter only needs to read the file. That is a workaround, not a replacement for knowing the permission rules.
Windows does it differently
Windows has no chmod and no x bit in the Unix sense. It uses access control lists to decide who can read, change, or run a file, and, as a rule, it decides whether a file is runnable from its extension. Microsoft's documentation notes the PATHEXT variable, which lists the extensions Windows treats as executable, such as .exe.
PowerShell adds one more rule: an execution policy. It controls whether PowerShell will run .ps1 script files. If scripts are blocked, you may see an error saying running scripts is disabled on this system, even though you did nothing wrong. You can look at the current setting with Get-ExecutionPolicy. Microsoft's documentation also mentions that a policy can be set for just the current session with Set-ExecutionPolicy and -Scope Process. Changing system or security settings can matter for your safety, so read Microsoft's documentation for about_Execution_Policies and ask your administrator if this is a work or school computer before changing it.
If you use Git Bash or WSL on Windows, the chmod behavior can depend on where the file is stored. Files in the Linux side of WSL follow the Linux rules, but files on the Windows disk may not honor chmod the same way. Test on your own system.
Other causes of "Permission denied"
The execute bit is the most common cause, but not the only one:
- No write permission. Writing into a read-only file, or deleting from a folder where you lack
w. - Someone else's file. A system file owned by another account, such as the administrator. Administrator tools such as
sudocan override this, but do not use them just to silence an error until you understand what the command will change.
When you see the message, run ls -l on the file and on its folder, and read the permissions before changing anything.
Examples
Example 1: Read a permission string (simple)
-rwxr-x---:
- First character
-: a regular file. - Owner
rwx: can read, write, and run. - Group
r-x: can read and run, not change. - Others
---: no access at all.
Example 2: Fix a script that will not run (practical)
Create the script with redirection from Lesson 4 (single quotes keep the shell from interpreting the !):
$ echo '#!/bin/bash' > hi.sh
$ echo 'echo "script is alive"' >> hi.sh
$ ./hi.sh
bash: ./hi.sh: Permission denied
$ ls -l hi.sh
-rw-r--r-- 1 ana staff 35 Nov 3 10:30 hi.sh
$ chmod +x hi.sh
$ ./hi.sh
script is aliveBefore: no x, so the shell refused. After: x added, and it ran. The contents of the file did not change at all: only its permissions.
Example 3: Windows, same idea, different rule (realistic)
On Windows, you create hi.ps1 containing Write-Output "script is alive" and run .\hi.ps1. You may get:
.\hi.ps1 : File ... cannot be loaded because running scripts is disabled on this system.The message wording varies. There is no chmod fix here. The cause is the execution policy, which you can inspect with Get-ExecutionPolicy. The right next step is to read Microsoft's documentation for your Windows version and ask whoever manages the computer. The principle is the one you learned for Linux: the system has a rule about what may be run, and "Permission denied" style errors mean a rule said no.
Backend Insight
Backend deployments run on servers where permissions matter every day. A deploy script that was never marked executable fails on the server. A service started by a limited account cannot read a configuration file owned by someone else, or cannot write its log file in a folder it does not own. And a secrets file should be readable by the service account and nobody else; leaving a password file readable by everyone is a security mistake.
Permissions are also a safety net: a program running with limited rights cannot damage what it cannot write to. This idea, "give each program only the access it needs", returns later in the course under security.
Common Mistakes
Mistake 1: Typing the script name without ./
hello.sh gives "command not found" because the current folder is not on PATH. Type ./hello.sh.
Mistake 2: Using broad permissions as a fix
Making everything readable, writable, and executable by everyone stops the error but weakens security. Add only what is needed, such as chmod u+x.
Mistake 3: Using chmod on Windows
chmod is a Linux and macOS command. On Windows PowerShell it does not exist as such. Windows rules are different, and the .ps1 problem is usually the execution policy.
Mistake 4: Assuming a script that runs as bash hello.sh is fine
It runs because the interpreter only needs to read the file. It may still fail when started directly. Check ls -l.
Exercises
Try each exercise yourself before opening the solutions.
Level A: Foundation
A1. What do r, w, and x stand for?
أظهر الحل A1
A1. r is read, w is write, and x is execute (or "enter" for a folder).
A2. Name the three classes of people that permissions apply to.
أظهر الحل A2
A2. The owner (user), the group, and others (everyone else).
A3. What does the first character, d or -, of an ls -l line tell you?
أظهر الحل A3
A3. - means a regular file and d means a directory (folder).
Level B: Application
B1. Read -rwxr--r--: state what the owner, the group, and others can do.
أظهر الحل B1
B1. The owner can read, write, and run. The group can only read. Others can only read.
B2. Create a two-line script greet.sh (Bash users) as in Example 2, try to run it, fix it, and run it again. Write each command and the output.
أظهر الحل B2
B2. echo '#!/bin/bash' > greet.sh and echo 'echo hello' >> greet.sh. ./greet.sh gives Permission denied. chmod +x greet.sh. ./greet.sh prints hello. ls -l greet.sh shows x flags after the change. PowerShell users can skip this and use the Windows example instead.
B3. Explain why ./greet.sh works but greet.sh alone says "command not found", even in the same folder.
أظهر الحل B3
B3. The shell looks for a bare name only in the folders listed in PATH, and the current folder is normally not one of them. ./greet.sh gives a path (. is the current folder), so no search is needed.
Level C: Challenge
C1. A script runs with bash deploy.sh but fails with ./deploy.sh. Give the likely cause and fix, and say what you would check first.
أظهر الحل C1
C1. The file is missing its execute permission, so starting it directly is refused, while the interpreter only needs to read it. First check ls -l deploy.sh for an x. Fix: chmod +x deploy.sh. Also check that the first line has a valid shebang.
C2. A teammate on Windows tries to run build.ps1 and gets an error saying scripts are disabled. They ask you to run chmod +x build.ps1. Explain why that is the wrong advice, and what they should do instead.
أظهر الحل C2
C2. chmod is a Unix command, and Windows scripts are governed by the execution policy rather than an execute bit. They should read the error, check the policy with Get-ExecutionPolicy, consult Microsoft's documentation, and ask the administrator if the computer is managed, instead of changing system settings blindly.
Mini Challenge
Make a tiny "permission lab" and describe what you see:
- Create a script file that prints one line.
- Show its permissions with
ls -l(Linux and macOS) before and after you make it executable. - Write, in two sentences, what changed in the permission string and why that mattered.
- If you are on Windows only, instead run
Get-ExecutionPolicy, write down the value, and write one sentence on what you think it controls, without changing it.
Check your work: you changed only the permission you needed, and you can point at the x that appeared.
Summary & Next Lesson
Key takeaways
- Permissions are read, write, and execute, set for the owner, the group, and others.
ls -lshows them: type character, then three triplets ofrwxor dashes.- "Permission denied" when running a script usually means it lacks execute permission. Fix it with
chmod +x file. - Type
./script.shto run a script in the current folder, because the current folder is not normally onPATH. - Windows uses a different system. For PowerShell scripts, the execution policy is a common cause of refusal.
- Add only the permissions you need.
Next lesson
You now know how permissions govern what runs. The next lesson, "Bash vs PowerShell cheat sheet", puts the commands from this whole module side by side in one table.
Self-check
- What does
chmod +x script.shdo? - In
-rw-r--r--, can others change the file? - Why is
./needed to run a script in the current folder? - What is a shebang line?
- On Windows, what often blocks a
.ps1script?
أظهر الإجابات
- It adds execute permission so the file can be run.
- No. Others have read only.
- The current folder is not normally in
PATH, so the shell needs an explicit path. - A first line starting with
#!that names the program that should run the script. - The PowerShell execution policy.
References
- man7.org, chmod(1) manual page. Supports: the classes
u,g,o, anda, the meanings ofr,w, andx(includingxon a directory meaning search), and that+adds bits andchmod u+xmakes a file executable for its owner only. - Microsoft Learn, about_Environment_Variables. Supports:
PATHEXTlists the extensions Windows treats as executable,PSExecutionPolicyPreferenceandSet-ExecutionPolicy -Scope Processexist, and execution policy information applies to the Windows platform only. - man7.org, bash(1) manual page. Supports:
PATHis a colon-separated list of directories that the shell searches for commands. - Not verified on an official page: the
ls -lcolumn layout, the shebang's behavior, octal permission digits, that Windows uses access control lists, the exact error messages,sudo, WSL's handling of permissions, and the detail that the interpreter only needs read access. The text hedges these.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.