Linux File Permissions - Basics and Commands

    As an open source, accessing a file requires more security in Linux. This has been maintained by providing file permissions and access modes. These are applicable for all files and directory.

File Permissions:

    The file permissions tells who all have access to the files.There are 3 attributes in file permissions classified as,
  1. Owner Permission
  2. Group Permission
  3. Other or world Permission
Owner Permission:

    The owner permission defines the action that can be done to the files by the Owner.

Group Permission:

    The Group permission defines the action that can be done to the files by the users on the group.

Other or world Permission:

    The Other permissions defines the action that can be done to the files by the world or public.


File Modes:

    The file modes defines what kind of access are provided to the users. There are 3 attributes in file modes as well,
  1. read
  2. write
  3. execute
read   - The read mode allows to view the file( Readable ). The value for read access is 4
write  - The write mode allows to modify the file ( Readable, Writable and Removable ). The value for write access is 2
execute - The execution mode allows to run the file as a program( Mostly applied to bash files in Linux ). The value for execute access is 1


   
     The above image shows the file permissions for files.
  • First will be the type of file. It may be a file, directory, or a link. 
  • Second three sets of value is assigned to the Owner of the file
  • The third three sets is assigned to the Group.
  • The final three sets is assigned to the public or world.
Printing the File permissions:

    To view file permissions and modes we can use either ls command with -l flag or ll command.







The file permission for a directory is always started with  d.
The file permission for a file is always started with  d.
The file permission for a link is always started with  l.

Changing the Ownership of a file:

    The Owner ship of the file can be changed in order to provide access to other users and groups. To obtain this we use  chown  command.

    Using chown command we can change the owner of the file at a time or user and group at the same time.







Changing the group of a file:

    If you want to change the group-owner alone, you can use  chgrp  command





    If you wish to change the permissions to all the subdirectories use  -R  flag along the  chown  command. It will recursively change the permissions for all files inside the directory




Changing File modes:

    As mentioned earlier the file modes defines the kind of access are provided to the users or groups to the file either the user has the right to read or write or delete.

    When comes to file mode there are two ways to change it.
  1. Absolute mode
  2. Symbolic mode
Absolute Mode:

    Absolute mode is also known as Numeric mode, file permissions are not represented as characters but a three-digit octal number. r=4, w=2 and x=1. Let us view how the values are defined on the following table.

Symbol RepresentsValueFor Absolute modePermission Type
---00No Permission
--x11Execute
-w-22Write
-wx2 + 13Execute + Write
r--44Read
r-x4 + 15Read + Execute
rw-4 + 26Read +Write
rwx4 + 2 + 17Read + Write +Execute

    We will see the changing the file mode through absolute mode. On the following example we will provide 761 permission for a file.






 For the test1.txt file the permissions are as follows,

Owner - File Owner jhony can read,write and execute the file.
Group - Users under the group ljunix can read and write the file.
Others - Rest of the users cannot access the file.

Symbolic mode:

    The symbolic mode allows us to modify permissions for a specific owner. It uses mathematical operators to modify the file permissions.

    In Symbolic mode we use some user denotations to change the permission along with the operators.

u stands for Owner
g stands for Group
o stands for Others
a stands for all

For Operators the permissions will be,


OperatorPermissions
                     +Adds a permission to a file or directory
-Removes the permission
                     =Sets the permission and overrides the permissions set earlier.

We will see this with an example, initially we will set the permissions to null for our test file



Now we will apply using symbolic mode,



    Without defining a user denotations the permissions will set to all the owner.



    We will add read and write access to the Owner of the file,





    These are the methods followed in changing the permissions for files using absolute mode and symbolic mode.

    Hope this articles helps you. Feel free to ask if you have any questions.

Comments

Popular Posts on this blog

Apache Webserver - Installation from source (tar.gz and tar.bz2)

Setting up a Mail server using Roundcube - Part-1 - Centos 7

How to Install LAMP Stack on Linux - Centos 7

Installing Docker-Compose on Ubuntu

Apache Tomcat Installation - Ubuntu - 17.04

Setting up a Mail server using Roundcube - Part-2 - Centos 7

Blog Top Sites

Blog Log