Linux File Permissions

Let’s start with creating a Linux file using below steps:

  1. Open the terminal, navigate to the desired directory

  2. Create an empty file using the touch command

  3. Confirm the creation with ls -l

    //Optionally view the file content using cat command.

Now that the file has been created, we will proceed to manage file permissions. This involves configuring access rights for different user categories using commands like chmod, chown, and chgrp

Before moving forward first lets understand differentiating between files and directories since it is crucial for effective system navigation. Utilize the ls command with the -l option, and examine the first character in the output. A hyphen denotes a regular file, while a "d" signifies a directory.

For eg:

Here, first character of my_directory is d which signifies directory. However, first character of my_file.txt is - which signifies it is file.


Chmod:

This command allows users to modify file permissions. It adjusts the read, write, and execute permissions for the owner, group, and others, enhancing security and control over file access.

It can be used by symbolic or numeric representations to assign specific permissions.

For instance:

Symbolic representation: chmod u+rwx,g-rw,o+r [file.txt]

Numeric representation: chmod 754 [file.txt]

These examples grant the owner read, write, and execute permissions, the group read-only, and others read-only access respectively.

For Numeric representation one can refer below chart. Here, each number represents various file permissions


Chown:

The chown command in Linux is used to change the ownership of files and directories. It allows users to transfer ownership between users and groups, providing flexibility in managing file access. This command assigns the specified user (new_owner) and group (new_group) as the new owners of the file.

The basic syntax is chown [new_owner]:[new_group] file, allowing for seamless adjustments to ownership.

For example: chown xyz:my_group my_file.txt

Initially, the file "my_file.txt" was owned by the user "ubuntu" and belonged to the group with the same name. After executing the chown command, the ownership was successfully changed to the user "XYZ" and the group "my_group". It's important to note that the execution of chown requires special permissions, and initially, it was denied, resulting in an "Operation not permitted" error. However, using the sudo command granted the necessary superuser privileges to execute the chown command successfully.

It is essential for effective user and group management in a Linux environment.


Chgrp:

The chgrp command in Linux is used to change the group ownership of a file or directory. This command allows users to modify the association of a file or directory with a specific group.

The basic syntax is chgrp [new_group] file, where [new_group] is the desired group name.

For example: chgrp my_group my_new_file.txt

This command changes the group ownership of "my_new_file.txt" to the specified group, such as "my_group". It is particularly useful when you need to adjust file or directory access permissions within a specific group.

Keep in mind as chown command executing chgrp also requires special permissions, and using sudo before the command allows for the necessary elevated privileges.

Understanding and effectively using chgrp is valuable for managing group ownership in a Linux environment.


ACL (Access Control List)

It offers advanced permissions beyond the standard owner, group, and others model.

  • Utilize 'getfacl' to view ACL entries for a file or directory, revealing detailed user-specific permissions.

  • Employ 'setfacl' to modify ACLs, granting or revoking permissions for users or groups.

For instance, setfacl -m u:abc:r allows user "abc" read access.

This level of granularity enhances access control.

This command is crucial for administrators seeking fine-grained control over file and directory permissions in a Linux environment.

Happy Learning!