The file permissions is a security feature built into Linux-based systems and is one of the first things you need to learn when you start to study linux.
Groups
There are three groups to access files and directories.
- owner – only to the owner of the file or directory.
- group – only to the group of the file or directory.
- all users – to all users in the system.
Types
There are three types of access to files and directories. So when we give an access to a group(owner, group, all users), we can chose the type of access. Can be one, two or all of them.
- read – to read the file.
- write – to write or modify a file or directory.
- execute – to execute a file or view the contents of a directory.
Viewing the Permissions
In the command line
In the command line you can see the permissions in the output of “ls -l” command on this format:
- The first character “?” is a special permission and it can vary.
- The first block of (rwx) is for the owner.
- The second block of (rwx) is for the group.
- The third block of (rwx) is for all users.
Special permissions
- _ – no special permission.
- d – directory.
- l – the file or the directory is a symbolic link.
- s – This indicates the setuid/setgid permissions.
- t – This indicates the sticky bit permissions.
Modify
We gonna see two ways to modify the permissions. Symbolic Mode – Explicitly Defining Permissions and the Absolute(Numeric) Mode – with binary references.
Symbolic Mode – Explicitly Defining Permissions
Groups
The groups used for this operation are:
- u – Owner
- g – Group
- o – Others
- a – All users
Assignment Operators
- + (plus) – add permission
- – (minus) – remove permission
Permissions to add or remove
- r – read
- w – write
- x – execute
Example
Let’s give permission to execute to all the users.
Absolute(Numeric) Mode – with binary references
First of all we need to understand how the references works.
References
- r – 4
- w – 2
- x – 1
Combine References
- rwx = 4 + 2 + 1 = 7
- rw_ = 4 + 2 = 6
- r__ = 4
- ___ = 0
Examples
Let’s keep read, write and execute to the owner (4 + 2 + 1 = 7) , read and execute to the group (4 + 0 + 1 = 5) and only read to all users (4 + 0 + 0 = 4). So we have the binary combination 754.
Changing the Owners and Groups(chown)
Now that we saw how to add and remove permissions for the owner, groups and all users, it’s time to learn how to change a owner and group of the file or directory.
It’s achieved with chown
sudo chown <OWNER>:<GROUP> <FILE_NAME>
But before to change the owners and groups, the itcoffeecrew user should be part of the sudo group. So lets add this user to the sudo group from another user that is already in the sudo group.
It’s achieved with adduser
sudo adduser itcoffeecrew sudo
Example
Once our itcoffeecrew user is in the sudo group we can change the owner and group of the file or directory.
Conclusion
Now that you learn how to change the owners and groups of the files and directories and to view and to modify the permissions in different ways, you should be able to better understand how the access to the files and directories in the Linux-based system works.