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.

✍️ 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").

Each set has three letters:
| Letter | On a file | On a folder | Number |
|---|---|---|---|
r read | read the content | list the names inside (ls) | 4 |
w write | change the content | create, rename and delete files inside | 2 |
x execute | run it as a program | enter it (cd) and reach files inside | 1 |
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,da folder (directory),la 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
- A Google Cloud account (the free trial works) and a project.
- 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.comPart 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 worksConnect 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-bEverything 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
whoamiuid=1001(alice) gid=1005(alice) groups=1005(alice),1004(devteam)
uid=1002(bob) gid=1006(bob) groups=1006(bob)
youid 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 reportstotal 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 reportsA 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
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 deniedThe 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
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.txtdrwx------ 2 alice alice 4096 Nov 18 20:10 /srv/team
alice wrote plan.txt
cat: /srv/team/plan.txt: Permission deniedbob 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:
- Put bob in the team group:
usermod -aG devteam bob(-ameans add; without it, bob would lose his other groups). - Give the folder to the group:
chgrp devteam /srv/team. - Mode
2770:rwxfor 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/teamdrwxrws--- 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
Look at the details:
- The folder shows
drwxrws---. Theswhere the group'sxwould be is the setgid bit. - bob read the plan and added a line (plan.txt grew to 21 bytes).
- His new file
bob-notes.txtbelongs to the group devteam, not to his own groupbob. That is setgid at work. - Our own user had to use
sudo ls: we are not indevteam, 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
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
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.txt0022
-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.txtalice 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.txtworld-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 denied0 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.

POC: prove it works
- A script with no
xfailed with exit 126 and ran afterchmod u+x. - bob was denied a
700folder, then used it after joiningdevteam, and his new file got the team group through setgid (2770). - SSH refused a
644private key and accepted the same key at600. 777let an unrelated user change a config file;640refused the same change.- The sticky bit on
/tmpstopped bob from deleting alice's file, even with666on the file. - The audit found 0 world-writable files.
Quiz task: check what you learned
- What number is
rwxr-x---? (Answer: 750.) - A web server user must read
index.html, but nobody may change it except you. Owner you, groupwww-data. Which mode? *(Answer:640, or644if every user may read it.)* cd /srv/appgivesPermission deniedbutls -ld /srv/appshowsdrw-rw-rw-. What is missing? *(Answer:xon the folder;xis what lets you enter it.)*- Add carol to
devteamwithout removing her other groups. *(Answer:sudo usermod -aG devteam carol.)* - 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
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 see | Why | Fix |
|---|---|---|
Permission denied running ./script.sh (exit 126) | No x on the file | chmod u+x script.sh |
Permission denied on cd folder | No x on the folder for you | Check ls -ld folder, fix group or mode |
UNPROTECTED PRIVATE KEY FILE | The key is readable by others | chmod 600 key |
Operation not permitted on chmod or chown | You are not the owner | Use sudo, or ask the owner |
| A user just added to a group still gets denied | Group changes apply at the next login | Log out and in again, or newgrp devteam |
| Teammates cannot edit each other's files in a shared folder | Files got each person's own group | chgrp the folder, chmod 2770 (setgid), fix old files once |
What next
- New to the Linux terminal? Start with Linux for Cloud Beginners: 50 Commands on a Free VM.
- Write scripts that use what you learned: Bash Scripting for DevOps: Automate Boring Tasks.
- Permissions in the cloud work on the same idea: Google Cloud IAM for Beginners: Roles, Members and Least Privilege.
- Keep secrets out of files: Secret Manager: Store Passwords and API Keys Safely.
- More hands-on posts: ByteBlog. Practice in ByteLabs and see the courses.
Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.
⭐ Keep learning with CareerByteCode
Hand-picked to go deeper on this topicGet 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.
Related blogs
CareerByteCode TeamLinux Users, Groups and sudo Explained
What you build: a shared Linux server for 3 people on a free VM: users, a developers group, a sudo rule that allows one job only, visudo catching a typo, the sudo log, and an account locked and remove
#beginners
CareerByteCode TeamCron Jobs and Log Rotation on Linux
What you build: a disk report job run by cron every minute with its log rotated by logrotate, on a free VM, plus 3 real mistakes: relative paths, the % sign and an insecure log folder.
#beginners
CareerByteCode TeamFrom Windows to Linux: A Gentle Start for Beginners
What you build: everyday Windows tasks (files, search, zip, Task Manager, services, apps, network, admin) done the Linux way on a free VM, plus the 3 classic surprises and their fixes.
#beginners
CareerByteCode TeamLinux for Cloud Beginners: 50 Commands on a Free VM
What you build: a free e2-micro Linux VM where you practise 50 everyday commands for files, text, permissions, processes, network and packages, in your own Google Cloud account. Every step tested.
💬 Comments (0)
Top · NewestSign in to comment