You create a script, try to run it, and Linux replies:

Permission denied

Using 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 -l

A file entry might look like this:

-rw-r----- 1 learner learners 24 Nov 17 09:00 notes.txt

Your username, group, date, and file size will differ.

The first character identifies the entry type:

  • - means a regular file.

  • d means a directory.

The next nine characters form three groups:

rw-  r--  ---

They represent permissions for:

  1. The owner.

  2. Members of the file’s group.

  3. 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

r — read

Read the contents

List entry names

w — write

Modify the contents

Create or remove entries, normally with search permission too

x — execute/search

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-lab

If that folder already exists, choose another name.

Create a practice file:

printf 'Practice notes\n' > notes.txt
chmod 600 notes.txt
ls -l notes.txt

The 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:

  • u means the owner.

  • g means the group.

  • o means others.

  • a means 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.txt

The permission string should now begin:

-rw-r-----

Remove that access:

chmod g-r notes.txt
ls -l notes.txt

The 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

4

Write

2

Execute

1

Add the values you want for each class.

For example:

  • 7 means read, write, and execute.

  • 6 means read and write.

  • 5 means read and execute.

  • 4 means read only.

  • 0 means none.

In a basic three-digit mode, the digits represent owner, group, and others.

Run:

chmod 640 notes.txt

This 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.sh

Try running it:

./hello.sh

In an ordinary Linux home directory, this should fail because the owner lacks execute permission.

Add that permission:

chmod u+x hello.sh
./hello.sh

Expected output:

Hello from ReconHack Labs!

The ./ prefix tells the shell to run the file in your current directory.

Running this instead:

sh hello.sh

is 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:

id

to 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:

pwd

Inspect the file:

ls -l hello.sh

Inspect 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.

Further reading