Linux File Permissions : chmod, chown, and Fixing Issues
Last week, I was working on a small project on my Ubuntu laptop. I set up a web server (Apache) to host a webpage and wrote a script log.sh to save visitor data into data.txt. When I ran the script in the terminal, it worked fine. But when the web server tried to run it, I got “Permission denied.” I was stuck! After some digging, I learned it was a permissions issue, the web server user (www-data) couldn’t access my files. Here’s what I learned and how I fixed it.
What Are Linux Permissions?
Every file in Linux has rules about who can read, write, or run it. There are three types of users:
Owner: The person who made the file.
Group: A group of users.
Others: Everyone else.
And three types of permissions:
Read (r): Can view the file.
Write (w): Can edit the file.
Execute (x): Can run the file (like a script).
Run ls -l to see permissions. Example:
-rwxr-xr-- 1 zasem mygroup 1024 Apr 28 2025 log.sh
rwxr-xr--: Owner hasrwx(read, write, execute), group hasr-x(read, execute), others haver--(read).zasem: Owner (shoutout to my buddy who inspired me to start this journey!).mygroup: Group.
Using chmod to Change Permissions
chmod changes permissions. I use the number method because it’s easier:
Read = 4
Write = 2
Execute = 1
Add them up for each user. So rwx = 7, rw- = 6, r-x = 5, etc. You use three numbers for owner, group, and others.
For my script, I needed it to be executable:
chmod 755 log.sh
7: Owner can do everything (rwx).5: Group can read and execute (r-x).5: Others can read and execute (r-x).
Now the web server could run it!
Using chown to Change Ownership
My script was owned by my user (zasem), but the web server runs as www-data. So, I used chown to change the owner:
sudo chown www-data:www-data log.sh data.txt
This made www-data the owner and group for both files. Now the web server could access them.
Common Permission Problems and Fixes
Here are issues I faced and how I fixed them:
“Permission Denied” When Running a Script
Check withls -l. If there’s nox, add it:chmod +x log.shAlso, make sure the right user owns the file (use
chownif needed).Can’t Write to a File
My web server couldn’t write todata.txt. I added write permission:chmod 664 data.txt6: Owner can read and write.6: Group can read and write.4: Others can read.
Too Much Access
I once didchmod 777(everyone can do everything)big mistake! It’s not safe. Use755for scripts or644for files instead.
How I Fixed My Issue
Here’s what I did to solve my web server problem:
Checked permissions:
ls -llog.shshowed no execute permission.Added execute:
chmod 755log.sh.Changed owner:
sudo chown www-data:www-datalog.shdata.txt.Fixed the log file:
chmod 664 data.txtso the web server could write to it.
It worked! My script now logs visitor data without errors.
What I Learned
This taught me to always check permissions with ls -l first. Use chmod to set the right access and chown to fix ownership. Don’t give too much access, stick to what’s needed. I’m getting more confident with Linux now, and I hope this helps you too! If you’ve had permission issues, share them in the comments.

