chmod

chmod

The chmod command stands for „change mode“ and is used to alter the permissions of a file or directory on Linux systems. Permissions determine who can read, write, or execute a file, with three categories of access: owner, group, and others.

The basic syntax of the chmod command is:
bash
chmod [permissions] [file/directory]

Where:

  • [permissions]: specifies the new permissions for the file or directory. This can be in the form of a numeric value (e.g., 755), an octal code (e.g., 0755), or symbolic notation (e.g., u+x, g-w).
  • [file/directory]: specifies the file or directory whose permissions are to be changed.

The permissions can be specified in three ways:

  1. Numeric value: uses a three-digit number, where each digit represents the permissions for owner, group, and others, respectively. The digits 0-7 have the following meanings:

    • 0: no permission
    • 1: execute only
      *2: write only
    • 4: read-only
    • 5: read and execute (or write)
    • 6: read and write
    • 7: read, write, and execute
      Example: chmod 755 file.txt sets the permissions to owner read/write/execute, group read/execute, and others read/execute.
  2. Octal code: uses a four-digit octal number (0-7) where each digit represents the permissions for owner, group, and others, respectively.
    Example: chmod 0755 file.txt is equivalent to chmod 755 file.txt.

  3. Symbolic notation: uses letters to specify the new permissions. The letters have the following meanings:

    • u: owner
    • g: group
    • o: others
    • x: execute
    • w: write
    • r: read
      Example: chmod u+x file.txt adds execute permission for the owner, while chmod g-w file.txt removes write permission for the group.

Use cases:

  • Set permissions for a new file or directory: chmod 755 new_file.txt
  • Change ownership and permissions at once: chmod u=rwx,g=rw,o=r file.txt
  • Remove execute permission for others on a script: chmod o-x my_script.sh

Special hacks:

  • To reset permissions to default (readable, writable, and executable by the owner; readable by the group and others), use: chmod 600 file.txt (owner read/write/execute) or chmod 644 file.txt (owner read/write/execute, group read).
  • To create a world-readable directory for shared files: chmod o+rX my_shared_dir

Level of experience required: Intermediate to Advanced.

The chmod command is essential for managing file and directory permissions on Linux systems. Understanding the different ways to specify permissions will help you manage access control effectively, making it an important skill for system administrators, developers, and power users.