Linux File Persmissions

Linux File Persmissions

Today we will discuss about file permissions in context of Linux.In this session, we will delve into the world of Linux permissions and ownership, and engage in practical tasks related to both. Let's begin by exploring the realm of Permissions.

File permissions and ownership are crucial aspects of system security in Unix-based systems like Linux. They control who can access files, directories, and execute programs. there are three types of permissions: read (r), write (w), and execute (x). These permissions apply to three entities: the owner (u), the group (g), and others (o). This can be easily understand by given image:

File Permissions:

  1. Read (r): Allows viewing the contents of a file or listing a directory's contents.

  2. Write (w): Permits modifying a file's contents or adding/removing files within a directory.

  3. Execute (x): Enables executing a file (if it's a program or script) or accessing contents within a directory.

Permission Levels:

  1. Owner (u): The user who owns the file or directory.

  2. Group (g): The group associated with the file or directory.

  3. Others (o): Everyone else who isn't the owner or part of the group.

Permission Representation:

  • Symbolic Notation:

    • u (user/owner), g (group), o (others)

    • +(add permission), - (remove permission), = (set permission)

    • Example: chmod u=rw,g=r,o=r file grants read and write permissions to the owner, read to the group, and read to others.

  • Octal Notation:

    • Read (4), Write (2), Execute (1)

    • Example: chmod 755 file sets read, write, and execute for the owner, and only read and execute for the group and others.

Ownership:

  • User Owner: The individual who created the file or directory.

  • Group Owner: A collection of users with shared access permissions.

  • chown: Command used to change file ownership.

  • chgrp: Command used to change group ownership.

Commands and Usage:

  • chmod: Modify file permissions.

  • chown: Change file owner.

  • chgrp: Change group ownership.

  • ls -l: List files with detailed information including permissions and ownership.

  • sudo: Prefix command for superuser privileges when altering system files.

Example:Created a directory named testfile. Inside testfile created an empty directory "data" and text file "testfile.txt". For checking file permission run ls -l command. This will list following details:

  • permission

  • link

  • owner

  • group owner

  • size of file

  • date and time of file creation

  • name of file

If we look permission, for directory "data" the permission is "drwxrwxr-x" , d stands for directory, for user and group permission is "rwx" it means user and group both have read, write and execute permission, for others permission is "r-x" it means others only can read and execute they can't perform write. For textfile.txt the permission is "-rw-rw-r--" , first "-" it means its a file, for user and group permission is "rw-rw-" it means user and group both have read and write permission they don't have execute permission, for others permission is "r--" it means others can only perform read operation they don't have write and execute.

chmod u=rw,g=r,o=w testfile.txt grants read and write permissions to the owner, read to the group, and write to others.

"chmod u-rw,g-r, o-r data" remove read, write permissions from user, read permission from group and others.

"chmod u+rw data" add read write permission to user.

chmod 755 testfile.txt sets read, write, and execute for the owner, and only read and execute for the group and others.

file permission weightage we can also calculate with the below table.

Happy Learning,

Khushbu Tiwari