Skip to content

Chmod command in Linux with examples

Chmod command in Linux with examples
Share

Reading Time: 3 minutes

Chmod command in Linux with examples. The ‘chmod’ command plays a pivotal role in this domain, allowing users to control access rights and security. This article provides an in-depth exploration of the ‘chmod’ command in Linux, accompanied by illustrative examples.

Introduction

In the Linux ecosystem, understanding and managing file permissions are fundamental skills for system administrators and users alike. The ‘chmod’ command plays a pivotal role in this domain, allowing users to control access rights and security. This article provides an in-depth exploration of the ‘chmod’ command in Linux, accompanied by illustrative examples.

Understanding File Permissions

In Linux, each file and directory has associated permissions that dictate who can read, write, or execute them. These permissions are categorized for the owner, group, and others, providing a granular control mechanism. The ‘chmod’ command allows users to modify these permissions.

Chmod Syntax: The syntax for the ‘chmod’ command is as follows:

bashCopy code

chmod [options] permissions file/directory

  • options: Optional parameters that modify the behavior of the command.
  • permissions: Numeric or symbolic representation of the desired permissions.
  • file/directory: The target file or directory for which permissions are to be modified.

Numeric Representation: The numeric representation of permissions consists of three digits, each representing the permissions for the owner, group, and others, respectively. Each digit is a sum of specific permission values:

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

For example, to give read and write permissions to the owner, read-only permissions to the group, and no permissions to others, the numeric representation would be 640.

Symbolic Representation: The symbolic representation of permissions allows for a more intuitive and flexible approach. It uses letters to denote the permission type and the symbols +, -, and = to assign, remove, or set permissions explicitly.

  • u: Owner
  • g: Group
  • o: Others
  • a: All (equivalent to ugo)

For instance, to grant execute permission to the owner, the symbolic representation would be chmod u+x file.

Examples:

  1. Numeric Representation:
    • chmod 755 myfile: Gives read, write, and execute permissions to the owner, and read and execute permissions to both the group and others.
    • chmod 600 sensitivefile: Grants read and write permissions exclusively to the owner.
  2. Symbolic Representation:
    • chmod u=rw,g=r,o=r myfile: Explicitly sets read and write permissions for the owner, read-only for the group, and read-only for others.
    • chmod a+x script.sh: Adds execute permissions for all users to the script.
  3. Combining Permissions:
    • chmod u+rw,g-w,o+r myfile: Adds read and write permissions for the owner, removes write permissions for the group, and adds read permissions for others.
  4. Recursive Mode:
    • chmod -R u=rwX,g=rX,o=rX directory: Recursively sets read and write permissions for files and execute permissions for directories within the specified directory.
  5. Octal Mode:
    • chmod 1777 tempdir: Sets the sticky bit, ensuring that only the file owner can delete or rename their files within the directory.

Advanced Chmod Techniques:

  1. Special Modes:
    • The setuid (s) and setgid (g) modes allow users to execute a file with the permissions of the file owner or group owner, respectively. For example, chmod u+s myfile sets the setuid bit.
  2. Default ACLs:
    • Using ‘default ACLs’ with ‘chmod’ ensures that specified permissions are inherited by newly created files and directories within a particular directory.
  3. Symbolic Links:
    • When dealing with symbolic links, ‘chmod’ operates on the link itself rather than the linked file. To change permissions on the linked file, use the ‘-h’ option.

Chmod command in Linux with examples

You can set permission based on your need however it is good to assign limited permission.

600 – In this first 6 is for owner , next Zero is for group and third Zero is for others.

$ chmod 600 test-gunzip

Chmod command in Linux with examples

We have also written a tutorial “How to Change Owner of File in Linux chown Command“, Please follow the same.

You can also read our tutorials on Linkedin

Conclusion:

Mastering the ‘chmod’ command is essential for effective file and directory management in Linux. This guide has provided a comprehensive overview of both numeric and symbolic representations, along with advanced techniques. Understanding and skillfully using ‘chmod’ empowers users to secure their files, control access, and enhance overall system security. We hope this tutorial “Chmod command in Linux with examples” explain concept of chmod.


Share

Leave a Reply

Your email address will not be published. Required fields are marked *