How to Use chmod Command in Linux

The chmod command in Linux is used to control file permissions. It allows you to specify who can access files, search directories, and run scripts. Let's dive into how it works:

  1. Understanding File Permissions

    • In Linux, each file or directory has three sets of permissions:
      • User permissions: These apply to the owner of the file.
      • Group permissions: These apply to members of the file's group.
      • Other permissions: These apply to everyone else.
    • Permissions determine actions that can be performed on the file or directory, such as reading, modifying, or executing.
  2. Viewing Permissions

    • To see the permissions set on a file or directory, use the ls -l command.
    • The output shows three sets of permissions (user, group, and other) represented by characters:

      • r: Read permission
      • w: Write permission
      • x: Execute permission
    • For example:

      Command: $ ls -l

      Result: -rw-r--r-- 1 user group 1234 Mar 10 10:00 myfile.txt

  3. Modifying Permissions

    • The chmod command uses the following syntax:

    chmod <permissions> <file_or_directory>

    • You can specify permissions using:

      • Text (Symbolic) Notation: Add or remove permissions using + or -.

        Example: chmod +x myfile.sh (adds execute permission)

      • Numeric (Octal) Notation: Represent permissions as a three-digit number.

        Each digit corresponds to user, group, and other permissions:

        4: Read permission

        2: Write permission

        1: Execute permission

        Example: chmod 755 myfile.sh (gives read, write, and execute permissions to the owner, and read and execute permissions to others)

  4. Examples

    • To make a script executable:

    chmod +x myscript.sh

    • To restrict permissions:

    chmod 600 sensitive_file.txt

Remember that chmod empowers you to manage access to files and directories effectively. Feel free to explore and experiment with different permissions!