Unix - permissions in UNIX.

1. What are Permissions in UNIX?

  • Permissions control who can access a file and how.

  • Every file/directory in UNIX has access rights for three categories of users:

    1. Owner (user) – The person who created/owns the file.

    2. Group – Users who belong to the file’s group.

    3. Others (world) – Everyone else.


2. Types of Permissions

There are three main types of permissions:

Symbol Permission Applies To Files Applies To Directories
r Read View file contents List directory contents
w Write Modify file contents Add/delete files in directory
x Execute Run the file (if program/script) Enter the directory (cd into it)

3. Viewing Permissions

Use the ls -l command:

ls -l file.txt

Example output:

-rwxr-xr--  1 user group  1024 Aug 16 10:00 file.txt

Breakdown:

  • - → type (- = file, d = directory).

  • rwx → Owner permissions (read, write, execute).

  • r-x → Group permissions (read, execute).

  • r-- → Others permissions (read only).

So:

  • Owner can read, write, execute.

  • Group can read and execute.

  • Others can only read.


4. Changing Permissions

(a) Using chmod (Change Mode)

Two methods:

  1. Symbolic method

chmod u+x file.txt   # add execute permission for owner
chmod g-w file.txt   # remove write permission from group
chmod o=r file.txt   # set others to read-only
  1. Numeric method (Octal values)

  • r = 4, w = 2, x = 1

  • Add them up to set permissions.

Examples:

chmod 755 file.txt   # rwx for owner, r-x for group, r-x for others
chmod 644 file.txt   # rw- for owner, r-- for group, r-- for others

5. Changing Ownership

  • chown → change file owner

  • chgrp → change group

Examples:

chown alice file.txt    # change owner to alice
chgrp developers file.txt # change group to developers

6. Special Permissions (Advanced)

  1. Setuid (s) – Run program with file owner’s privileges.

    • Example: passwd command runs with root privileges.

  2. Setgid (s) – Files created in a directory inherit the group.

  3. Sticky bit (t) – On a directory, only the file’s owner can delete it.

    • Example: /tmp directory.


7. Diagram – Permissions in UNIX

           +---------------------------+
           |        PERMISSIONS        |
           +---------------------------+
           |   User   |   Group | Other|
           +---------------------------+
           |   r w x  |   r w x | r w x|
           +---------------------------+

r = read   → file: view   | directory: list
w = write  → file: edit   | directory: add/remove
x = execute→ file: run    | directory: enter

In summary:

  • UNIX permissions define who can read, write, or execute a file/directory.

  • Controlled via ls -l, chmod, chown, chgrp.

  • Three levels: Owner, Group, Others.

  • Special bits (Setuid, Setgid, Sticky bit) give extra control.