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 chown Command: Modify The Owner And Group Of Files And Directories

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 chown command in Linux:

    • The chown command is used to change the owner and/or group of files and directories in Linux.
    chown [options] owner[:group] file
    
  2. Changing file ownership in Linux with chown:

    • Modify the owner of a file.
    chown new_owner filename
    
  3. Assigning a new owner to files and directories:

    • Change the owner of multiple files.
    chown user1 file1 file2
    
  4. Changing both owner and group with chown:

    • Set both owner and group for a file.
    chown user:group filename
    
  5. Recursive ownership change with chown in Linux:

    • Apply ownership changes recursively to files and subdirectories.
    chown -R user:group directory
    
  6. Viewing current file ownership with chown:

    • Display the current owner and group of a file.
    ls -l filename
    
  7. Preserving file permissions during ownership change:

    • Retain existing file permissions when changing ownership.
    chown --preserve=file_mode new_owner:new_group filename
    
  8. Using chown to transfer ownership between users:

    • Facilitate the transfer of file ownership between users.
    chown --from=old_owner --to=new_owner file
    
  9. Combining chown and chgrp commands in Linux:

    • Change both owner and group using chown and chgrp.
    chown user:group file
    chgrp new_group file