> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Linux Permissions

> Guide to Linux file and directory ownership and permissions, reading ls -l output, using chmod chown chgrp, and understanding SUID SGID and sticky bits with examples.

On Linux, every process and file operation runs under a user account. Controlling who can log in, which files they can access, and what actions they can perform starts with correct user, group, and permission management. This guide explains how to read permission strings, manage ownership, apply file/directory permissions, and use special permission bits (SUID/SGID/sticky). Follow the examples and use `ls -l` frequently to verify changes.

## Overview — the access metaphor

Think of a Linux system as a large office building:

* Each file or directory is a room.
* The owner is the person responsible for that room.
* The group is a department that may share access.
* Others are everyone else in the building.
  Permissions determine what each of these entities can do in a room: read, write, or enter/execute.

## Reading permissions with ls -l

The `ls -l` command gives a long listing that reveals file type, permissions, ownership, size, and timestamps.

What each field means:

* First field: file type and permission bits
  * First character: file type (`d` = directory, `-` = regular file, `l` = symlink).
  * Next nine characters: three triplets (owner, group, others).
* Second field: number of hard links.
* Third and fourth fields: user owner and group owner.
* Following fields: file size, modification time, and name.

Example:

```bash theme={null}
# View files and their permissions in the current directory.
ls -l

# Sample output:
drwxr-xr-x 1 student students 122 Jan 13 12:00 mydirectory
-rw-r--r-- 1 student students 2000 Feb  2 13:12 myfile
```

* The owner triplet (first 3 permission characters after the file type) applies to the user owner.
* The group triplet applies to members of the file's group.
* The others triplet applies to everyone else.

## Ownership basics

* User owner: typically the account that created the file. They get the owner triplet.
* Group owner: determines access for members of that group. Use groups to implement shared access across team members.

If a directory belongs to the group `developers` and group write permission is enabled, any member of `developers` can modify files in that directory (subject to directory entry permissions).

## Basic permissions (r, w, x)

* Read (`r`): For files, allows reading contents. For directories, allows listing contents.
* Write (`w`): For files, allows modifying the file. For directories, allows creating or deleting entries.
* Execute (`x`): For files, allows running the file (if executable). For directories, allows entering the directory (`cd`) and resolving inodes.

![A dark-themed slide titled "Basic Permissions" showing a three-column table of Permission, Files, and Directories. It lists Read, Write, and Execute with their effects (e.g., read = can be opened/listed, write = can be edited/modified, execute = can be executed/entered).](https://mintcdn.com/kodekloud-c4ac6d9a/9E2T0ocsNwD7Od8i/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/Linux-Permissions/Linux-Permissions/basic-permissions-read-write-execute.jpg?fit=max\&auto=format\&n=9E2T0ocsNwD7Od8i\&q=85\&s=ff70982297a2ac09447f1bd0ecea2698)

Permission evaluation order (how the kernel checks access):

1. Is the accessing user the file owner? If yes, use the owner triplet.
2. Else, is the accessing user a member of the file's group? If yes, use the group triplet.
3. Else, use the others triplet.

## Numeric (octal) and symbolic permission modes (chmod)

Use `chmod` to change permission bits.

Octal mode:

* Each permission triplet (owner/group/others) is a digit 0–7.
* Each digit is a sum of read (4), write (2), execute (1).

Permission bit mapping:

| Bit value | Permission |
| - | - |
| `4` | read (`r`) |
| `2` | write (`w`) |
| `1` | execute (`x`) |
| `7` | `rwx` (4+2+1) |
| `6` | `rw-` (4+2) |
| `5` | `r-x` (4+1) |
| `0` | `---` |

Examples:

```bash theme={null}
# Set permissions with numeric (octal) coding
chmod 764 myfile    # owner=rwx (7), group=rw- (6), others=r-- (4)

# Recursive application of permissions to a directory
chmod -R 764 mydirectory
```

Symbolic mode uses entities and operators:

* Entities: `u` = user (owner), `g` = group, `o` = others, `a` = all
* Operators: `+` = add, `-` = remove, `=` = set exactly

Example commands:

```bash theme={null}
# Add execute permission to everyone
chmod a+x myfile    # or chmod +x myfile

# Set owner to rwx only
chmod u=rwx myfile

# Add group write permission
chmod g+w file

# Set sticky bit example for world-writable directory (e.g., /tmp)
chmod 1777 mydirectory
```

## Special permissions: SUID, SGID, sticky bit

Special bits affect how executables run or how directories behave.

* Set-user-ID (SUID): when set on an executable, processes launched run with the file owner's effective user ID. Set with `u+s`.
* Set-group-ID (SGID):
  * On executables: runs with the file's group ID (`g+s`).
  * On directories: new files created inside inherit the directory’s group.
* Sticky bit: on a directory, prevents users from deleting or renaming files they don't own. Commonly used on `/tmp` (e.g., `chmod 1777 /tmp`).

Special bits are encoded as a leading octal digit:

* `4` = setuid
* `2` = setgid
* `1` = sticky

Examples:

```bash theme={null}
# Set SUID on an executable
chmod u+s /usr/local/bin/someprog

# Set SGID on a directory so new files inherit the group's owner
chmod g+s /srv/shared

# Set sticky bit so only owners can remove their files
chmod 1777 /tmp
```

## Changing ownership: chown and chgrp

* `chown` changes user owner, optionally the group owner.
* `chgrp` changes only the group owner.
* Both support `-R` for recursive changes.

Examples:

```bash theme={null}
# Change the user owner of a file
chown student myfile

# Change both the user and group ownership
chown student:students myfile

# Change ownership recursively
chown -R student:students /path/to/directory

# Change the group ownership only
chgrp students myfile
```

Managing permissions without correct ownership can leave security gaps. Combine `chmod` with `chown`/`chgrp` to fully control access.

## Demo: users, groups, ownership, and permissions

This example walks through creating users and a group, assigning group membership, creating a project directory, and applying ownership and permissions.

Run as root (or use `sudo` where needed):

```bash theme={null}
# Create users and a group
useradd dev1
useradd dev2
useradd manager
groupadd devteam

# Add dev1 and dev2 to devteam
usermod -aG devteam dev1
usermod -aG devteam dev2

# Create project directory and files
cd /
mkdir project
touch /project/app.conf

# Change ownership of the file to dev1:devteam
chown dev1:devteam /project/app.conf

# Verify ownership and permissions
ls -l /project/
# Expected:
# total 0
# Set permissions: owner read/write, group read, others none
chmod 640 /project/app.conf

# Verify result
ls -l /project/
# Expected:
# total 0
# Create a second file
touch /project/app2.conf

# Ensure directory and contents belong to dev1:devteam
chown -R dev1:devteam /project

# Verify ownership for all files
ls -l /project/
# Expected:
# total 0
# -rw-r--r-- 1 dev1 devteam 0 Apr 20 16:06 app2.conf
# Add write for group to app.conf (symbolic)
chmod g+w /project/app.conf

# Verify group write was added
ls -l /project/
# Expected:
# total 0
# -rw-r--r-- 1 dev1 devteam 0 Apr 20 16:06 app2.conf
# Optionally remove all permissions for others explicitly
chmod o-rwx /project/app.conf

# Final verification
ls -l /project/
# Expected:
# total 0
# -rw-r--r-- 1 dev1 devteam 0 Apr 20 16:06 app2.conf
# -rw-rw---- 1 dev1 devteam 0 Apr 20 16:03 app.conf
```

This sequence demonstrates:

* Creating users and a group.
* Granting supplementary group membership.
* Creating files and directories.
* Assigning ownership and permissions.
* Using both symbolic and octal modes.

Use `ls -l` after each change to confirm results.

When designing access control, follow the principle of least privilege: grant only the minimum permissions required. Prefer using groups for shared access instead of broad "others" permissions.

## Quick reference table

| Command | Purpose | Example |
| - | - | - |
| `ls -l` | Show file type, permissions, owner, group, size, time | `ls -l /etc/passwd` |
| `chmod` | Change permissions (octal or symbolic) | `chmod 644 file.txt` |
| `chown` | Change owner and group | `chown alice:staff file.txt` |
| `chgrp` | Change group only | `chgrp staff file.txt` |
| `useradd`, `groupadd` | Create users and groups | `useradd bob` |
| `usermod -aG` | Add user to supplementary group | `usermod -aG devteam dev1` |

## Links and references

* [Linux file permissions overview (The Linux Documentation Project)](https://tldp.org/LDP/intro-linux/html/sect_03_01.html)
* [chmod manual (man7.org)](https://man7.org/linux/man-pages/man1/chmod.1.html)
* [chown manual (man7.org)](https://man7.org/linux/man-pages/man1/chown.1.html)
* [useradd/usermod manual pages (man7.org)](https://man7.org/linux/man-pages/man8/useradd.8.html)
* [Understanding SUID, SGID and sticky bit (Red Hat)](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security_guide/s-special-permissions)

By mastering these commands and concepts, you'll be able to control who can access files and directories on your Linux systems and apply secure, auditable permissions for both single-user and multi-user environments.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/3c103226-2d1c-4dd2-ac12-db543617e7c2/lesson/16c06a8c-57b0-479c-b8eb-17f821ec1ae5)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.