The chmod command set the permission to the file which will permit only the certain user to view the file.Chmod utility simply changes the way in which a file can be accessed by the owner of the file. the group to which the file belongs, and/or all other users. Only the owner of a file or the superuser can change the permission of the file.
chmod[option] who[operation][permission] file-list
Arguments give chmod information about which file are to have their modes changed in what ways.
Before moving into command list chmod changes the permission for certain class of users field by who. The class of letter is specificied by one of the following letters
|u||user||owner of the file|
|g||group||Group to which file belongs|
|o||other||all other users|
|a||all||can be used in place of u, g and o above|
Operation to be performed is defined by the following list
|+||Add permission for the specified user class|
|-||remove permission for the specified user class|
|=||set permission for the specified user-reset all other permissions for that user class|
The Access permission is defined by the following list
|s||set user ID or set group ID(depending on the who argument) to that of the owner of the file while the file is being executed|
|t||set the sticky bit(only the superuser can set the sticky bit, and it only can be used with u)|
Sticky bit is an access permission bit that cause an executable program to remain on the swap area of the disk. It takes less time to load a program that has its sticky bit set than one that does not. Only super user can set the sticky bit. The owner can only remove the file if the sticky bit is set on a directory that is publicly writable, only the owner of a file in the directory can remove the file.
|400||Read by owner|
|040||Read by group|
|004||Ready by others|
|200||Write by owner|
|020||Write by group|
|002||Write by anybody|
|100||Execute by owner|
|010||Execute by group|
Some chmod modes
|777||Owner, group and public can read, write and execute file|
|755||Owner can read, write, and execute; group and public can read and execute file|
|644||Owner can read and write; group and public can read file.|
|711||Owner can read, write and execute;group and public can execute file.|
-c, –changes like verbose but report only when a change is made
–no-preserve-root do not treat `/’ specially (the default)
–preserve-root fail to operate recursively on `/’
-f, –silent, –quiet suppress most error messages
-v, verbose output a diagnostic for every file processed
–reference=RFILE use RFILE’s mode instead of MODE values
-R, –recursive change files and directories recursively
–help display this help and exit
–version output version information and exit
To add the permission:
I have a file recorfile.txt with the following permission which I viewed using ls -l command:
“-rw-rw-r–” shows us the permission. The first ‘-’ makes it clear that it is a file, if it’s a directory it would have shown ‘d’. Now the user have read and write permission, group have read and write permission and other users have read permission only. I want to add execute permission to the group and both write and execute permission to the other users. To grant the permission to group and others I would type:
chmod go+wx recordfile.txt
If I want to add the permission to all the three users. I would type chmod ugo+wx recordfile.txt.
If I want to add the permission to only others, I would type chmod g+wx recordfile.txt.
See the result, the writing and executing permission is added to group and others.
To remove the permission replace ‘+’ with ‘-’.
chmod ugo-rwx recordfile.txt
Adding permission using decimal:
You can also add the terminal using the decimal but it’s not recommended. The decimal value for read, write and execute is 4, 2 and 1 respectively. To add all the three permission type 7, to add only read and execute permission type 5, to add read and write permission add 6. Add the number and write the value(refer above table). To give all the permissions to all the users I would type:
chmod 777 recordfile.txt
It’s simple. You just need to play with the chmod command for little long. Practice it, play with it, you will be perfect!