Change file / directory permission using Chmod and Chown in Ubuntu Linux
Linux uses chmod and chown to change access permission of directory and files.
Chmod is used for change mode for file and direcories. It changes access permission for all users in Linux.
Chown is used to get the ownership for file and derectory. It changes access permission for given user.
There are 3 types of permission in linux operation system.
As in above table you can see for READ you need to give 4, WRITE need 2 and EXECUTE need 1. So if you need to give more than one permission to a file or directory you have to sum the permission.
For READ and WRITE : 4+2 = 6
For READ, WRITE and EXECUTE : 4+2+1 = 7
Now you need to know usertype for Linux operation system. Linux has 3 type of uses set:
|o||Othes than User and Group|
Now you can set the mode with chmod for a file or directory with following examples:
To change the example.txt file permission.
sudo chmod 644 example.txt
In above command READ + WRITE permission is set for user, READ for group and all.
To change the directory permission write directory name at the place of file. For directory you can use -R switch for change the permission recursively.
sudo chmod -R 644 mydirectory/
You can also use letters to chage access mode.
sudo chmod u+rw, o+r example.txt
For execute permission add +x permission.
sudo chmod +x myapp.run
To remove access permission on a file use -
sudo chmod u=r+w,o=r-w,g=-r-w example.txt
Now you will know how to change ownership for a file or directory.
chown -R <user>:<group> <Directory/File>
sudo chown -R ubuntu:ubuntu mydirectory/
Difference between chmod and chown:
As in example image Chown provides ownership to the file/directory. It is now for public.
Chmod changes the mode of file for all users that belongs in the provided user type/group.