# 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`](http://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:

```plaintext
-rwxr-xr--  1 zasem mygroup  1024 Apr 28 2025 log.sh
```

* `rwxr-xr--`: Owner has `rwx` (read, write, execute), group has `r-x` (read, execute), others have `r--` (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:

```bash
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:

```bash
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:

1. **“Permission Denied” When Running a Script**  
    Check with `ls -l`. If there’s no `x`, add it:
    
    ```bash
    chmod +x log.sh
    ```
    
    Also, make sure the right user owns the file (use `chown` if needed).
    
2. **Can’t Write to a File**  
    My web server couldn’t write to `data.txt`. I added write permission:
    
    ```bash
    chmod 664 data.txt
    ```
    
    * `6`: Owner can read and write.
        
    * `6`: Group can read and write.
        
    * `4`: Others can read.
        
3. **Too Much Access**  
    I once did `chmod 777` (everyone can do everything)big mistake! It’s not safe. Use `755` for scripts or `644` for files instead.
    

## How I Fixed My Issue

Here’s what I did to solve my web server problem:

1. Checked permissions: `ls -l` [`log.sh`](http://log.sh) showed no execute permission.
    
2. Added execute: `chmod 755` [`log.sh`](http://log.sh).
    
3. Changed owner: `sudo chown www-data:www-data` [`log.sh`](http://log.sh) `data.txt`.
    
4. Fixed the log file: `chmod 664 data.txt` so 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.
