Linux - File System Permissions

Linux - File System Permissions

·

4 min read

File permissions in Linux are required to restrict access to owners, groups and others. It secures the files and directories from unauthorized access. There are 3 types of file permissions:

  1. Basic permission

  2. Special permission

  3. Access Control List (ACL permission)

Viewing Permissions

  1. Check the file permission.

     ubuntu@ip-172-31-93-3:~$ ls -al newfile_txt
     # Permission Link Owner GroupOwner FileSize Date   Time  Filename
       -rw-rw-r--  1   ubuntu   ubuntu      0    Mar 31 20:21 newfile_txt
    
  2. Check the directory permission.

     ubuntu@ip-172-31-93-3:~$ ls -ld dev 
     # Permission Link Owner GroupOwner FileSize Date   Time  directoryName
       drwxrwxr-x  2   ubuntu ubuntu     4096    Mar 31 20:29   dev
    

Permission Groups/ Classes:

Owner (u)

Permissions are used for the owner of the file

Group (g)

Permissions used by members of the group

Other (o)

Permissions used by all other users

Permission Types:

PermissionAccess to a fileAccess to a directory
Read (r)display file contents and copy the fileview the contents of the directory
Write (w)modify the file contentsmodify the contents of the directory
Execute (x)execute the file if it is an executable allowallow the use of the cd command to access the directory

Permission with Numeric & Symbols :

NumberPermission TypeSymbol
0No Permission- - -
1Execute- - x
2Write- w -
3Execute + Write- w x
4Readr - -
5Read + Executer - x
6Read + Writer w -
7Read + Write + Executer w x

Change permissions of the file/folder :

  1. Give execute permission to the owner/user

     # [syntax: chmod u+<permission_type> <name of file or directory>]
     ubuntu@ip-172-31-93-3:~$ chmod u+x newfile_txt
    
  2. Give write and execute permissions to groups

     # [syntax: chmod g+<permission_type> <name of file or directory>]
     ubuntu@ip-172-31-93-3:~$ chmod g+wx newfile_txt
    
  3. Give write permission to others

     # [syntax: chmod o+<permission_type> <name of file or directory>]
     ubuntu@ip-172-31-93-3:~$ chmod o+w newfile_txt
    
  4. Remove execute permission of the owner/user

     # [syntax: chmod u-<permission_type> <name of file or directory>]
     ubuntu@ip-172-31-81-146:~$ chmod u-x newfile_txt
    
  5. Change ownership of a file

     # [syntax: chown <user_name> <name of file or directory>]
     ubuntu@ip-172-31-81-146:~$ chown steve newfile_txt
    
  6. Change group ownership

     # [syntax: chown <group_name> <name of file or directory>]
     ubuntu@ip-172-31-81-146:~$ chgrp steve newfile_txt
    
  7. Set the permission with a numeric value of a file 'rwx' for a user, 'rw' for group, 'r' for others

     # [syntax: chmod <numeric_permissions> <name of file or directory>]
     ubuntu@ip-172-31-81-146:~$ chmod 764 newfile_txt
    

    Access Control List (ACL)

    It provides an additional, more flexible permission mechanism for file systems. It is a service that provides special permissions to specific users and groups for particular directories and files.

    Also, we can provide read-and-write access to a particular user that is not a member of the group created by you using ACL.

    1. Check ACL permission

       # [syntax: getfacl <name of file or directory>]
       ubuntu@ip-172-31-94-49:~$ getfacl newfile.txt
      
    2. Set ACL permission to the user

       # [syntax: setfacl -m u:<user_name>:<permission_type> <name of file or directory>]
       ubuntu@ip-172-31-94-49:~$ setfacl -m u:steve:rwx newfile.txt
      
    3. Set ACL permission to group

       # [syntax: setfacl -m g:<group_name>:<permission_type> <name of file or directory>]
       ubuntu@ip-172-31-94-49:~$ setfacl -m g:testgrp:rw newfile.txt
      
    4. Remove ACL permission of the user

       # [syntax: setfacl -x u:<user_name> <name of file or directory>]
       ubuntu@ip-172-31-94-49:~$ setfacl -x u:steve newfile.txt
      
    5. Remove ACL permission from the group

       # [syntax: setfacl -x g:<group_name> <name of file or directory>]
       ubuntu@ip-172-31-94-49:~$ setfacl -x g:testgrp newfile.txt
      
    6. Remove all ACL permissions

       # [syntax: setfacl -b <name of file or directory>]
       ubuntu@ip-172-31-94-49:~$ setfacl -b newfile.txt