Linux file permissions: read ls -l, fix "Permission denied", and never chmod 777
AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (
AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (GNU coreutils 9.7); all outputs below are copied from that run, not written by hand.
Learn to read the permission string in ls -l, translate it to numbers like 644 and 755, and fix three real "Permission denied" situations: a script that won't run, a secret file other users can read, and a private folder. Then see live why chmod 777 is a bad fix, and how to find world-writable files.
The problem: Permission denied. Every Linux user sees it sooner or later, and the tempting fix is chmod 777. Please don't.
Reading ls -l
Here is a small website folder. It belongs to the user alice and the group web. There's also a second user, bob, who should not be able to touch alice's secrets.
$ ls -l
total 16
-rw-r--r-- 1 alice web 24 Nov 21 19:15 deploy.sh
-rw-r--r-- 1 alice web 6 Nov 21 19:15 index.html
drwxr-xr-x 2 alice web 4096 Nov 21 19:15 private
-rw-r--r-- 1 alice web 17 Nov 21 19:15 secret.env
Look at the first column. The first character is the type: a dash for a file, d for a directory. Then come three groups of three letters: the owner, the group, and everyone else. In each group, r is read, w is write, and x is execute. A dash means the permission is missing. So index dot html is: owner can read and write, the group can read, everyone else can read.
From letters to numbers
The numbers are just a shorter way to write the same thing. Read is four, write is two, execute is one. Add them up for each group.
$ stat -c '%A %a %U:%G %n' *
-rw-r--r-- 644 alice:web deploy.sh
-rw-r--r-- 644 alice:web index.html
drwxr-xr-x 755 alice:web private
-rw-r--r-- 644 alice:web secret.env
Read plus write is six, read alone is four, so the file is six four four. The folder has read, write and execute for the owner, which is seven, then five and five: seven five five. I'm root in this container, and root ignores most permissions. So to test what normal users can do, I'll run commands as alice or bob with su -c.
Problem 1: the script won't run
Alice tries to run her deploy script.
$ su alice -c ./deploy.sh
sh: 1: ./deploy.sh: Permission denied
Permission denied, even though she owns it. The file simply has no execute bit. chmod u+x adds execute for the user who owns the file, and nobody else.
$ chmod u+x deploy.sh && su alice -c ./deploy.sh
deployed
You can combine several changes in one go. Here: remove write from group and others, and add execute for the group.
$ chmod go-w,g+x deploy.sh && stat -c '%A %a %n' deploy.sh
-rwxr-xr-- 754 deploy.sh
Problem 2: a secret anyone can read
Now the opposite problem. Can bob read alice's password file?
$ su bob -c 'cat secret.env'
DB_PASSWORD=demo
He can. Six four four means everyone may read it. For secrets you want six hundred: only the owner can read and write.
$ chmod 600 secret.env && su bob -c 'cat secret.env'
cat: secret.env: Permission denied
Permission denied, this time exactly what we wanted.
Problem 3: a private folder
For a folder, read means listing the names, and execute means being allowed to enter it. Let's lock bob out of the private folder, but keep access for alice's group.
$ chmod 750 private && su bob -c 'ls private'
ls: cannot open directory 'private': Permission denied
$ ls -ld private
drwxr-x--- 2 alice web 4096 Nov 21 19:15 private
Seven five zero: alice has full access, the web group can enter and list, and everyone else gets nothing.
Why chmod 777 is a bad fix
So why not chmod 777, and be done with it? Let's see what it really means. This is dangerous on a real server, we only do it in a throwaway container.
$ chmod 777 index.html && su bob -c 'echo defaced > index.html' && cat index.html
defaced
Bob, who has nothing to do with this site, just replaced the home page. The last seven means everyone on the system can write the file. Any compromised service or user can do the same. If you suspect someone already did this, find can list world-writable files.
$ find . -perm -o=w -type f
./index.html
Fix it back to six four four, and bob is locked out again.
$ chmod 644 index.html && su bob -c 'echo again > index.html'
sh: 1: cannot create index.html: Permission denied
Changing the owner with chown
Sometimes the permissions are fine, but the owner is wrong. chown changes the user and the group in one command, written as user, colon, group.
$ chown bob:web index.html && ls -l index.html
-rw-r--r-- 1 bob web 8 Nov 21 19:15 index.html
Changing owners usually needs root, which is why on a normal system you'd put sudo in front of it.
The trap, in one paragraph
Which mode lets only the owner read and write a password file? Six hundred: read and write for the owner, nothing for the group or anyone else.
Quick check
1. Alice owns deploy.sh, but running it says Permission denied. Is the fix chmod 777 or chmod u+x?
- chmod 777 deploy.sh
- chmod u+x deploy.sh
- chown alice deploy.sh
chmod u+x deploy.sh: Only the execute bit was missing. Seven seven seven would let every user on the system change the script.
2. Which mode lets only the owner read and write a password file?
- 644
- 600
- 755
600: Six hundred: read and write for the owner, nothing for the group or anyone else.
Recap
Read the permissions as owner, group, others. Read is four, write is two, execute is one. Six four four for normal files, seven five five for scripts and folders, six hundred for secrets. Use u+x style changes when you only need to fix one bit.
Source and reproducible setup
- Lesson page with the same quiz (and a video version): https://shellprovenai.github.io/lekcje/chmod-permissions/
- Commands, demo setup script and full test log: https://github.com/shellprovenai/terminal-tutorials/tree/main/chmod-permissions
This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.