4 - Linux Permissions

What are permissions in Linux OS? Every file or folder in Linux has access permissions.This permissions will define what we can do with the files.

There are three types of permissions:

  • read access
  • write access
  • execute access
  • Permissions are defined for three types of users:

  • the owner of the file
  • the group that the owner belongs to
  • other users
  • To see the permission of a file we can use the following command :

      ls -l

    will list all the objects in the current working directory

    We can see in the above image that we have 3 objects in our /Documents dir.

    File permissions notation

    Now let's learn how to interpret all the crazy numbers and words and the other stuff on the terminal after the use the "ls -l" command.

    Since we are at the terminal line and we do not have any graphical and we can not click+right to see our file proprieties we need to learn how to interpret out terminal information.

    Textual representation

    Is represented by 10 characters.

     -rwxr--r-- (symbols)
     0123456789 (position)

    These permissions can be divided up as follows:

  • position 1 = type of object.(can be a file or a directory)(0---------)
  • positions 2-4 = permissions for owner. (the user that created the file/dir)(-123-------)
  • positions 5-7 = permissions for group.(----456---)
  • positions 8-10 = permissions for the rest of the users/visitors.(-------789)
  • Position 1 and it's possible types:

  • d= directory .
  • -= file .
  • l= link to another file or object (like a Windows shortcut) .
  • s= socket .
  • == pipe .
  • b= block device (eg, a disk drive that handles data a block at a time) .
  • c= character device .
  • Positions 2, 3 and 4 tell us if the owner has read, write and execute permissions for the file or object.

  • 2 - r = read .
  • 3 - w = write (create, modify, delete) .
  • 4 - x = execute (run).
  • So if we have for example this situation :

    We can see in the above image that we have 3 objects in our /Documents dir.

    Let's "translate" each of them :

  • we will start with the object/file called "dir1"
  •   drwxr-xr-x 2 wannabedba wannabedba 4096 2012-03-29 17:35  dir1

    So how do we interpret this ?

    Type

    We can see the object is a Directory.

    Permissions

  • -the owner has the rights( rwx ) - read,write and execute;
  • -the group has the rights( r-x ) - read,has no rights to write and can execute;
  • -other visitors/users that are not the owner or they are not in the group have the rights ( r-x ) - read,has no rights ro write and can execute;
  • Links

    - we have 2 links to this dir.

    Owner

    - the owner of this dir is "wannabedba";

    Group

    - the group that "wannabedba" is part of is "wannabedba";

    Size

    - the size of the file is 4.0 k or 4096 , if the dir has less then 4.0 k then it will show the minimum size of a block.(which is 4096);

    Data of creation/modified

    - we can see that this dir was created on 2012-03-29;

    Time of creation/modified

    - this dir was created/modified at 17:35;

    Name

    - the name of our dir is "dir1";

    How to give and change permissions in Linux.

    For this task we will need to use the "Chmod" command.

    Syntax of the chmod command is:

      chmod [options] permissions file[s]

    Options

  • [-R] set permissions recursively.
  • [-f] "forced" or silent mode.
  • [-v] "verbose", show information for every file processed.
  • [-c] show information only if changes are made to the file.

    Numeric Permissions:

    CHMOD can be attributed by using Numeric Permissions:

    Syntax

      chmod 644 "file.txt"

    This gives the file read/write by the owner and only read by everyone else(-rw-r--r--).

    Here is shown how the octane and how they be used with "chmod":

    First thing you need to know is that there are 10 bits associated with every file sliced in 4 parts (type,owner,group,guests) like we have learned at the begging of this tutorial. We will take an example of a file and show how permissions are represented in octal number, so we can apply them when we give permissions to our file,dirs.

    The existing permissions for the above file in octal numbers could be represented as follows

      rwxr-xr-x ==>
      111101101 ==>
      755

    Frequently used numeric parameters for chmod.

  • 755 -The general preferred permissions for almost all the files on your disk.
  • 700 -Extremely private data.
  • 500 -Extremely private data that you would not like to accidentally modify. So write protect it.
  • 775 -General files used when working as a Group (Others can only view/execute your files).
  • 770 -Important files used when working as a Group (Others cannot do anything with your files).
  • 750 -Allowing group to view your files but no write access (Others cannot do anything with your files).
  • 777 -Full permission for everybody (not very common).
  • In the next tutorial we will learn how to create new objects(files,dir,etc..) in Linux OS.