Day 6 : Linux File Permissions

vinod chandravinod chandra
2 min read

Understanding Linux File Permissions

File permissions in Linux are crucial for securing and managing access to files and directories. Here's a simple explanation of how they work and how you can manage them.

Categories of Users

  1. Owner: The user who created the file or directory.

  2. Group: A group of users who share similar permissions.

  3. Others: All other users on the system.

Permission Types

Each file or directory has three types of permissions:

  • Read (r): Allows viewing the content of a file or listing the contents of a directory.

  • Write (w): Allows modifying the content of a file or adding/removing files in a directory.

  • Execute (x): Allows running a file as a program or entering a directory.

Viewing Permissions

To view the permissions of a file, use the ls -ltr command:

ls -ltr filename

The output will show the permissions in a string like -rwxr-xr--. Here’s a breakdown:

  • The first character indicates the type (- for file, d for directory).

  • The next three characters (rwx) are the permissions for the owner.

  • The middle three characters (r-x) are the permissions for the group.

  • The last three characters (r--) are the permissions for others.

Changing Permissions

  1. Change Ownership: Use chown to change the owner.

     chown new_owner filename
    
  2. Change Group: Use chgrp to change the group.

     chgrp new_group filename
    
  3. Change Permissions: Use chmod to change permissions.

     chmod u+rwx filename    # Add read, write, and execute permissions for the owner
     chmod g-w filename      # Remove write permission for the group
     chmod o=r filename      # Set read-only permission for others
    
  4. Task Example

  5. Create a File:

     touch myfile.txt
    
  6. View Permissions:

     ls -ltr myfile.txt
    
  7. Change Permissions:

     chmod u+x myfile.txt     # Add execute permission for the owner
     chmod g+r myfile.txt     # Add read permission for the group
     chmod o-r myfile.txt     # Remove read permission for others
    

    View Changes:

     ls -ltr myfile.txt
    

By understanding and using these commands, you can effectively manage who can access and modify your files, ensuring better security and organization within your Linux system.

0
Subscribe to my newsletter

Read articles from vinod chandra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

vinod chandra
vinod chandra