This article explores how to list, set, and modify standard file permissions in Linux for effective file and directory ownership management.
In this article, we explore how to list, set, and modify standard file permissions in Linux. Mastering file permissions is crucial for managing file and directory ownership effectively.When you run the following command:
Copy
Ask AI
$ ls -l
you may see output similar to this, which shows that each file or directory is owned by a particular user:
Copy
Ask AI
-rw-r----- 1 aaron family 49 Oct 27 14:41 family_dog.jpg
In the example above, the file “family_dog.jpg” is owned by the user “aaron” and associated with the group “family”. Only the file owner or the superuser (root) can change its permissions.
To change the group of a file or directory, use the chgrp command. The syntax is as follows:
Copy
Ask AI
$ chgrp group_name file/directory
For example, to change the file’s group to “sudo”, execute:
Copy
Ask AI
$ chgrp sudo family_dog.jpg
After running this command and listing the file details using ls -l, you will see the group updated to “sudo”. Note that you can only change the group to one that you are a member of. To display your current groups, run:
Copy
Ask AI
$ groupsaaron sudo family
Only the root user can change the file group to any group available on the system.
To change the user owner of a file or directory, use the chown command with the syntax below:
Copy
Ask AI
$ sudo chown new_owner file/directory
For example, to change the ownership of “family_dog.jpg” to “jane”, use:
Copy
Ask AI
$ sudo chown jane family_dog.jpg
After executing ls -l, you will observe that the file’s owner is now “jane”. Only the root user has the privileges to change the file owner.You can also modify both the owner and group simultaneously using:
Copy
Ask AI
$ sudo chown aaron:family family_dog.jpg
This command resets the owner to “aaron” and the group to “family”. Here is a sequence of commands demonstrating changing ownership and group:
Copy
Ask AI
$ ls -l-rw-r----- 1 aaron family 49 Oct 27 14:41 family_dog.jpg$ chgrp sudo family_dog.jpg$ ls -l-rw-r----- 1 aaron sudo 49 Oct 27 14:41 family_dog.jpg$ sudo chown jane family_dog.jpg$ ls -l-rw-r----- 1 jane sudo 49 Oct 27 14:41 family_dog.jpg$ sudo chown aaron:family family_dog.jpg$ ls -l-rw-r----- 1 aaron family 49 Oct 27 14:41 family_dog.jpg
When accessing a file, Linux evaluates permissions in the following order:
If the user is the file owner, user permissions apply.
If not, and the user is a member of the file’s group, group permissions apply.
Otherwise, the “others” permissions are enforced.
Consider the following output:
Copy
Ask AI
(aaron)$ ls -l-r--rw---- 1 aaron family 49 family_dog.jpg
Even though “aaron” is in the “family” group (which has read and write permissions), the file displays the owner’s permissions (r—), meaning Aaron can only read the file. Attempting to append text as Aaron results in:
Copy
Ask AI
(aaron)$ echo "Add this content to file" >> family_dog.jpgbash: family_dog.jpg: Permission denied
However, if another user, such as “jane” (a member of the “family” group), accesses the file:
Copy
Ask AI
(aaron)$ su jane(jane)$ echo "Add this content to file" >> family_dog.jpg(jane)$ cat family_dog.jpgPicture of Milo the dog
If the user is neither the owner nor a member of the file’s group, the “others” permissions are applied.
You can set permissions to exact values using the equal sign. For instance, to set group permissions to read-only:
Copy
Ask AI
$ chmod g=r family_dog.jpg
This command sets group permissions to exactly “r—”, even if write or execute permissions were previously set. To remove all permissions for a group, use:
You can combine changes for the user (u), group (g), and others (o) in a single command. For example, to grant the owner read and write permissions, set the group to read-only, and remove all permissions for others:
For the owner, “rw-” translates to 110 in binary (6 in octal).
For the group, “r—” translates to 100 in binary (4 in octal).
For others, ”---” translates to 000 in binary (0 in octal).
A more common octal permission setting is 755, which means:
Owner: 7 (rwx, or 111 in binary)
Group: 5 (r-x, or 101 in binary)
Others: 5 (r-x, or 101 in binary)
Similarly, 777 means full permissions (read, write, and execute) for all.Below is an image that illustrates the conversion of binary file permissions to octal values:
Another image further explains the octal permission notation used in Unix-like systems:
In this article, we covered the following key topics:
Viewing file ownership and permissions using ls -l
Changing file group ownership with chgrp
Modifying file user ownership with chown
Understanding the structure and significance of file and directory permissions
Using chmod to modify permissions both with symbolic operators and octal notation
With this detailed guide, you now have the knowledge to effectively manage file permissions on Linux systems, ensuring both security and proper access control. Happy learning and see you in the next article!