banner image
Sedang Dalam Perbaikan

How to change File Permissions in Ubuntu



Although there are already a lot of good security features built into Linux-based systems, one very important potential vulnerability can exist when local access is granted; that is file permission based issues resulting from a user not assigning the correct permissions to files and directories. So based upon the need for proper permissions, I will go over the ways to assign permissions and show you some examples where modification may be necessary.

Permission Groups

There are mainly 3 types of users introduced in Linux Operating Systems. These are related to files and also directories.
  1. Owner  -  The Owner permissions are allowed only to the owner of the file or directory.
  2. Group  -  The Group permissions are allowed only to the group that uses the file or directory.
  3. Other users  -  All users are allowed to use files or directories

Permission Types

  1. Read  -  Ability to read a file
  2. Write  -  Ability to write and modify a file or a directory
  3. Execute  -   Ability to execute a file or view the contents of a directory
If you want to view  the permissions, you can access them using terminal. Give "ls -l" command to see the permissions. An example is shown here.



File permissions are based on the following format.


rwxrwxrwx

Here, 3 letters are reserved for each type of user. First rwx is for the owner, next rwx is for the group users and the last rwx is for the other users. You can see letter " d " or " - "sign before the file permissions in each line. What is it? Let me explain. Here, letter d is for directories and - sign is for files.
                                    r - Read
                                    w - Write
                                    x - Execute

The types of users are indicated as following.
                                    u - Owner
                                    g - Group
                                    o - Others
                                    a - All users

There are two types of assignment operators for these users. They are " + " and " - " operators. Mainly + operator is adding permissions while - operator is doing removing permissions for a particular user. We use chmod command with super user privileges(sudo), for modifying file  or directory permissions. 

Binary References to set permissions

How to we change fie permissions? Getting to know about users is not enough to do it. This is the most crucial part. We use a number set to indicate several types of permissions. There's a standard for it. Specific values have been reserved for 3 types of permissions; read, write and execute.
                                            r  -  4
                                            w  -  2
                                            x  -  1
These numbers should be used when a permission is given to a file or directory. But how? I will explain it simply. 
First example :
      sudo chmod 777 /opt/lampp/htdocs

After we installed XAMPP(I think you followed my previous article about it), there will be a folder called htdocs lampp folder. Navigate opt > lampp to see it. You can not paste anything to this folder by default. Permission is denied. But you need that permission to paste your PHP Projects folders in the htdocs root folder. Otherwise you can not run PHP files. So, this is the way to get permission. Open a terminal and type the above command! Now you can paste anything in this folder.

Explanation is here

The first 7 is for owner, next 7 is for group users and the remaining 7 is for other users. What is this 7? Simply 7 means 4 + 2 + 1. So, this first 7 means that the owner has been given all types of actions including read, write and execute. The same privileges have been given to the other users and other users because permission number is 7 for them also. I think you got it!!! 

There's an example of changing permissions of the folder called "PHPProjects". Steps are included with codes in the screenshots. 
Important : Whenever you are going to change permissions, Ubuntu asks for your password. After giving it hit enter.




Look at the line of PHPProjects folder! In first picture last 3 letters of the permission is shown as r-x. That means other users can not write anything in this folder. But they can read and executes files in this folder. After we have changed the permission, last 3 letters have converted into rwx. Modification has been done correctly!

Possible permissions :
  • 0 – no permission, this person cannot read, write or execute
  • 1 – execute only
  • 2 – write only
  • 3 – execute and write only (1 + 2)
  • 4 – read only
  • 5 – execute and read only (1 + 4)
  • 6 – write and read only (2 + 4)
  • 7 – execute, write and read (1 + 2 + 4)

Another way of changing permissions without numbers

Second example : 

When we install XAMMP using its installer, We gave the following command before we run the installer. 
          sudo chmod +x xampp-linux-x64-7.1.1-0-installer.run

As I told before we can user + and - for giving permissions to a specific user. Here, +x means give the permission to execute the installer to all users. Now I'll explain this with the previous example.
Now the PHPProjects folder is having all permissions for all user as drwxrwxrwx. I'm going to remove writing ability for other users. So output should be as drwxrwxr-x.



Change has been done! Look at the relevant line. 
Let me to explain some possible permission types for specific users.
  • o-x     Remove executing ability for other users
  • g+w   Remove writing ability for group users
  • u+r     Add reading ability for the owner
  • o-x     Remove executing ability for other users
  • g+r     Add reading ability for group users
  • u-w    Remove writing ability for the owner

Important : If we  give just +x or +w or +r, the modification is affected to all types of users. Same situation can be explained with the - operator. Whenever you are going to modify the permissions, you should look for its existing permissions by using ls - l command. Then go for the modification.

I think now you have got an idea on Linux file permissions. You can modify your files and directories now!



How to change File Permissions in Ubuntu How to change File Permissions in Ubuntu Reviewed by DAL on July 14, 2017 Rating: 5

No comments:

Powered by Blogger.