Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux chmod Command: Modify The Permissions Of A File Or Directory

The chmod command in Linux (short for "change mode") is a utility used to change the permissions of files and directories. In this tutorial, we will discuss how to use the chmod command effectively, along with various options and examples.

  1. Understanding Linux permissions:

    Linux permissions are based on three types of access: read (r), write (w), and execute (x). These permissions are set for three different categories of users: the owner of the file, the group that the file belongs to, and others (everyone else). Permissions are usually represented using a combination of these characters (e.g., rwxr-xr-x).

  2. Basic usage of the chmod command:

    The chmod command can be used with either symbolic or numeric (octal) representations of permissions.

    • Symbolic representation:

      To change permissions using symbolic representation, use the chmod command followed by the permissions changes and the path of the file or directory:

      chmod [ugoa][+-=][rwx] file.txt
      

      The first part [ugoa] represents the user categories: user (owner) u, group g, others o, and all a. The second part [+-=] represents the operations: add +, remove -, and set =. The third part [rwx] represents the permissions: read r, write w, and execute x.

    • Numeric (octal) representation:

      To change permissions using numeric representation, use the chmod command followed by a three-digit octal number and the path of the file or directory:

      chmod 755 file.txt
      

      Each digit in the octal number represents the permissions for a specific category of users: the first digit is for the owner, the second is for the group, and the third is for others. The digit is the sum of the permissions' numeric values: read 4, write 2, and execute 1.

  3. Examples of using the chmod command:

    • Grant read, write, and execute permissions to the owner, and read and execute permissions to the group and others:

      chmod u=rwx,g=rx,o=rx file.txt
      

      or

      chmod 755 file.txt
      
    • Remove write permission from the group and others:

      chmod go-w file.txt
      
    • Add execute permission for all users:

      chmod a+x file.txt
      
  4. Changing permissions recursively:

    To change the permissions of a directory and its contents recursively, use the -R or --recursive option:

    chmod -R 755 /path/to/directory
    

    This command will change the permissions of the specified directory and all its files and subdirectories to 755.

By following this tutorial, you should now have a good understanding of how to use the chmod command in Linux to change the permissions of files and directories. The chmod command is an essential tool for managing file access and ensuring that the right users have the appropriate permissions for files and directories.

  1. How to use chmod command in Linux:

    • The chmod command is used to change the permissions of files and directories in Linux.
    chmod [options] mode file
    
  2. Changing file permissions with chmod:

    • Modify the permissions of a file using symbolic or numeric mode.
    chmod +x filename
    
  3. Setting read, write, and execute permissions in chmod:

    • Grant or revoke specific permissions using symbolic notation.
    chmod u+rwx file
    
  4. Modifying permissions for specific users in chmod:

    • Adjust permissions for user, group, or others individually.
    chmod u=rx,g=w,o+x file
    
  5. Recursive permission change with chmod in Linux:

    • Apply permission changes recursively to files and subdirectories.
    chmod -R mode directory
    
  6. Numeric mode vs. symbolic mode in chmod:

    • Choose between numeric (octal) and symbolic notation to set permissions.
    chmod 755 file   # Numeric mode
    chmod u=rwx,go=rx file   # Symbolic mode
    
  7. Viewing current file permissions with chmod:

    • Display the current permissions of a file.
    ls -l filename
    
  8. Revoking permissions using chmod:

    • Remove specific permissions from a file.
    chmod go-rw file
    
  9. Advanced chmod options for fine-grained control:

    • Utilize advanced options like --reference and --preserve-root for precise control.
    chmod --reference=reference_file target_file