Share your love
Linux File Permissions Explained with Examples – 4 Easy Steps
This tutorial dives into the Linux File Permissions Explained with Examples. As you may know, Linux file permissions control who can read, write, or execute files and directories on a system. Every file and directory in Linux has an associated set of permissions, which defines what actions users can perform on that file or directory.
You can now proceed to the following steps from the Orcacore website on Linux File Permissions Explained with Examples.
Table of Contents
Dive Into Linux File Permissions Explained with Examples
For effective security, Linux divides authorization into 2 levels: first ownership and second permission.
Let’s start with the ownership from the Linux File Permissions Explained with Examples.
Step 1 – What is the Ownership of Linux files?
Every file and directory in our Linux system has 3 types of owner including user, group, and other.
- User: A user is the owner of the file. By default, the person who created the file becomes its owner.
- Group: A user group can contain multiple users. All users in a group have the same permission access to the file. Think you have a project where several users ask to access a file. In this situation, instead of manually setting permissions for each user, you can create a group, set permissions for that group, and add your members to it and only group members can read or modify the files.
- Other: This person hasn’t created a file or doesn’t belong to a group. It means everybody else. When we set the permissions for others, it means that we set permissions for the world.
Step 2 – What Are the Types of Permission in Linux?
Every file and directory in our Linux system has three permissions for all three owners mentioned above including read, write, and execute.
- Read: With this permission, you can open a file and read it. On a directory, it will give you the ability to list its content.
- Write: With this permission, you can modify the contents of a file. On a directory, by this permission, you can add, remove, and rename files stored in a directory.
- Execute: In Linux, you can’t run a program unless the execute permission is set.
Note: By using the ls -l
command you can see file type and access permissions.
For example, we get this output: ‘-rw-rw-r-- ‘
The first ‘-‘shows that we have selected a file. If it were a directory instead of ‘-‘it would have been shown by ‘d’.
Let’s dive into the file types and access permissions characters from the Linux File Permissions Explained with Examples.
How To Read File Permission Characters in Linux?
The following characters are used for file permissions in Linux:
- r=read permission
- w=write permission
- x=execute permission
- –=no permission
Consider this example output: ‘-rw-rw-r-- ‘
:
The first part of the code ‘rw-‘ means that the owner can: Read the file. Write or edit the file. But he can’t execute the file.
The second part of the code is ‘rw-‘. It is for the user group. Group members can: Read the file. Write or edit the file.
The third part is for the world which means any user. It says ‘r–‘. This means the user can only: Read the file.
Step 3 – How To Change File or Directory Permissions in Linux?
Ok! Now you have learned types of File permissions in Linux and ownership. At this point, you can use the ‘chmod’ command (changing mode) to set the permissions (read, write, execute) on a file or directory for the owner, group, and the world.
chmod permissions filename:
There are two ways of using this command: numeric and symbolic mode.
chmod command Numeric mode
For example:
chmod 764 filename
This code means that the file owner can read, write, and execute. The user group can read and write. The world can only read.
chmod command Symbolic mode
From Linux File Permissions Explained with Examples, In the symbolic mode, you can modify the permissions of a specific owner.
- + : (adds a permission to a file or directory)
- – : (removes the permissions)
- = : (Sets the permission and overrides the permissions set earlier)
Owners are shown as:
- u : (users)
- g : (groups)
- o : (other)
- a : (all)
For example:
chmod u-x filename
It means that the user cannot execute the file.
Step 4 – How To Change Ownership and Groups in Linux?
In this step of Linux File Permissions Explained with Examples, you can change the ownership of a file or directory. To do this, you can use the chown command:
chown user
If you want to change the user as well as a group for a file or directory use the command:
chown user:group filename
For changing group-owner only, you can use the following command:
chgrp group-name filename
That’s it, as you saw in the Linux File Permissions Explained with Examples, the learning process is too easy. To get more information, you can visit the official docs page of Linux.
Conclusion
In conclusion, Linux File Permissions Explained with Examples. Understanding file permissions is essential for effective system administration and maintaining security. Linux’s permission model allows you to control who can read, modify, or execute files and directories. By learning the symbolic and numeric representation of permissions, and commands like chmod, chown, and ls, you can easily manage access rights.
Hope you enjoy it. You may also, like the following articles:
Create and remove a directory in Linux