chmod ‐ #files #permissions - five4nets/Linux-Knowledgebase GitHub Wiki

Linux chmod Command Tutorial

The chmod (change mode) command in Linux is used to modify file and directory permissions. Permissions determine who can read, write, or execute a file or directory. This tutorial explains how chmod works, its syntax, and provides practical examples.

Table of Contents

Understanding Permissions

Linux permissions are divided into three categories:

  • Owner (u): The user who owns the file.
  • Group (g): Users in the file's group.
  • Others (o): All other users.

Each category can have these permissions:

  • Read (r): View file contents or list directory contents.
  • Write (w): Modify file contents or create/delete files in a directory.
  • Execute (x): Run a file (e.g., script or binary) or access a directory.

Permissions are displayed using ls -l, e.g., -rwxr-xr--:

  • First character: File type (- for file, d for directory).
  • Next 3 characters: Owner permissions (e.g., rwx).
  • Next 3: Group permissions (e.g., r-x).
  • Last 3: Others permissions (e.g., r--).

chmod Syntax

chmod [options] mode file
  • options: Common options include -R (recursive, applies to directories and their contents).
  • mode: Specifies the permissions to set, using either symbolic or numeric notation.
  • file: The target file or directory.

Using Symbolic Notation

Symbolic notation uses letters to represent permissions:

  • u (user/owner), g (group), o (others), a (all).
  • + (add), - (remove), = (set exact permissions).
  • r (read), w (write), x (execute).

Examples

  1. Add execute permission for the owner:

    chmod u+x script.sh
    

    Adds execute permission for the owner of script.sh.

  2. Remove write permission for group and others:

    chmod go-w file.txt
    

    Removes write permission for group and others on file.txt.

  3. Set exact permissions (read and write for owner, read for group, none for others):

    chmod u=rw,g=r,o= file.txt
    

    Sets owner to read/write, group to read-only, and no permissions for others.

  4. Recursively apply permissions to a directory:

    chmod -R a+rx /path/to/dir
    

    Grants read and execute permissions to everyone for dir and its contents.

Using Numeric (Octal) Notation

Numeric notation uses three or four digits (0–7) to represent permissions. Each digit is the sum of:

  • Read (r) = 4
  • Write (w) = 2
  • Execute (x) = 1

Each digit corresponds to owner, group, and others (in that order). For example:

  • 7 = rwx (4+2+1)
  • 6 = rw- (4+2)
  • 5 = r-x (4+1)
  • 0 = --- (no permissions)

Examples

  1. Set permissions to rwxr-xr-x (755):

    chmod 755 script.sh
    

    Owner: read/write/execute; group and others: read/execute.

  2. Set permissions to rw-r----- (640):

    chmod 640 file.txt
    

    Owner: read/write; group: read; others: no permissions.

  3. Recursively set directory permissions to rwxr-xr-x (755):

    chmod -R 755 /path/to/dir
    

    Applies rwxr-xr-x to dir and all its contents.

  4. Set permissions to rw------- (600):

    chmod 600 secret.txt
    

    Only the owner has read/write permissions; no access for group or others.

Common Examples

  1. Make a script executable by all:

    chmod +x myscript.sh
    

    Adds execute permission for owner, group, and others.

  2. Secure a private file:

    chmod 600 private.key
    

    Only the owner can read/write; no access for others.

  3. Allow group to edit a shared file:

    chmod g+w shared.txt
    

    Adds write permission for the group.

  4. Set a web directory to standard permissions:

    chmod -R 755 /var/www/html
    

    Sets directories to rwxr-xr-x and files typically inherit similar permissions.

References