Linux File Permissions Explained: Understanding chmod and “Permission Denied”
Learn to read Linux file permissions, use symbolic and numeric chmod modes, and troubleshoot permission errors through a simple practice exercise.
You create a script, try to run it, and Linux replies:
Permission deniedUsing sudo may seem like the quickest fix. Before doing that, work out which permission is missing and who needs access.
This guide explains Linux’s basic file permissions and walks through a small practice exercise using chmod.
Before you start
Use a Linux terminal, an Ubuntu virtual machine, or Ubuntu through Windows Subsystem for Linux.
Work inside your Linux home directory. Windows-mounted drives, such as paths under /mnt/c, can handle permissions differently.
You can complete these exercises as your normal user. Administrator privileges are not needed.
Read a permission string
Run:
ls -lA file entry might look like this:
-rw-r----- 1 learner learners 24 Nov 17 09:00 notes.txtYour username, group, date, and file size will differ.
The first character identifies the entry type:
-means a regular file.dmeans a directory.
The next nine characters form three groups:
rw- r-- ---They represent permissions for:
The owner.
Members of the file’s group.
Other users.
In this example, the owner can read and write, the group can read, and other users have none of these permissions.
A dash means that the permission is absent.
Linux normally selects the applicable permission class according to the process’s user and group identities. It does not simply combine all three classes. Additional controls, such as access-control lists, can also affect access.
What do r, w, and x mean?
Permission | Regular file | Directory |
|---|---|---|
| Read the contents | List entry names |
| Modify the contents | Create or remove entries, normally with search permission too |
| Attempt to execute the file | Traverse the directory and access named entries |
Directory permissions deserve special attention.
Deleting a file depends mainly on permissions on its parent directory, with additional rules sometimes applying. Making a file read-only does not guarantee that it cannot be deleted.
Create a practice folder
Run:
cd ~
mkdir reconhack-permissions-lab
cd reconhack-permissions-labIf that folder already exists, choose another name.
Create a practice file:
printf 'Practice notes\n' > notes.txt
chmod 600 notes.txt
ls -l notes.txtThe printf command replaces an existing file named notes.txt in the current directory, so use a separate practice folder.
The permission string should begin:
-rw-------The owner can read and write. The group and other users have no permissions.
Make targeted changes with symbolic modes
Symbolic modes use letters to identify the permission class:
umeans the owner.gmeans the group.omeans others.ameans all three classes.
The operators describe the change:
+adds permission.-removes permission.=sets the specified permissions for that class.
Add group read access:
chmod g+r notes.txt
ls -l notes.txtThe permission string should now begin:
-rw-r-----Remove that access:
chmod g-r notes.txt
ls -l notes.txtThe file returns to:
-rw-------These commands change group read permission while preserving the other permission bits.
Understand numeric modes
Numeric modes use these values:
Permission | Value |
|---|---|
Read |
|
Write |
|
Execute |
|
Add the values you want for each class.
For example:
7means read, write, and execute.6means read and write.5means read and execute.4means read only.0means none.
In a basic three-digit mode, the digits represent owner, group, and others.
Run:
chmod 640 notes.txtThis produces:
-rw-r-----The owner gets read and write access, the group gets read access, and others get no access.
The right mode depends on the file’s purpose. A file intended to be readable by other local users might use 644. Private notes might use 600.
Make a small script executable
Create a script:
printf '#!/bin/sh\nprintf "Hello from ReconHack Labs!\\n"\n' > hello.sh
chmod 600 hello.shTry running it:
./hello.shIn an ordinary Linux home directory, this should fail because the owner lacks execute permission.
Add that permission:
chmod u+x hello.sh
./hello.shExpected output:
Hello from ReconHack Labs!The ./ prefix tells the shell to run the file in your current directory.
Running this instead:
sh hello.shis different. It asks the shell to read the script, so it does not test the script file’s execute permission in the same way.
Why chmod 777 is a poor default fix
For ordinary permission bits, 777 grants read, write, and execute access to the owner, group, and others.
That gives other local users more access than most files need. Applying it recursively can spread unnecessary permissions across an entire project.
Before changing permissions, ask:
What operation failed?
Which user is performing it?
Who owns the file?
What permissions does the parent directory have?
What is the smallest change that would provide the required access?
Use:
idto inspect your user and group memberships.
Remember that chmod changes permissions. Ownership changes use chown and should have a specific reason.
If “Permission denied” remains
Check your location:
pwdInspect the file:
ls -l hello.shInspect the current directory itself:
ls -ld .Confirm that you own your practice files.
If the basic permissions appear correct, other causes may include a read-only filesystem, a noexec mount, access-control lists, or security policies.
Do not assume that every permission error requires administrator access.
Try it yourself
Set notes.txt so that only its owner can read and write it.
Then inspect the result and explain every character in its permission string.
The goal is to understand and justify each change you make.