# Understanding Linux File Ownership and Permissions (chown & chmod Explained)

Managing file ownership and permissions is a **core Linux skill**. Whether you’re working on servers, deploying applications, or writing shell scripts, understanding how Linux controls access to files and directories is critical for both **security and functionality**.

In this article, we’ll explore:

* File ownership in Linux
    
* Using `chown` to change ownership
    
* Understanding file and directory permissions
    
* Using `chmod` with **numeric** and **symbolic** notation
    
* Practical examples with files and scripts
    

---

## File Ownership in Linux

Every file and directory in Linux has:

* **An owner (user)**
    
* **A group**
    
* **Permissions** that define who can read, write, or execute it
    

You can view ownership and permissions using:

```bash
ls -l
```

Example output:

```bash
-rw-rw-r-- 1 root root 0 Jul 29 15:11 example.txt
```

Here:

* `root` → owner
    
* `root` → group
    

---

## Changing Ownership with `chown`

The `chown` command allows you to change the owner and group of a file or directory.

### Basic Syntax

```bash
sudo chown owner:group filename
```

### Example: Change File Ownership

```bash
sudo chown root:root example.txt
```

**Explanation:**

* `sudo` → runs the command with root privileges
    
* `chown` → change ownership
    
* `root:root` → new owner and group
    
* `example.txt` → target file
    

> ⚠️ Without `sudo`, you’ll usually get a **Permission denied** error.

---

## Working with Directories Recursively

Let’s create a directory structure with files:

```bash
mkdir -p new-dir/subdir
echo "Hello, world" > new-dir/file1.txt
echo "Another file" > new-dir/subdir/file2.txt
```

List everything recursively:

```bash
ls -lR new-dir
```

### Change Ownership Recursively

```bash
sudo chown -R root:root new-dir
```

* `-R` → applies the ownership change to **all files and subdirectories**
    

Without `-R`, only the top-level directory would change ownership.

---

## Understanding Linux File Permissions

Consider this permission string:

```bash
-rw-rw-r--
```

### Breaking It Down

| Section | Meaning |
| --- | --- |
| `-` | Regular file |
| `rw-` | Owner permissions |
| `rw-` | Group permissions |
| `r--` | Others |

### Permission Symbols

| Symbol | Meaning |
| --- | --- |
| `r` | Read |
| `w` | Write |
| `x` | Execute |
| `-` | Permission denied |

---

## Changing File Permissions with `chmod` (Numeric Mode)

The numeric (octal) method is concise and widely used.

### Permission Values

| Permission | Value |
| --- | --- |
| Read | 4 |
| Write | 2 |
| Execute | 1 |

### Example: Owner-Only Access

```bash
sudo chmod 700 example.txt
```

**Meaning:**

* Owner → read + write + execute (7)
    
* Group → no permissions (0)
    
* Others → no permissions (0)
    

Result:

```bash
-rwx------
```

---

## Directory Permissions Explained

Directory permissions behave slightly differently:

| Permission | Directory Meaning |
| --- | --- |
| `r` | List contents (`ls`) |
| `w` | Create/delete files |
| `x` | Enter directory (`cd`) |

### Example: Secure Directory

```bash
mkdir ~/test-dir
chmod 700 ~/test-dir
```

Check permissions:

```bash
ls -ld ~/test-dir
```

Output:

```bash
drwx------ 2 user user 4096 Jul 29 15:45 test-dir
```

---

## Making a Directory Publicly Readable

```bash
chmod -R 755 ~/test-dir
```

### What Does `755` Mean?

| Role | Permissions |
| --- | --- |
| Owner | rwx (7) |
| Group | r-x (5) |
| Others | r-x (5) |

Result:

```bash
drwxr-xr-x
```

Anyone can now **list and access** files, but only the owner can modify them.

---

## Symbolic Permissions (Human-Friendly)

Symbolic notation is useful when you want to **add or remove specific permissions**.

### Create a Script

```bash
cd ~/project
echo '#!/bin/bash\necho "Hello, World"' > script.sh
```

Check permissions:

```bash
ls -l script.sh
```

```bash
-rw-rw-r--
```

Try running it:

```bash
./script.sh
```

❌ Permission denied

---

## Adding Execute Permission with Symbolic Mode

```bash
chmod u+x script.sh
```

### Explanation:

* `u` → user (owner)
    
* `+` → add permission
    
* `x` → execute
    

Verify:

```bash
ls -l script.sh
```

```bash
-rwxrw-r--
```

Now run it:

```bash
./script.sh
```

✅ Output:

```bash
Hello, World
```

---

## Numeric vs Symbolic chmod

| Method | Best For |
| --- | --- |
| Numeric (`755`) | Setting full permission sets |
| Symbolic (`u+x`) | Small, targeted changes |

---

## Key Takeaways

* `chown` controls **ownership**
    
* `chmod` controls **permissions**
    
* Files need **execute (**`x`) permission to run
    
* Directories need **execute (**`x`) permission to be accessed
    
* Use `-R` carefully — recursive changes can be dangerous
    
* Always double-check commands when using `sudo`
    

---

## Final Thoughts

Linux permissions are the foundation of system security. Once you understand how ownership and permissions work together, managing servers and applications becomes much safer and more predictable.

If you’re learning Linux seriously, mastering `chown` and `chmod` is **non-negotiable**.

Happy hacking 🐧✨

---
