CareerByteCode
ByteBlogby CareerByteCode

Linux File Permissions Explained with Real Mistakes

What you build: a script, a team folder, an SSH key and a config file that break with Permission denied, then work with the right chmod, chown and group, on a free VM.

Linux File Permissions Explained with Real Mistakes

✍️ Translate this blog into Hindi, Tamil or Telugu

In this lab you learn Linux file permissions the way most engineers really learn them: by hitting Permission denied and fixing it. On a free Google Cloud VM you make a script that will not run, a team folder one teammate cannot open, an SSH key that SSH refuses to use, and the famous chmod 777 "fix" that opens a config file to everybody. Then you fix each one with the right command, and you know why it works.

Who it is for: beginners who know a few Linux commands (ls, cd, cat) and want to stop guessing with chmod. Time: about 1 hour. Cost: free. An e2-micro VM in us-east1 is in Google Cloud's Always Free tier (one per account), and we delete it at the end.

Every command below was run for real by the CareerByteCode Team on a fresh Google Cloud project, and every output and screenshot is the real result we got. Only dates and times will differ on your machine.

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →

Part A: permissions in plain English

Every file and folder on Linux has an owner (a user), a group, and three sets of permissions: one for the owner, one for the group, one for everybody else ("others").

Permissions in one picture
Who asks (owner, group or others) decides which three letters count; r, w and x decide what that person may do

Each set has three letters:

LetterOn a fileOn a folderNumber
r readread the contentlist the names inside (ls)4
w writechange the contentcreate, rename and delete files inside2
x executerun it as a programenter it (cd) and reach files inside1

Real-life example: an office. The owner is the person whose cabin it is, the group is their team, and others are visitors. r is "may look through the glass", w is "may move things around", x is "may open the door and walk in".

How to read ls -l output such as -rw-r----- 1 alice devteam:

  • The first character is the type: - a file, d a folder (directory), l a link.
  • Then three groups of three: rw- for the owner, r-- for the group, --- for others.
  • Then the owner (alice) and the group (devteam).

Numbers (octal mode): add up r=4, w=2, x=1 for each set. rw- = 6, r-- = 4, --- = 0, so rw-r----- is 640. You will meet 644 (normal file), 755 (program or folder), 600 (private file like a key) and 700 (private folder).

The commands: chmod changes permissions, chown changes the owner (and group), chgrp changes only the group. Only the owner or root (the admin user, reached with sudo) may change them.

Before you start

  1. A Google Cloud account (the free trial works) and a project.
  2. Open Cloud Shell (the >_ icon at the top right of the Google Cloud console) and set your project:
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
gcloud services enable compute.googleapis.com

Part B: the lab

Step 1: a free e2-micro VM

Why: permissions are about several users, so we want a real Linux server where we can create users safely. --no-service-account --no-scopes gives the VM no Google Cloud permissions, because this lab needs none.

gcloud compute instances create perm-lab --zone=us-east1-b --machine-type=e2-micro \
  --image-family=debian-12 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard \
  --no-service-account --no-scopes 2>&1 | tail -2
until gcloud compute ssh perm-lab --zone=us-east1-b --tunnel-through-iap --command="echo ssh ok" 2>/dev/null | grep -q ok; do sleep 10; done; echo "SSH works"
NAME      ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
perm-lab  us-east1-b  e2-micro                   10.142.0.11  EXTERNAL_IP  RUNNING
SSH works

Connect to it. In the console, Compute Engine > VM instances > SSH next to perm-lab opens a terminal in your browser, or from Cloud Shell:

gcloud compute ssh perm-lab --zone=us-east1-b

Everything from Step 2 runs inside the VM, in a folder ~/lab263 (mkdir -p ~/lab263 && cd ~/lab263). In the outputs, you stands for your own user name.

Step 2: two practice users and a team group

Why: to see permissions work you need more than one person. groupadd makes a group, useradd -m makes a user with a home folder, and -G devteam puts alice in the group. bob is not in it (yet).

# Make 2 practice users and a team group (sudo = run as the admin root user)
sudo groupadd devteam
sudo useradd -m -s /bin/bash -G devteam alice
sudo useradd -m -s /bin/bash bob
id alice
id bob
whoami
uid=1001(alice) gid=1005(alice) groups=1005(alice),1004(devteam)
uid=1002(bob) gid=1006(bob) groups=1006(bob)
you

id shows each user's number (uid), main group (gid) and every group they belong to. alice is in devteam, bob is not.

Step 3: read permissions

Why: before changing anything, learn to read what is there. stat -c "%A %a %U %G %n" prints the letters, the number, the owner, the group and the name side by side, which is the easiest way to learn the numbers.

# Read permissions: make a file and a folder, then look at them
echo "hello" > notes.txt
mkdir reports
ls -l
stat -c "%A %a %U %G %n" notes.txt reports
total 8
-rw-r--r-- 1 you you    6 Nov 18 20:09 notes.txt
drwxr-xr-x 2 you you 4096 Nov 18 20:09 reports
-rw-r--r-- 644 you you notes.txt
drwxr-xr-x 755 you you reports

A new file got 644 (rw-r--r--) and a new folder 755 (rwxr-xr-x). Why those? That comes from your umask, which you meet in Step 7.

Step 4: change permissions with chmod

Why: there are two ways to say the same thing. Letters (symbolic): u user (owner), g group, o others, with = set, + add, - remove. Numbers (octal): all three sets at once.

# Change permissions two ways: letters (symbolic) and numbers (octal)
chmod u=rw,g=r,o= notes.txt
ls -l notes.txt
chmod 640 notes.txt
stat -c "%A %a %n" notes.txt
chmod o+r notes.txt
stat -c "%A %a %n" notes.txt
chmod 755 reports
stat -c "%A %a %n" reports
-rw-r----- 1 you you 6 Nov 18 20:09 notes.txt
-rw-r----- 640 notes.txt
-rw-r--r-- 644 notes.txt
drwxr-xr-x 755 reports
chmod two ways
Steps 2 to 4: two users, the permissions read with ls and stat, then changed with letters and numbers

u=rw,g=r,o= and 640 give the same result. o+r added read for others (back to 644) without touching the rest. Use letters to add or remove one thing and numbers to set everything at once.

Break it: a script that will not run

You write a small backup script and try to run it.

# Break it: a script without the x (execute) permission
printf '#!/bin/bash\necho "backup done at $(date +%%H:%%M)"\n' > backup.sh
ls -l backup.sh
./backup.sh
-rw-r--r-- 1 you you 49 Nov 18 20:09 backup.sh
bash: ./backup.sh: Permission denied

The file is rw-r--r--: nobody has x. Linux refuses to run it, even for the owner, and the exit code is 126 ("found it, but cannot run it").

Fix it: give the owner execute permission

# Fix it: give the owner execute permission
chmod u+x backup.sh
ls -l backup.sh
./backup.sh
-rwxr--r-- 1 you you 49 Nov 18 20:09 backup.sh
backup done at 20:09
Script permission denied and fixed
Permission denied (exit 126) because the script has no x; chmod u+x and it runs

chmod u+x adds execute for the owner only: rwxr--r--. Many people type chmod +x, which adds x for everyone. That is fine for a normal script, but adding only what is needed is the better habit.

Break it: a team folder bob cannot open

alice makes the shared folder /srv/team, owned by her, with mode 700 (only the owner may do anything). bob tries to read the plan.

# Break it: a shared team folder that bob cannot use
sudo mkdir -p /srv/team
sudo chown alice:alice /srv/team
sudo chmod 700 /srv/team
ls -ld /srv/team
sudo -u alice bash -c 'echo "plan v1" > /srv/team/plan.txt' && echo "alice wrote plan.txt"
sudo -u bob cat /srv/team/plan.txt
drwx------ 2 alice alice 4096 Nov 18 20:10 /srv/team
alice wrote plan.txt
cat: /srv/team/plan.txt: Permission denied

bob is "others" for this folder, and others have ---. He cannot even enter it.

Fix it: a group-owned folder with setgid

Why: the clean answer is not "give everyone access". It is:

  1. Put bob in the team group: usermod -aG devteam bob (-a means add; without it, bob would lose his other groups).
  2. Give the folder to the group: chgrp devteam /srv/team.
  3. Mode 2770: rwx for alice and the group, nothing for others. The leading 2 is the setgid bit: new files inside the folder get the folder's group (devteam) instead of the creator's own group, so the whole team can use them.
# Fix it: group devteam owns the folder, group can write, setgid keeps the group on new files
sudo usermod -aG devteam bob
sudo chgrp devteam /srv/team
sudo chmod 2770 /srv/team
sudo chgrp devteam /srv/team/plan.txt
sudo chmod 660 /srv/team/plan.txt
ls -ld /srv/team
sudo -u bob bash -c 'cat /srv/team/plan.txt; echo "bob was here" >> /srv/team/plan.txt; touch /srv/team/bob-notes.txt'
sudo ls -l /srv/team
drwxrws--- 2 alice devteam 4096 Nov 18 20:10 /srv/team
plan v1
total 4
-rw-r--r-- 1 bob   devteam  0 Nov 18 20:10 bob-notes.txt
-rw-rw---- 1 alice devteam 21 Nov 18 20:10 plan.txt
Team folder fixed
bob is denied, then joins devteam; the folder is drwxrws--- and his new file belongs to devteam automatically

Look at the details:

  • The folder shows drwxrws---. The s where the group's x would be is the setgid bit.
  • bob read the plan and added a line (plan.txt grew to 21 bytes).
  • His new file bob-notes.txt belongs to the group devteam, not to his own group bob. That is setgid at work.
  • Our own user had to use sudo ls: we are not in devteam, so for this folder we are "others" too. Permissions apply to everyone, including you.

Break it: an SSH key that SSH refuses

A very common mistake: you copy a private key to a server and its permissions end up as 644, readable by every user. Here we make a key, allow it to log in, set it to 644 and try.

# Break it: an SSH private key that everybody can read (a very common mistake)
ssh-keygen -q -t ed25519 -N "" -f lab_key
cat lab_key.pub >> ~/.ssh/authorized_keys
chmod 644 lab_key
ls -l lab_key
ssh -i lab_key -o BatchMode=yes -o StrictHostKeyChecking=no localhost "echo logged in" 2>&1 | grep -v "^Warning: Permanently"
-rw-r--r-- 1 you you 419 Nov 18 20:10 lab_key
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'lab_key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "lab_key": bad permissions
you@localhost: Permission denied (publickey).
🎓⭐ Recommended by CareerByteCode · Live trainingCloud & DevOps Live Cohort Internshipโ‚น2,360See the training →

SSH protects you from yourself: a private key that others can read is treated as stolen, so SSH ignores it (bad permissions) and the login fails.

Fix it: chmod 600

# Fix it: a private key must be readable by its owner only (600)
chmod 600 lab_key
ls -l lab_key
ssh -i lab_key -o BatchMode=yes -o StrictHostKeyChecking=no localhost "echo logged in with the key as \$(whoami)" 2>&1 | grep -v "^Warning: Permanently"
-rw------- 1 you you 419 Nov 18 20:10 lab_key
logged in with the key as you
SSH key refused and fixed
UNPROTECTED PRIVATE KEY FILE with 0644; with 600 the same key logs in

600 = rw-------: only the owner may read it. The same rule applies to ~/.ssh (700) and ~/.ssh/authorized_keys (600).

Break it: the chmod 777 "fix"

When something says Permission denied, it is tempting to run chmod 777 and move on. 777 means everyone may read, change and run the file. Watch what that does to a config file.

# The chmod 777 "fix" people try, and why it is wrong: now ANY user can change the file
echo "DB_HOST=10.0.0.5" > app.conf
chmod 777 app.conf
ls -l app.conf
sudo -u bob bash -c "echo 'DB_HOST=evil.example.com' >> $PWD/app.conf" && echo "bob changed app.conf"
cat app.conf
sed -i '/evil/d' app.conf; chmod 640 app.conf
sudo -u bob bash -c "echo 'DB_HOST=evil.example.com' >> $PWD/app.conf"
stat -c "%A %a %n" app.conf; cat app.conf
-rwxrwxrwx 1 you you 17 Nov 18 20:11 app.conf
bob changed app.conf
DB_HOST=10.0.0.5
DB_HOST=evil.example.com
bash: line 1: /home/you/lab263/app.conf: Permission denied
-rw-r----- 640 app.conf
DB_HOST=10.0.0.5
chmod 777 mistake
With 777 bob changed the database host in app.conf; with 640 the same change is refused

With 777, bob, who has nothing to do with this app, changed the database address. With 640 (rw-r-----) the owner may write, the group may read, others get nothing, and bob's change is refused. 777 hides the real problem and opens a new one. The fix is always: work out who needs what, then give exactly that, as we did for the team folder.

Step 5: umask and the sticky bit

Why: umask decides the permissions of new files. 0022 removes w for group and others, so files start at 644 and folders at 755. The sticky bit (the t at the end of drwxrwxrwt on /tmp) means that in a folder everyone can write to, only a file's owner may delete it.

# umask: the default permissions of new files; sticky bit: why /tmp is safe to share
umask
touch default-file.txt; mkdir default-dir
stat -c "%A %a %n" default-file.txt default-dir
ls -ld /tmp
sudo -u alice bash -c 'echo "alice temp" > /tmp/alice.txt; chmod 666 /tmp/alice.txt'
sudo -u bob rm /tmp/alice.txt
ls -l /tmp/alice.txt
0022
-rw-r--r-- 644 default-file.txt
drwxr-xr-x 755 default-dir
drwxrwxrwt 10 root root 4096 Nov 18 20:09 /tmp
rm: cannot remove '/tmp/alice.txt': Operation not permitted
-rw-rw-rw- 1 alice alice 11 Nov 18 20:11 /tmp/alice.txt

alice made /tmp/alice.txt and even set it to 666 (everyone may write). bob still could not delete it, because of the sticky bit on /tmp.

Step 6: an audit, and namei

Why: two handy checks. find -perm -o+w finds files that others may write (there should be none in your app folders). namei -l shows the permissions of every folder on the path to a file, which is how you find which folder blocks access.

# Quiz check: find files anyone can write, then a whole-folder audit
find ~/lab263 /srv/team -perm -o+w -type f 2>/dev/null | sed "s|$HOME|~|" ; echo "world-writable files: $(find ~/lab263 /srv/team -perm -o+w -type f 2>/dev/null | wc -l)"
namei -l /srv/team/plan.txt
world-writable files: 0
f: /srv/team/plan.txt
drwxr-xr-x root  root    /
drwxr-xr-x root  root    srv
drwxrws--- alice devteam team
                         plan.txt - Permission denied

0 world-writable files: good. And namei did exactly its job: as our own user (not in devteam), it walked /, srv and team, and stopped at team with Permission denied. So the folder that blocks you is team (drwxrws---, others get nothing), not the file. On a real server this is the fastest way to find which folder on a long path is missing an x.

Audit
umask and the sticky bit, then an audit: no world-writable files, and namei shows every folder on the path

POC: prove it works

  • A script with no x failed with exit 126 and ran after chmod u+x.
  • bob was denied a 700 folder, then used it after joining devteam, and his new file got the team group through setgid (2770).
  • SSH refused a 644 private key and accepted the same key at 600.
  • 777 let an unrelated user change a config file; 640 refused the same change.
  • The sticky bit on /tmp stopped bob from deleting alice's file, even with 666 on the file.
  • The audit found 0 world-writable files.

Quiz task: check what you learned

  1. What number is rwxr-x---? (Answer: 750.)
  2. A web server user must read index.html, but nobody may change it except you. Owner you, group www-data. Which mode? *(Answer: 640, or 644 if every user may read it.)*
  3. cd /srv/app gives Permission denied but ls -ld /srv/app shows drw-rw-rw-. What is missing? *(Answer: x on the folder; x is what lets you enter it.)*
  4. Add carol to devteam without removing her other groups. *(Answer: sudo usermod -aG devteam carol.)*
  5. Which command shows the permissions of every folder on the way to a file? *(Answer: namei -l /path/to/file.)*

Clean up (delete everything)

Why: the e2-micro is free for one VM per account, and its disk counts too. Exit the VM (exit), then in Cloud Shell:

gcloud compute instances delete perm-lab --zone=us-east1-b --quiet 2>&1 | tail -1
echo "perm-lab VMs left: $(gcloud compute instances list --filter="name=perm-lab" --format="value(name)" | wc -l)"
echo "perm-lab disks left: $(gcloud compute disks list --filter="name~^perm-lab" --format="value(name)" | wc -l)"
Deleted [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-east1-b/instances/perm-lab].
perm-lab VMs left: 0
perm-lab disks left: 0
Clean up
The perm-lab VM and its disk are deleted: 0 left

FAQ

Why can root do everything? root (user id 0) skips the permission checks. That is why you use sudo only when you must, and why services should run as their own user, not as root.

**What is the difference between chown and chgrp?** chown alice file changes the owner, chown alice:devteam file changes owner and group, and chgrp devteam file changes only the group.

**When do I use -R?** chmod -R and chown -R change a folder and everything inside it. Be careful: chmod -R 644 on a folder removes x from every sub-folder, and then nobody can enter them. For mixed trees use chmod -R u=rwX,g=rX,o= (capital X adds execute only to folders and to files that already have it).

What are ACLs? Access Control Lists (setfacl, getfacl) give extra users or groups their own permissions on one file, beyond owner, group and others. Use them when the three sets are not enough.

Do permissions matter in containers and Kubernetes? Yes. A container that runs as a non-root user (the safe default) still needs the right owner and mode on its files, and Kubernetes fsGroup sets the group of mounted volumes much like setgid does here.

**Is chmod 777 ever OK?** On a throwaway test folder, maybe. On a server, never: it lets any user or any hacked program change the file.

Troubleshooting

You seeWhyFix
Permission denied running ./script.sh (exit 126)No x on the filechmod u+x script.sh
Permission denied on cd folderNo x on the folder for youCheck ls -ld folder, fix group or mode
UNPROTECTED PRIVATE KEY FILEThe key is readable by otherschmod 600 key
Operation not permitted on chmod or chownYou are not the ownerUse sudo, or ask the owner
A user just added to a group still gets deniedGroup changes apply at the next loginLog out and in again, or newgrp devteam
Teammates cannot edit each other's files in a shared folderFiles got each person's own groupchgrp the folder, chmod 2770 (setgid), fix old files once

What next

Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.

Read next →Linux Users, Groups and sudo Explained⏱ 13 min

⭐ Keep learning with CareerByteCode

Hand-picked to go deeper on this topic
News, guides and updates from the CareerByteCode team
🏅 Expert writer

The CareerByteCode team writes about what is new on careerbytecode.in: courses, labs, exams, events and the community, which is free to join for influencers and content creators in tech.

Get CareerByteCode Team's next blog by email

One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.

We email you once to confirm. By subscribing you agree to the privacy policy.

💬 Comments (0)

Top · Newest

Sign in to comment

Related blogs

🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →